2011-03-25 261 views
1

SMTP的發送方法始終引發異常。該消息是:發送郵件失敗發送電子郵件的問題

這裏是我的代碼:

MailMessage mm = new MailMessage(); 
mm.To.Add("[email protected]"); 

mm.From = new MailAddress("[email protected]"); 
mm.Subject = "Ant Subject"; 
mm.Body = "Body Cha MEssag here "; 

SmtpClient ss = new SmtpClient("localhost"); 

ss.EnableSsl = false; 

try 
{ 
    **ss.Send(mm);** 
    Result.Text = "Message Sent"; 
    Result.ForeColor = System.Drawing.Color.Green; 
} 
catch (SmtpException ex) 
{ 
    Result.Text = "Message Not Sent : \n\n " + ex.Message; 
    Result.ForeColor = System.Drawing.Color.Red; 
} 

我使用

ss.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; 

現在,它不會引發任何異常也試過,它執行罰款,但接收器在他的收件箱中沒有收到任何郵件。

我該如何解決這個問題?

編輯 - 這是堆棧跟蹤即時得到

Message=Failure sending mail. Source=System StackTrace: at System.Net.Mail.SmtpClient.Send 
(MailMessage message) at WebApplication1._Default.Page_Load(Object sender, EventArgs e) in D:\Emcure- 
Kumar\Work\Not in Use\WebApplication1\WebApplication1\Default.aspx.cs:line 30 InnerException: 
System.Net.WebException Message=Unable to connect to the remote server Source=System StackTrace: – 
    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) at System.Net.ConnectionPool.GetConnection(Object owningObject, 
GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) 
    at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at 
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at 
System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) 
InnerException: System.Net.Sockets.SocketException Message=No connection could be made because the 
target machine actively refused it 127.0.0.1:25 Source=System ErrorCode=10061 NativeErrorCode=10061 
+0

發送郵件失敗 – kumar 2011-03-25 08:42:34

+0

@他有,它是「發送郵件失敗」。 – 2011-03-25 08:43:56

+3

您可以發佈完整的詳細信息(類型和* all *屬性,包括堆棧跟蹤和innerException類型和* all *屬性,如果它不爲null),而不僅僅是消息。 – Richard 2011-03-25 08:59:09

回答

0

嘗試在你的web.config第一次使用此:

<system.net> 
    <mailSettings> 
     <!-- Use this setting for development 
     <smtp deliveryMethod="Network"> 
     <network host="mail.mydomain.com" port="25" /> 
     </smtp> 
     --> 
     <smtp deliveryMethod="SpecifiedPickupDirectory"> 
     <specifiedPickupDirectory pickupDirectoryLocation="C:\Tmp"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 

這會將所有文件複製到C:\ TMP。 你可以只實例化類是這樣的:

SmtpClient client = new SmtpClient(); 

而改變配置在你的web.config之後。試試看,如果這有幫助,請告訴我們。

+0

它不工作的敵人我 – kumar 2011-03-25 09:22:46

+0

System.Net.Mail.SmtpException被捕獲 消息=發送郵件失敗。 Source = System StackTrace: at System.Net.Mail.SmtpClient.Send(MailMessage message) at WebApplication1._Default。D:\ Emcure-Kumar \ Work \中的Page_Load(Object sender,EventArgs e)\未使用\ WebApplication1 \ WebApplication1 \ Default.aspx.cs:line 30 InnerException:System.IO.DirectoryNotFoundException Message =找不到零件的路徑'C:\ Tmp \ cd7da094-97a5-4057-af9d-e17a842b89c4.eml'。 – kumar 2011-03-25 09:23:16

+0

是否存在「c:\ Tmp」? – 2011-03-25 09:25:30

2

你execption應包含比「發送郵件失敗」

對於這個鏈接調試找對方法SmtpClient.Send拋出產生的異常細節的詳細信息 - >SmtpClient.Send Method

