2010-10-11 142 views
1

這裏是我的代碼:.NET爲什麼我無法發送電子郵件?

try 
{ 
    MailMessage m = new MailMessage 
     ("[email protected]", 
     "[email protected]", 
     "Quarterly data report.", 
     "Hello, world."); 

    SmtpClient client = new SmtpClient("smtp.gmail.com", 465); 
    client.Credentials = new NetworkCredential("[email protected]", "password"); 
    client.EnableSsl = true; 
    client.Send(m); 
    Console.WriteLine("sent"); 
} 
catch (InvalidOperationException ey) 
{ 
    Console.WriteLine(ey.Message); 
} 
catch (SmtpFailedRecipientException sm) 
{ 
    Console.WriteLine(sm.Message); 
} 
catch (SmtpException ex) 
{ 
    Console.WriteLine(ex.Message); 
} 

此代碼在SmtpException產生此錯誤: 「發送郵件失敗。」 全是例外的是:

ex.ToString() "System.Net.Mail.SmtpException: Failure sending mail. 
    ---> System.Net.WebException: Unable to connect to the remote server 
    ---> System.Net.Sockets.SocketException: A connection attempt failed because 
    the connected party did not properly respond after a period of time, or 
    established connection failed because connected host has failed to respond 
    74.125.67.109:587 
    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) 
    --- End of inner exception stack trace --- 
    at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) 
    at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) 
    at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)\r\n at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) 
    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 ConsoleApplication3.Program.Main(String[] args) in c:\\users\\alan\\documents\\visual studio 2010\\Projects\\ConsoleApplication3\\ConsoleApplication3\\Program.cs:line 24" string 
+4

接受你的其他問題的一些答案,或者人們會不太願意幫助你。 – 2010-10-11 16:48:31

+0

您的防火牆和網絡邊界的防火牆是否在正確的端口上打開? – 2010-10-11 16:49:33

+0

我不能,我需要一些聲譽去做 – Alan 2010-10-11 16:49:38

回答

0

嘗試使用這個片段發送一個簡單的電子郵件:

var smtpClient = new SmtpClient("smtp.gmail.com", 587) 
{ 
    Credentials = new NetworkCredential(
     "[email protected]", 
     "yourpassword" 
    ), 
    EnableSsl = true 
}; 
smtpClient.Send("[email protected]", "[email protected]", "subject", "body"); 

從你的代碼的差別就是587端口(而非465)。

+0

我測試過了,但它會拋出相同的錯誤。 無論如何謝謝 – Alan 2010-10-11 17:01:04

0

看起來您的腳本無法聯繫指定端口上的Gmail服務器。確保您的服務器可以通過遠程登錄到達該地址/端口並查看是否收到回覆。

0

一段時間後我發生了類似的情況。我遵循在System.Net中使用SmtpClient的模板。經過研究,我發現我的ISP制定了策略來放棄某些端口上的郵件流量,因爲他們希望人們通過他們自己的郵件服務器(打擊垃圾郵件)來路由流量。

我的情況很複雜,因爲我需要使用的端口(用於我的企業ymail帳戶)被我的ISP阻止。原來我唯一可以連接的郵件服務器是gmail。因此,您可以嘗試使用不同的郵件提供商,或與您的ISP就郵件丟失/阻止策略以及特定端口的處理方式進行交談。

下面是我工作:

private bool sendEmailAlert(string emailAddress, string subject, string message) 
     { 
      try 
      { 
       //Construct e-mail message: 
       string fromAddress = "[email protected]", 
         fromName = "Your Biz";//"Don't Reply"; 
       MailMessage email_msg = new MailMessage(); 
       email_msg.From = new MailAddress(fromAddress, fromName); 
       email_msg.Sender = new MailAddress(fromAddress, fromName); 
       email_msg.To.Add(emailAddress); 
       email_msg.Subject = subject; 
       email_msg.Body = message; 
       SmtpClient mail_client = new SmtpClient(); 
       NetworkCredential network_cdr = new NetworkCredential(); 
       network_cdr.UserName = "[email protected]"; 
       network_cdr.Password = "password"; 
       mail_client.Credentials = network_cdr; 
       mail_client.Port = 587; 
       mail_client.Host = "smtp.gmail.com"; 
       mail_client.EnableSsl = true; 

       //Send e-mail message; 
       mail_client.Send(email_msg); 
       return true; 
      } 
      catch (Exception ex) 
      { 
       StreamWriter errorFile = new StreamWriter("errorLog.txt", true); 
       errorFile.WriteLine(DateTime.Now.ToString() + ": sendEmailAlert exception: " + ex.ToString()); 
       errorFile.Close(); 
       return false; 
      } 
     } 
+0

老兄,你的代碼是等於我的代碼:U – Alan 2010-10-11 18:19:18

+0

該端口是不同的...張貼沒有刷新,所以我沒有看到他推薦改變你的端口。不過,有關探索ISP如何混淆「可疑」或「垃圾」流量的建議仍然存在。你也可能想確保你不是一個黑名單的IP地址。檢查spamhaus.org並查看您的IP地址是否在策略阻止列表中。如果是,您可以刪除並重新測試。許多ISP限制垃圾郵件阻止列表中列出的主機的郵件流量。 – kmarks2 2010-10-11 18:35:11

+0

好的,謝謝,但事實並非如此。 – Alan 2010-10-11 19:46:01

相關問題