2014-09-19 163 views
0

我想通過Gmail服務器使用ASP.NET發送電子郵件。這是我的代碼,你能幫我解決超時問題嗎?使用ASP.NET發送電子郵件

 try 
     { 
      MailMessage mail = new MailMessage(); 
      SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 

      mail.IsBodyHtml = true; 
      mail.Priority = System.Net.Mail.MailPriority.High; 
      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add("[email protected]"); 
      mail.Subject = "Fees Reminder";     
      mail.Body = "Please, Student ID: " + username + " pay your fees " + sqlFeesValue + "."; 
      mail.Body += " <html>"; 
      mail.Body += "<body>"; 
      mail.Body += "<table>"; 
      mail.Body += "<tr>"; 
      mail.Body += "<td>User Name : </td><td>" + username + "</td>"; 
      mail.Body += "</tr>"; 
      mail.Body += "<tr>"; 
      mail.Body += "<td>Fees : </td><td> " + sqlFeesValue.ToString() +"</td>"; 
      mail.Body += "</tr>"; 
      mail.Body += "</table>"; 
      mail.Body += "</body>"; 
      mail.Body += "</html>"; 

      smtp.Port = 465; 
      smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "passwd"); 
      smtp.EnableSsl = true;     
      smtp.UseDefaultCredentials = true; 
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
      smtp.Send(mail); 
     } 
     catch (Exception error) 
     { 
      lblMessage.Visible = true; 
      lblMessage.Text = error.Message; 
     } 

在此先感謝

+1

查看http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail – 2014-09-19 08:52:09

+0

我想你需要在一個字符串變量中創建一個body變量,然後試試。或者可能是您正在使用錯誤的端口號 – Amit 2014-09-19 08:54:48

+0

嘗試使用端口587.您確定您因爲gmail中的新安全設置而沒有收到gmail超時? – Alexander 2014-09-19 08:55:44

回答

0

嘗試使用不同的端口號。 Gmail在發送郵件時將接受端口25或587,但使用端口465超時。

還要確保您也有UseDefaultCredentials = False。

+0

我也試過使用這些設置,但沒有爲我工作。 – Badar 2014-09-19 09:04:32

+0

你可以嘗試使用,smtp.EnableSsl = false; – mrsrizan 2014-09-19 09:07:50

+0

仍然無效 – Badar 2014-09-19 09:08:57