简体   繁体   中英

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.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM