2013-04-29 2199 views
1

这是相关的代码(C#)System.Net.Mail.SmtpException:发送邮件失败。无法从传输连接

    MailMessage m; 
        SmtpClient client; 

        client.Host = "smtp.mail.yahoo.com"; 
        client.Port = 465; 
        client.EnableSsl = true; 
        client.Credentials = new NetworkCredential("[email protected]", "mypassword"); 

        m = new MailMessage(); 
        m.From = new MailAddress("[email protected]"); 
        m.To.Add(new MailAddress("[email protected]")); 
        m.Subject = "My subject"; 
        m.Body = "This is my body. "; 

        client.Send(m); 

这是完全错误跟踪

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 
    at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) 
    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) 
    --- End of inner exception stack trace --- 
    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) 
    at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) 
    at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) 
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) 
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) 
    at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) 
    at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) 
    at System.Net.Mail.SmtpClient.GetConnection() 
    at System.Net.Mail.SmtpClient.Send(MailMessage message) 
    --- End of inner exception stack trace --- 
    at System.Net.Mail.SmtpClient.Send(MailMessage message) 
    at EmailSender.EmailSenderForm.send() in c:\Users\Ankur\Documents\Visual Studio 2012\Projects\EmailSender\EmailSender\EmailSenderForm.cs:line 91 

什么我错在这里做读取数据?

回答

0

据我所知,雅虎不允许第三方邮件客户端操作他们的帐户。这就是为什么您需要使用Thunderbird进行网络邮件扩展的原因。雅虎为这些服务提供Yahoo Mail Plus。您可以使用Gmail中的其他邮件帐户试用您的应用程序吗?在那里它应该工作得很好。

我将这段代码配置为用于发送电子邮件的Outlook帐户。我现在工作了两年,现在没有任何修改。它应该开箱即用(只需更改帐户名称):

MailMessage mm = new MailMessage(@"[email protected]", 
        dest, textBox1.Text, richTextBox1.Text); 
SmtpClient client = new SmtpClient("smtp.live.com", 587); 
client.Credentials = new NetworkCredential(@"examplehotmail.com", "password"); 
client.EnableSsl = true; 
messageSent = false; 
client.SendAsync(mm, null); 
client.SendCompleted += SentMessageTrigger; 

我希望它有帮助。

+0

我没有Gmail,但我确实有学校提供的Outlook帐户。它的设置如下所示 - http://i.imgur.com/q1ZEUPb.png。当我尝试使用此帐户的代码时,出现以下错误: – 2013-04-30 08:43:44

+0

System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:由于目标机器主动拒绝而无法建立连接157.56.238.22:587 – 2013-04-30 08:44:58

+0

那么,我已经使用不同的设置配置了Outlook。对于smtp我有地址'smtp.live.com',它的工作原理。我有一个代码可以完成你想要的功能(通过c#发送和通过这个类发送的电子邮件),使用gmail进行配置。我会检查它,但我强烈建议gmail只是为了测试,因为它是更正常的*邮件帐户,当涉及到这个东西。 – 2013-04-30 12:46:21

相关问题