Wednesday, May 19, 2010

System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Unable to relay

10:52 PM Posted by Unknown , No comments
I have got this issue many times, when I try to send email using smtp. There can be several causes for this. You have to allow relay for mail to be sent to outside servers like gmail.com. For sending mail within your server you don't have to do this. You can find some helps from the following links:
Help1
Help2
Help3

The code I have used is given below:

using System.Net.Mail;


SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
smtpClient.Port = 25;
smtpClient.Host = "smtp.domain.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("webmail username", "webmail password");
message.From = new MailAddress("webmail username");
message.To.Add("test@gmail.com");
string emailBody = "Hi,

Welcome

";
message.Body = emailBody;
message.Subject = "testing";
message.IsBodyHtml = true;
smtpClient.Send(message);

But, after making the changes on the server, as specified on the above links, I was still getting the same error. Then I added the following line in between the above code.
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

Then the error was gone.

0 comments:

Post a Comment