C# SMTP 电子邮件发送代码对于 Yahoo Mail 失败,但对于其他服务器工作正常,有人可以帮忙吗?

发布于 2024-10-19 05:45:06 字数 1436 浏览 8 评论 0原文

我正在使用此代码通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅笑依然 2024-10-26 05:45:06
using System.Net.Mail;
using System.Net;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btn_Send_Click(object sender, RoutedEventArgs e)
    {
        MailMessage oMail = new MailMessage(new MailAddress("[email protected]"), new MailAddress("[email protected]"));
        SmtpClient oSmtp = new SmtpClient();
        oSmtp.Host = "smtp.mail.yahoo.com";
        oSmtp.Credentials = new NetworkCredential("username", "password");
        oSmtp.EnableSsl = false;
        oSmtp.Port = 587;
        oSmtp.Send(oMail);
    }
}
using System.Net.Mail;
using System.Net;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btn_Send_Click(object sender, RoutedEventArgs e)
    {
        MailMessage oMail = new MailMessage(new MailAddress("[email protected]"), new MailAddress("[email protected]"));
        SmtpClient oSmtp = new SmtpClient();
        oSmtp.Host = "smtp.mail.yahoo.com";
        oSmtp.Credentials = new NetworkCredential("username", "password");
        oSmtp.EnableSsl = false;
        oSmtp.Port = 587;
        oSmtp.Send(oMail);
    }
}
青衫儰鉨ミ守葔 2024-10-26 05:45:06

不支持通过 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

情未る 2024-10-26 05:45:06

System.Net.Mail.SmtpClient 不支持端口 465。

http://msdn.microsoft.com/en -us/library/system.net.mail.smtpclient.enablessl.aspx

从备注部分:

此连接方法有时称为 SMTP/SSL、SMTP over SSL 或 SMTPS,默认情况下使用端口 465。当前不支持这种使用 SSL 的备用连接方法。

编辑:
您可以尝试使用端口 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:

This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.

Edit:
You could try using port 587 instead (if Yahoo supports it).

黄昏下泛黄的笔记 2024-10-26 05:45:06

我遇到了同样的问题,直到我将端口设置为 587 并禁用 SSL。

I had the same problem until I set the port to 587 and disabled SSL.

昔日梦未散 2024-10-26 05:45:06

我认为您应该恢复使用 System.Web.Mail ,它可以让您控制无法通过较新的 System.Net 访问的字段。
尝试玩那些。例如你可以尝试这个:
(使用记录在此处,字段记录在此处

        MailMessage msg = new MailMessage();            
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.mail.yahoo.com");   
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");

        // try "2", I have not tested for yahoo mail
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");                                    
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "1");                        
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // 0= anonymous - 1=basic - 2=NTLM
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "yahoousername");
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "yahoopwd");

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)

        MailMessage msg = new MailMessage();            
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.mail.yahoo.com");   
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");

        // try "2", I have not tested for yahoo mail
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");                                    
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "1");                        
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // 0= anonymous - 1=basic - 2=NTLM
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "yahoousername");
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "yahoopwd");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文