2011-07-07 112 views
0

我需要使用Gmail SMTP發送郵件,但它顯示Failure sending mail.使用Gmail SMTP

可以請你幫忙發送郵件嗎?

<system.net> 
    <mailSettings> 
     <smtp from="[email protected] "> 
     <network host="smtp.gmail.com" defaultCredentials="false" 
     port="587" userName ="[email protected]" password="[email protected]" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 


    public string SendEmail(bool readWebConfig) 
    { 
     string emailStatus = string.Empty; 

     string from = "[email protected]"; //Replace this with your own correct Gmail Address 

     //Replace this with the Email Address to whom you want to send the mail 
     string to = "[email protected]"; 
     string cc = ""; 
     string bcc = ""; 

     string fromName = "bryian"; 
     string userName = "[email protected]"; 
     string password = "[email protected]"; 

     Attachment attachment = null; 

     //if you want to send attachment, include the file location 
     // Attachment attachment = new Attachment("file location"); 

     SmtpClient client = new SmtpClient(); 
     client.EnableSsl = true; //Gmail works on Server Secured Layer 

     if (!readWebConfig) 
     { 
      client.Credentials = new System.Net.NetworkCredential(userName, password); 
      client.Port = 587; // Gmail works on this port 
      client.Host = "smtp.gmail.com"; //hotmail: smtp.live.com 
     } 

     try 
     { 
      using (MailMessage mail = new MailMessage()) 
      { 
       mail.From = new MailAddress(from, fromName); 
       mail.ReplyTo = new MailAddress(from, fromName); 
       mail.Sender = mail.ReplyTo; 

       if (to.Contains(";")) 
       { 
        string[] _EmailsTO = to.Split(";".ToCharArray()); 
        for (int i = 0; i < _EmailsTO.Length; i++) 
        { 
         mail.To.Add(new MailAddress(_EmailsTO[i])); 
        } 
       } 
       else 
       { 
        if (!to.Equals(string.Empty)) 
        { 
         mail.To.Add(new MailAddress(to)); 
        } 
       } 

       //CC 
       if (cc.Contains(";")) 
       { 
        string[] _EmailsCC = cc.Split(";".ToCharArray()); 
        for (int i = 0; i < _EmailsCC.Length; i++) 
        { 
         mail.CC.Add(new MailAddress(_EmailsCC[i])); 
        } 
       } 
       else 
       { 
        if (!cc.Equals(string.Empty)) 
        { 
         mail.CC.Add(new MailAddress(cc)); 
        } 
       } 

       //BCC 
       if (bcc.Contains(";")) 
       { 
        string[] _EmailsBCC = bcc.Split(";".ToCharArray()); 
        for (int i = 0; i < _EmailsBCC.Length; i++) 
        { 
         mail.Bcc.Add(new MailAddress(_EmailsBCC[i])); 
        } 
       } 
       else 
       { 
        if (!bcc.Equals(string.Empty)) 
        { 
         mail.Bcc.Add(new MailAddress(cc)); 
        } 
       } 

       string message = "this is a test message"; 

       mail.Subject = "test subject"; 

       mail.Body = "<div style=\"font: 11px verdana, arial\">"; 
       mail.Body += message.Replace("\n", "<br />") + "<br /><br />"; 
       mail.Body += "<hr /><br />"; 
       mail.Body += "<h3>Sender information</h3>"; 
       mail.Body += "<div style=\"font-size:10px;line-height:16px\">"; 
       mail.Body += "<strong>Name:</strong> " + fromName + "<br />"; 
       mail.Body += "<strong>E-mail:</strong> " + from + "<br />"; 
       mail.IsBodyHtml = true; 

       if (HttpContext.Current != null) 
       { 
        mail.Body += "<strong>IP address:</strong> " + HttpContext.Current.Request.UserHostAddress + "<br />"; 
        mail.Body += "<strong>User-agent:</strong> " + HttpContext.Current.Request.UserAgent; 
       } 

       //attachment 
       if (attachment != null) 
       { 
        mail.Attachments.Add(attachment); 
       } 

       client.Send(mail); 
       emailStatus = "success"; 
      } 
     } 

     catch (Exception ex) 
     { 
      emailStatus = ex.Message; 
     } // end try 

     return emailStatus; 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     string status = SendEmail(false); 
     Response.Write(status); 
    } 
+1

你的問題標記鍵似乎被打破。 – Raoul

回答

0

我試過你的代碼,它對我來說工作正常(憑我的憑據)。您是否嘗試過禁用防火牆或防病毒軟件? inner exception說什麼?

步驟禁用:

Windows XP Firewall

Windows 7 Firewall

Antivirus

+0

謝謝,它工作正常... –

+0

如何禁用防火牆? –

+0

我已經更新了我的答案。 – NaveenBhat

0

嘗試像這樣的網絡標籤設置enableSsl = 「真」:

<network host="smtp.gmail.com" enableSsl="true" defaultCredentials="false" 
    port="587" userName ="[email protected]" password="[email protected]" />