該代碼可以使用「SMTP .google.com」

 MailMessage mm = new MailMessage(); 
     mm.From = new MailAddress("xx"); 
     mm.To.Add("xx"); 
     mm.Subject = "Ant Subject"; 
     mm.Body = "Body Cha MEssag here "; 

     SmtpClient ss = new SmtpClient(); 
     ss.Host="smtp.gmail.com"; 
     ss.Port = 587; 
     ss.EnableSsl = true; 

     ss.Credentials = new System.Net.NetworkCredential("xx", "xx"); 
     try 
     { 
      ss.Send(mm); 
      Label1.Text = "Message Sent"; 
      Label1.ForeColor = System.Drawing.Color.Green; 
     } 
     catch (SmtpException ex) 
     { 
      Label1.Text = "Message Not Sent : \n\n " + ex.Message; 
      Label1.ForeColor = System.Drawing.Color.Red; 
     } 

谷歌需要啓用SSL和端口587是輸出端口。您還需要一個Google帳戶以及憑據。

沒有錯,你的代碼 - 這是最有可能您的服務器或防火牆

+0

消息=發送郵件失敗 來源=系統 堆棧跟蹤:? 在System.Net.Mail.SmtpClient.Send(消息MAILMESSAGE) 在WebApplication1._Default.Page_Load(對象發件人, EventArgs e)D:\ Emcure-Kumar \ Work \ Not in Use \ WebApplication1 \ WebApplication1 \ Default.aspx.cs:line 30 InnerException:System.Net.WebException Message =無法連接到遠程服務器 Source =系統 堆棧跟蹤: – kumar 2011-03-25 09:28:05

+0

在System.Net.ServicePoint.GetConnection(PooledStream PooledStream,對象所有者,布爾異步,ip地址和地址,插座&abortSocket,插座&abortSocket6,的Int32超時) 在System.Net.PooledStream.Activate(對象owningObject,布爾異步, INT32超時,GeneralAsyncDelegate的AsyncCallback) 在System.Net.PooledStream.Activate(對象owningObject,GeneralAsyncDelegate的AsyncCallback) 在System.Net.ConnectionPool.GetConnection(在System.Net.Mail.SmtpConnection對象owningObject,GeneralAsyncDelegate的AsyncCallback,INT32 creationTimeout) – kumar 2011-03-25 09:28:53

+0

.GetConnection(String host,Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host,Int32 port) at System.Net.Mail.SmtpClient.GetConnection() 在System.Net.Mail.SmtpClient.Send(消息MAILMESSAGE) 的InnerException:System.Net.Sockets.SocketException 消息=無連接可以作出,因爲目標機器積極地拒絕它127.0.0.1:25 源=系統 ErrorCode = 10061 NativeErrorCode = 10061 – kumar 2011-03-25 09:29:42

0

試試這個

SmtpClient smtpClient = new SmtpClient(); 
    MailMessage message = new MailMessage(); 

    try 
    { 
     // System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 
     MailAddress fromAddress = new MailAddress(txt_name.Text, txt_to.Text); 


     smtpClient.Host = "smtp.gmail.com"; 


     smtpClient.Port = 25; 

     // msg.From = new System.Net.Mail.MailAddress("[email protected]"); 

     message.From = fromAddress; 

     message.To.Add("[email protected]"); 


     message.Body = txt_des.Text; 
     smtpClient.EnableSsl = true; 

     System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(); 

     NetworkCred.UserName = "[email protected]"; 

     NetworkCred.Password = "xtz"; 

     smtpClient.UseDefaultCredentials = true; 

     smtpClient.Credentials = NetworkCred; 


     smtpClient.Send(message); 

     lblStatus.Text = "Email successfully sent."; 
    } 
    catch (Exception ex) 
    { 
     lblStatus.Text = "Send Email Failed." + ex.Message; 
    } 
1

我有同樣的問題,而且是我代理,我不知道是不是真的,因爲我即有一個具有良好配置的代理,並且我讀取了一個C#應用程序獲取OS代理的配置。我只是改變我的conecction一些沒有代理和運行得很好。所有的解決方案,第一,第二和第三。也許是代理或防火牆,或者嘗試在其他PC上進行確認。我希望能幫助你。