C# SMTP 电子邮件发送代码对于 Yahoo Mail 失败,但对于其他服务器工作正常,有人可以帮忙吗?
我正在使用此代码通过 yahoo SMTP 服务器发送 SMTP 电子邮件,它用于我正在编写的个人项目。
using System.Net.Mail;
using System.Net;
SmtpClient theClient = new SmtpClient("smtp.mail.yahoo.com", 465);
theClient.UseDefaultCredentials = false;
theClient.Credentials = new NetworkCredential("username", "password");
theClient.EnableSsl = true;
MailMessage theMessage = new MailMessage("[email protected]",
"[email protected]");
theMessage.Subject = "Dave test from C# subject";
theMessage.Body = "Dave test from C# body";
theClient.Send(theMessage);
这都是发送 SMTP 电子邮件的相当标准的代码,但是......服务器似乎抛出一个错误。它强制终止连接。如果我使用其他 SMTP 服务器(例如 Gmail、Windows Live 或各种其他 ISP Smtp 服务器),则不会发生这种情况。
这是异常和堆栈跟踪:
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at ConsoleApplication1.Program.Main(String[] args) in E:\dev\ARCSoftware.FTPProcessor\ConsoleApplication1\Program.cs:line 28
我知道问题不是环境问题,但我可以使用 Outlook Express 将这些精确设置发送到同一服务器。我想知道是否需要寄证书之类的?
如果您或您认识的任何人对此有任何想法,我将不胜感激。
I am using this code to send an SMTP email via the yahoo SMTP server, it is for a personal project I am writing.
using System.Net.Mail;
using System.Net;
SmtpClient theClient = new SmtpClient("smtp.mail.yahoo.com", 465);
theClient.UseDefaultCredentials = false;
theClient.Credentials = new NetworkCredential("username", "password");
theClient.EnableSsl = true;
MailMessage theMessage = new MailMessage("[email protected]",
"[email protected]");
theMessage.Subject = "Dave test from C# subject";
theMessage.Body = "Dave test from C# body";
theClient.Send(theMessage);
It's all pretty standard code for sending SMTP email, but... the server seems to throw an error. It forcibly terminates the connection. This does not happen if I use other SMTP servers like Gmail, Windows Live or various other ISP Smtp servers.
This is the exception and stack trace:
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at ConsoleApplication1.Program.Main(String[] args) in E:\dev\ARCSoftware.FTPProcessor\ConsoleApplication1\Program.cs:line 28
I know the problem is not environmental though as I can send to the same server with these exact settings using Outlook Express. I am wondering if I need to send a certificate or something?
If you, or anyone you know where has any ideas about this I would greatly appreciate some help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不支持通过 465,但以下帖子详细介绍了解决方法
如何使用 .NET Framework 通过 SSL SMTP 发送电子邮件?
更新:此链接详细说明了为什么它可以通过 Outlook Express 工作,但不能通过 System.Net.Mail
http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx
It's not supported through 465, but the following post details a workaround
How can I send emails through SSL SMTP with the .NET Framework?
UPDATE: This link details why it might work through Outlook Express, but not through the System.Net.Mail
http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx
System.Net.Mail.SmtpClient 不支持端口 465。
http://msdn.microsoft.com/en -us/library/system.net.mail.smtpclient.enablessl.aspx
从备注部分:
编辑:
您可以尝试使用端口 587(如果 Yahoo 支持)。
Port 465 isn't supported by System.Net.Mail.SmtpClient.
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx
From the Remarks Section:
Edit:
You could try using port 587 instead (if Yahoo supports it).
我遇到了同样的问题,直到我将端口设置为 587 并禁用 SSL。
I had the same problem until I set the port to 587 and disabled SSL.
我认为您应该恢复使用 System.Web.Mail ,它可以让您控制无法通过较新的 System.Net 访问的字段。
尝试玩那些。例如你可以尝试这个:
(使用记录在此处,字段记录在此处)
I think you should revert to
using System.Web.Mail
which lets you control fields that are not accessible through the newer System.Net.Try to play with those. For instance you could try this:
(use is documented here, fields are documented here)