c# - 无法从 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。
有任何想法吗
解决方案
推荐阅读
- java - 如何从我的 Android 应用程序中发送电子邮件?
- c# - 由于虚拟内存不足,线程无法启动 C#
- arrays - 在数组中查找项目
- android - recyclerview 内部连接 SQLite
- angular - firebase 通过嵌套子 ID 获取 url,例如树
- unity3d - Unity - 更改顶点颜色
- ios - 关闭和呈现 UIViewController 时出现巨大延迟
- php - 如何在 debian 9 上安装 ffmpeg 4.x?
- python - 如何在 Python Cryptography 中为 ECDSA (secp256k1) 生成较短的私钥
- typescript - 如何获取省略最后一个参数的函数类型