C# sending email code suddenly stopped working
i have the following code that has worked fine for months but suddently stopped working on this line:
smtpClient.Send(msg);
this is an internal applicat开发者_StackOverflow中文版ion sending emails to internal people in my company
with the following error:
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for ABC@comp.com
can anyone think of a reason why this code would work fine for ages and suddently just stop work ?
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromEmailAddress_);
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress(email.Trim()));
msg.Subject = subject_;
msg.Body = body_;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
var smtpClient = new SmtpClient(_mailServer);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(_user, _pwd);
try
{
smtpClient.Send(msg);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
The mail server you're connecting to has been tightened up to not allow relaying either for your credentials or from your network/IP. This is not a code issue (short of changing the SMTP server) but an issue or question for whoever manages the SMTP server you're using.
Apart from checking to ensure the smtp service is running you can try the following:
Try setting the SmtpDeliveryMethod enum to PickupDirectoryFromIis.
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpdeliverymethod.aspx
精彩评论