2012-05-04 82 views
0

我試圖發送電子郵件使用此代碼..但在發生錯誤smtp.Send(郵件);短信 「發送郵件失敗」發送電子郵件在C#

MailMessage mail = new MailMessage(); 
    // set the addresses 
    mail.From = new MailAddress("[email protected]"); 

    mail.To.Add(new MailAddress("[email protected]")); 

    // set the content 
    mail.Subject = "test sample"; 
    mail.Body = @"thank you"; 
    SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 

    smtp.Credentials = new NetworkCredential("[email protected]", "password"); 
    smtp.Send(mail); 
+2

您需要提供更多來自錯誤的細節 - 拋出異常嗎?什麼是完整的異常字符串跟蹤? – 2012-05-04 06:57:22

+0

您是否使用From和To的真實電子郵件地址?這段代碼要做的就是去gmail並把你的郵件放在smtp隊列中,但在它發出之前它會檢查你發送的有效郵件。這意味着你的地址需要合法 – JonVD

+0

你是否缺少mail.Host?和mail.Port? –

回答

0

您需要設置smtp.EnableSsl = true的Gmail。

看看這個類,它應該爲你工作:

public class Email 
{ 
    NetworkCredential credentials; 
    MailAddress sender; 

    public Email(NetworkCredential credentials, MailAddress sender) 
    { 
     this.credentials = credentials; 
     this.sender = sender; 
    } 

    public bool EnableSsl 
    { 
     get { return _EnableSsl; } 
     set { _EnableSsl = value; } 
    } 
    bool _EnableSsl = true; 

    public string Host 
    { 
     get { return _Host; } 
     set { _Host = value; } 
    } 
    string _Host = "smtp.gmail.com"; 

    public int Port 
    { 
     get { return _Port; } 
     set { _Port = value; } 
    } 
    int _Port = 587; 

    public void Send(MailAddress recipient, string subject, string body, Action<MailMessage> action, params FileInfo[] attachments) 
    { 
     SmtpClient smtpClient = new SmtpClient(); 

     // setup up the host, increase the timeout to 5 minutes 
     smtpClient.Host = Host; 
     smtpClient.Port = Port; 
     smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; 
     smtpClient.UseDefaultCredentials = false; 
     smtpClient.Credentials = credentials; 
     smtpClient.Timeout = (60 * 5 * 1000); 
     smtpClient.EnableSsl = EnableSsl; 

     using (var message = new MailMessage(sender, recipient) 
     { 
      Subject = subject, 
      Body = body 
     }) 
     { 
      foreach (var file in attachments) 
       if (file.Exists) 
        message.Attachments.Add(new Attachment(file.FullName)); 
      if(null != action) 
       action(message); 
      smtpClient.Send(message); 
     } 
    } 
} 
0

填充 mail.Host 和 mail.Port與正確的價值觀

0

屬性您應該在創建一個新的MailMessage時使用using語句,加上一些你錯過了的東西,比如端口號和啓用SSLL

using (MailMessage mail = new MailMessage()) 
{ 
    mail.From = new MailAddress("[email protected]"); 
    mail.To.Add(new MailAddress("[email protected]")); 
    mail.Subject = "test sample"; 
    mail.Body = @"thank you"; 

    SmtpClient smtpServer = new SmtpClient("smtp.gmail.com"); 
    smtpServer.Port = 587; 
    smtpServer.Credentials = new NetworkCredential("[email protected]", "password"); 
    smtpServer.EnableSsl = true; 
    smtpServer.Send(mail); 
} 
+0

c0deNinja謝謝!你說得對...再次感謝你.. – blessie

0

這裏有一個基本的Gmail SMTP電子郵件實現我寫了前段時間:

public static bool SendGmail(string subject, string content, string[] recipients, string from) 
{ 
    bool success = recipients != null && recipients.Length > 0; 

    if (success) 
    { 
     SmtpClient gmailClient = new SmtpClient 
     { 
      Host = "smtp.gmail.com", 
      Port = 587, 
      EnableSsl = true, 
      UseDefaultCredentials = false, 
      Credentials = new System.Net.NetworkCredential("******", "*****") //you need to add some valid gmail account credentials to authenticate with gmails SMTP server. 
     }; 


     using (MailMessage gMessage = new MailMessage(from, recipients[0], subject, content)) 
     { 
      for (int i = 1; i < recipients.Length; i++) 
       gMessage.To.Add(recipients[i]); 

      try 
      { 
       gmailClient.Send(gMessage); 
       success = true; 
      } 
      catch (Exception) { success = false; } 
     } 
    } 
    return success; 
} 

應該做工精細你,但你需要添加一個有效的Gmail acocunt在那裏我已經標註在碼。

0

這是我檢查發送郵件的功能,它工作正常。

`

 private static bool testsendemail(MailMessage message) 
     { 

      try 

      { 

      MailMessage message1 = new MailMessage(); 

      SmtpClient smtpClient = new SmtpClient(); 

      string msg = string.Empty; 

      MailAddress fromAddress = new MailAddress("[email protected]"); 
      message1.From = fromAddress; 
      message1.To.Add("[email protected]"); 
      message1.Subject = "This is Test mail"; 
      message1.IsBodyHtml = true; 
      message1.Body ="You can write your body here"+message; 
      smtpClient.Host = "smtp.mail.yahoo.com"; // We use yahoo as our smtp client 
      smtpClient.Port = 587; 
      smtpClient.EnableSsl = false; 
      smtpClient.UseDefaultCredentials = true; 
      smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "YourPassword"); 

      smtpClient.Send(message1); 
     } 
     catch 
     { 
      return false; 
     } 
     return true; 

    }`   

謝謝。