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

时间:2011-02-23 14:31:40

标签: c# smtp yahoo smtpclient mailmessage

我使用此代码通过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("username@yahoo.com", 
                                         "to.someone@gmail.com");

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将这些确切的设置发送到同一台服务器。我想知道我是否需要发送证书或什么?

如果您或您认识的任何人对此有任何想法,我将非常感谢您的帮助。

5 个答案:

答案 0 :(得分:6)

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("username@yahoo.com"), new MailAddress("username@yahoo.com"));
        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);
    }
}

答案 1 :(得分:3)

465不支持此功能,但以下帖子详细介绍了解决方法

How can I send emails through SSL SMTP with the .NET Framework?

更新:此链接详细说明了它可以通过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

答案 2 :(得分:2)

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支持它)。

答案 3 :(得分:0)

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

答案 4 :(得分:-1)

我认为您应该恢复为using System.Web.Mail,这样您就可以控制无法通过较新的System.Net访问的字段。 尝试玩那些。比如你可以试试这个: (使用记录here,字段记录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");