无法使用 System.Net.Mail.SmtpClient 将电子邮件发送到其他域
请看下面的代码:
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);
我目前无法使用它发送电子邮件。该代码驻留在使用旧 ASPX 引擎的 ASP.NET MVC 3 应用程序中。它在带有 ASP.NET 4.0 的 IIS7 服务器上以集成管道模式运行。
运行此代码时,会发生以下两种情况之一:
- 如果 RECIPIENT_ADDRESS 等于 MY_OWN_ADDRESS 或我的域中的另一个电子邮件地址,则电子邮件会发送并且一切正常。
- 但是,如果 RECIPIENT_ADDRESS 是其他服务器(例如 gmail)上的任何电子邮件地址(无论是否有效),则在 SmtpClient.Send 期间会发生异常:
邮箱不可用。服务器响应为:中继需要身份验证
描述:当前 Web 请求执行期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.Net.Mail.SmtpFailedRecipientException:邮箱不可用。服务器响应是:中继需要身份验证
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到您的
message.To.Add(RECIPIENT_ADDRESS)
可能是一个字符串。我过去曾遇到过这个问题。将其包装在MailAddress
对象中,它应该可以工作。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 aMailAddress
object and it should work.