Unable to send email to other domains using System.Net.Mail.SmtpClient

半城伤御伤魂 提交于 2019-12-04 05:42:18

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/6659210/unable-to-send-email-to-other-domains-using-system-net-mail-smtpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!