2014-09-19 164 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