使用ASP.NET发送电子邮件

时间:2014-09-19 08:49:20

标签: asp.net email

我正在尝试使用ASP.NET通过Gmail服务器发送电子邮件。这是我的代码,你能帮我解决一下超时问题吗?

        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("FromEmail@gmail.com");
            mail.To.Add("ToEmail@gmail.com");
            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@gmail.com", "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 个答案:

答案 0 :(得分:0)

尝试使用其他端口号。 Gmail在发送邮件时会接受端口25或587,但使用端口465会超时。

同时确保您也使用UseDefaultCredentials = False。