首页 > 解决方案 > 无法从 MVC 发送邮件

问题描述

我正在尝试通过 MVC 发送电子邮件。我特别说 MVC 是因为相同的代码(对于Net.Mail.

我用谷歌搜索并尝试了各种设置,但没有运气。我认为这可能是 MVC 或我正在使用的某些平台的问题。

SmtpClient smtp = new SmtpClient();
smtp.Port = Convert.ToInt16(ConfigurationManager.AppSettings["mailPort"]);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["mailAccount"], ConfigurationManager.AppSettings["mailPassword"]);
smtp.Host = ConfigurationManager.AppSettings["mailHost"];
smtp.Timeout = (20 * 1000);

MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
mail.From = new System.Net.Mail.MailAddress(ConfigurationManager.AppSettings["mailAccount"]);
mail.To.Add(new MailAddress(item.Recipient));
mail.Subject = item.Subject;
mail.Body = item.Body;

smtp.Send(mail);

我所需要的只是一种从 MVC 发送邮件的方法

更新:我得到的错误:

System.Net.Mail.SmtpException:发送邮件失败。---> System.IO.IOException: Unable to read data from the transport connection: 一个现有的连接被远程主机强行关闭。---> System.Net.Sockets.SocketException: 远程主机在 System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) 在 System. Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 大小)在 System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) 在 System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) 在 System.Net.Security.SslState .

[更新] 我的发现:

Platform    Port    SSL Result
MVC         587 true    Error
MVC         587 false   got mail
MVC         465 true    Time out
MVC         465 false   Time out

WinForm     587 true    got mail
WinForm     587 false   got mail
WinForm     465 true    Time out
WinForm     465 false   Time out

在 Thunderbird 上,我使用端口 587 和 STARTTLS。

有任何想法吗

标签: c#asp.net-mvcemailsystem.net.mail

解决方案


推荐阅读