无法通过我的asp.net应用程序发送电子邮件

时间:2015-03-30 10:16:17

标签: c# asp.net email timeout gmail

我收到超时错误。

  

我无法使用此代码发送电子邮件。我几乎尝试过每一次   可能的方式,但没有用。

try
        {
            string myString = "Hello I am new email message. I am delivered for testing.";
            MailMessage mm = new MailMessage("XXXXXXXXXX", "XXXXXXXXXX");
            //mm.CC.Add("XXXXXXXXXX");
            mm.Subject = "Subject have been successfully placed.";
            mm.Body = myString.ToString();
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;

            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = "XXXXXXXXXX";
            NetworkCred.Password = "XXXXXXXXXX";
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 465;
            smtp.Send(mm);
            Request.Cookies.Clear();
        }
        catch (Exception ee)
        {
            Response.Write(ee.Message);
        }

1 个答案:

答案 0 :(得分:1)

试试这个

private void SendMail()
{
MailMessage mail = new MailMessage();
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);

mail.From = new MailAddress("Setfromaddress");
mail.To.Add(new MailAddress("recepient@gmail.com"));
mail.Subject = "Test";
mail.Body = "This is a test";

mailClient.EnableSsl = true;
mailClient.Credentials = new NetworkCredential("Username", "Password");
try
{
    mailClient.Send(mail);
}
catch(Exception ex)
{
    WriteErrorOutput(ex.Message);
}
}

我认为我们还需要检查连接

要在计算机上的Windows上运行telnet并进行测试:

1.Open the Start menu, and select Run.
2.Enter command in the Open: field, and click OK.
3.Enter 'telnet smtp.gmail.com 465,' and hit Enter, or enter 'telnet smtp.gmail.com 587' instead.