Unable to send email to other domains using System.Net.Mail.SmtpClient
Please look at the following code:
client.Credentials = new NetworkCredential(SMTP_SERVER_USERNAME, SMTP_SERVER_PASSWORD);
client.EnableSsl = false;
client.Host = SMTP_SERVER_HOSTNAME;
client.Port = 587;
client.UseDefaultCredentials = false;
client.Timeout = 4000;
MailMessage message = new MailMessage();
message.Body = "Test";
message.From = new MailAddress(MY_OWN_ADDRESS);
message.ReplyToList.Add(message.From);
message.Sender = message.From;
message.Subject = SUBJECT_LINE;
message.To.Add(RECIPIENT_ADDRESS);
I am currently unable to use it to send emails. The code resides in an ASP.NET MVC 3 application using the old ASPX engine. It runs on an IIS7 server with ASP.NET 4.0 in Integrated Pipeline mode.
When this code is run, one of twothings happens:
- If RECIPIENT_ADDRESS is the equal to MY_OWN_ADDRESS, or another email address on my domain, the email sends and all is well.
- However, if RECIPIENT_ADDRESS is any email address, working or otherwise, on another server such as gmail, an exception occurs during SmtpClient.Send:
Mailbox unavailable. The server response was: Authentication is required for relay
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net开发者_如何学Python.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: Authentication is required for relay
I noticed your message.To.Add(RECIPIENT_ADDRESS)
might have been a string. I've had issues with this in the past. Wrap it in a MailAddress
object and it should work.
精彩评论