2014-02-12 17 views
1

我想發送一封電子郵件,每次我調用這個方法並嘗試發送它時,它會拋出一個錯誤「Failure Sending Mail。」。這不是很具描述性,我不確定哪一點甚至會失敗,因爲它經歷了所有的代碼,並且看起來沒問題。發送電子郵件 - 我哪裏錯了?

代碼:

public void SendEmail() 
{ 
    private string localhost = System.Net.Dns.GetHostName(); 

    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 
    msg.To.Add(strUserEmail); 

    //determine if the email sent was successful 
    if (blnFlag == true) 
    { 
     msg.Subject = "File Move was successful"; 
    } 
    else 
    { 
     msg.Subject = "File Move"; 
    } 

    msg.From = new System.Net.Mail.MailAddress(GlobalVars.strFromEmail); 
    msg.Body = strMessage; 
    SmtpClient client = new SmtpClient(localhost); 
    client.Send(msg); 
} 
+0

你嘗試把斷點並調試? –

+4

你在你的機器上運行SMTP服務嗎? – AnthonyLambert

+1

你能告訴我們'localhost'嗎? – tnw

回答

0
client.EnableSsl = true; 
NetworkCredential crend = new NetworkCredential("SenderMailAddress","SenderPassword"); 
client.Credentials = crend; 

,並嘗試捕捉髮送方法 - 這只是常見的choise)

+0

我在哪裏以及爲什麼需要這些代碼? – user3302467