2013-04-03 40 views
1

**它只是說「發送郵件失敗。」 不確定它的代碼或SMTP服務器的問題?請幫助這裏是我的代碼,變量通過形式發送,我已經確認了所有的變量都OK在C#中使用SMTP發送電子郵件#

SMTP設置是在Web.config中**

<?xml version="1.0"?> 
<configuration> 
    <system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <network host="mail.abc.com" port="25" userName="[email protected]" password="abc#[email protected]"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 
    <system.web> 

    </system.web> 
</configuration> 

代碼在C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net.Mail; 

public partial class SendMail : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void cmdSend_Click(object sender, EventArgs e) 
    { 
     MailMessage mMailMessage = new MailMessage(); 

     // address of sender 
     mMailMessage.From = new MailAddress(txtFrom.Text); 
     // recipient address 
     mMailMessage.To.Add(new MailAddress(txtTo.Text)); 
     // Check if the bcc value is empty 
     if (txtBcc.Text != string.Empty) 
     { 
      // Set the Bcc address of the mail message 
      mMailMessage.Bcc.Add(new MailAddress(txtBcc.Text)); 
     } 

     // Check if the cc value is empty 
     if (txtCc.Text != string.Empty) 
     { 
      // Set the CC address of the mail message 
      mMailMessage.CC.Add(new MailAddress(txtCc.Text)); 
     }  // Set the subject of the mail message 
     mMailMessage.Subject = txtSubject.Text; 
     // Set the body of the mail message 
     mMailMessage.Body = txtBody.Text; 
     // Set the format of the mail message body as HTML 
     mMailMessage.IsBodyHtml = true; 
     // Set the priority of the mail message to normal 
     mMailMessage.Priority = MailPriority.Normal; 

     // Instantiate a new instance of SmtpClient 
     SmtpClient mSmtpClient = new SmtpClient(); 


     // Send the mail message 
     try 
     { 
      mSmtpClient.Send(mMailMessage); 
     } 
     catch (Exception ex) 
     { 
      ;//log error 
      lblMessage.Text = ex.Message; 
     } 
     finally 
     { 
      mMailMessage.Dispose(); 
     } 
    } 
} 
+0

哪裏是你的SMTP設置? – Sachin 2013-04-03 19:19:39

+0

你實例化了'SmtpClient'然後...什麼都沒有。你只是試圖用它來發送。 – 2013-04-03 19:20:04

+1

@Grant,只要設置存在於應用程序或Web配置中,它們將被拾取。 – 2013-04-03 19:20:44

回答

1

嘗試使用telnet命令來檢查您是否可以發送郵件。

開始 - >輸入 「telnet」:

open smtp.server.com 25 
Helo smtp.server.com 
Mail from:[email protected] 
Rcpt to:[email protected] 

Data 
Subject:The life 
OMG 
. 
Quit 

如果Telnet不存在,其添加通過添加/刪除的窗口的功能。請參閱:http://technet.microsoft.com/en-us/library/cc771275(v=ws.10).aspx

+0

我試過telnet它說的是C:\ Windows \ system32> telnet mail.abc.com 25 'telnet'不被識別爲內部或外部命令, 可操作的程序或批處理文件。 – user580950 2013-04-03 19:36:24

+0

對不起......它不是commnand提示。開始 - >輸入telnet – Guish 2013-04-03 19:42:04

+0

安裝Telnet,我應該在服務器上還是從任何客戶端機器上檢查Telnet命令? – user580950 2013-04-03 19:50:59

0

首先,我們沒有在指定的SMTP服務器名稱:

SmtpClient smtp = new SmtpClient(@"abc.company.com"); 

其次,在指定上述代碼段的SMTP服務器名稱後, 確保防火牆或Symantec(或類似程序)不阻止出站請求。

+0

首先,這些設置是在web配置中定義的 - 因此它們不需要傳遞到'ctor'中。 – 2013-04-03 19:26:58

+0

是的,這是正確的,我也試過,以及相同的錯誤 – user580950 2013-04-03 19:27:35

+1

正如OP指出,他的SMTP信息是在配置。請參閱http://msdn.microsoft.com/en-us/library/w355a94k.aspx – 2013-04-03 19:27:49

-1

在您的try catch塊中,您將需要檢查所有異常。保持循環,直到exception.innerexception爲空。郵件消息發送失敗的內部例外中會發現更詳細的錯誤消息。外部異常和相應的消息將是通用的並且沒有用處。

有些樣品可以在這裏找到:

Best way to check for inner exception?

這裏是一些代碼的僞

while (ex != null) 
    { 
     //LogThisException(ex); 
     ex = ex.InnerException; 
    } 
+0

我試着得到相同的消息「發送郵件失敗」 – user580950 2013-04-03 19:47:50

+0

嗯,通常有一個更好的消息在內部的異常。在這一行放置一個斷點:lblMessage.Text = ex.Message;並繼續擴大ex.InnerException。 – 2013-04-03 19:59:18