2016-11-21 50 views
0

我有發送電子郵件代碼:爲什麼電子郵件不在這裏發送?

public static bool SendEmail(
    string fromEmail, string toEmail, 
    string subject, string body, 
    bool isBodyHtml = false, bool isThrowException = false) 
{ 
    var message = new MailMessage(fromEmail, toEmail) 
    { 
     IsBodyHtml = isBodyHtml, 
     Subject = subject, 
     Body = body, 
    }; 

    using (var client = new SmtpClient()) 
    { 
     try 
     { 
      client.Send(message); 
     } 
     catch (Exception ex) 
     { 
      if (isThrowException) 
      { 
       throw new Exception(ex.ToString()); 
      } 
     } 
    } 

    return true; 
} 

web.config我:

<system.net> 
    <mailSettings> 
     <smtp> 
     <network host="smtp.bizmail.yahoo.com" port="25" enableSsl="true" [email protected]" password="@cr123" defaultCredentials="true"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 
</configuration> 

現在我沒有任何異常,尚郵在另一端沒有收到。 我可以通過我的憑據登錄到yahoo.com。 仍然

client->credentils->domain="" 
client->ServicePoint = 'client.ServicePoint' threw an exception of type 'System.TypeInitializationException' 

它出了什麼問題?

該代碼運行良好...在1個月前,我沒有更改一行,出了什麼問題?

+3

這是你真正的用戶名和密碼! –

+1

沒有更多 - 我只是刪除它的情況下:P – BugFinder

+0

其中''實際上位於配置? –

回答

0

首先登場的所有感謝所有誰試圖幫助你。

正如在我提到的問題中,創建一個新的smtp對象時出現錯誤。

smtp將讀取web.config並創建一個關於web.config的對象。 所以我才知道問題出在web.config上。我也確信C#代碼沒有問題。所以我分析了web.config並找到了一些過濾器。

<filter level="TraceEventType.Error" /> 

基本上我用這個來刪除Azure版本的錯誤信息。

這是造成Serverpoint的問題,同時創建一個smtp對象。 所以我剛剛刪除了該行。它運行良好。

我不僅知道smtp標籤,web.config文件中的其他標籤也會影響當前的問題。

0

添加連接性和憑據,以您的代碼:

NetworkCredentials Credencials = new NetworkCredential(SmtpUsername, SmtpPassword); 

client.Host = SmtpHost; 
client.Port = SmtpPort; 
client.Credentials = Credencials; 
+0

沒有這個主機和端口的需要沒有這個...證書也沒有,但沒有域名cominng,不知道是否需要。但仍然沒有收到郵件。 –

相關問題