2013-04-29 2198 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

相關問題