在asp.net中发送电子邮件时无法连接到远程服务器

时间:2012-10-30 07:08:10

标签: c# asp.net smtpclient

使用电子邮件发送代码我的代码时出现上述错误

string fromAddress = "mymail";
string fromPassword = "mypassword";
var smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
smtp.Send(fromAddress, toAddress, MailSubject, Body);

我google了很多次但没有得到适当的解决方案。 端口587被点亮,防火墙阻塞也没有。

4 个答案:

答案 0 :(得分:1)

try
   {
      MailMessage mail = new MailMessage();
      mail.To.Add("sender id");
      mail.From = new MailAddress("your id");   
      mail.Subject = "Mail from my web page";
      mail.Body ="Body Content";
      mail.IsBodyHtml = true;
      SmtpClient smtp = new SmtpClient();
      smtp.Host = "smtp.gmail.com";
      //Or your Smtp Email ID and Password  
      smtp.Credentials = new System.Net.NetworkCredential
      ("XYZ", "XXXXX");
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
      smtp.EnableSsl = true;
      smtp.Send(mail);
   }


catch (Exception ex)
    {
        //display exception             

    }

答案 1 :(得分:1)

此代码适用于我。试试这个。

  MailMessage mM = new MailMessage();
        mM.From = new MailAddress("YourGmail@gmail.com");
        mM.To.Add(Email);
        mM.Subject = "Your Sub";
        mM.Body = "Your Body" ;
        mM.IsBodyHtml = true;
        mM.Priority = MailPriority.High;
        SmtpClient sC = new SmtpClient("smtp.gmail.com");
        sC.Port = 587;
        sC.Credentials = new NetworkCredential("YourGmail", "YourPassword");
        //sC.EnableSsl = true;
        sC.EnableSsl = true;
        sC.Send(mM);

答案 2 :(得分:1)

感谢您的回复。我得到了解决方案 如果您使用的是任何防病毒软件,请检查它的日志,看是否是因为防病毒软件。当McAffee阻止我的邮件时,我遇到了同样的问题(有一个安全政策 - 防止群发邮件蠕虫发送邮件)。编辑此策略并将您的应用程序添加到例外列表中。在我的情况下,这排序问题。请检查它是否适合您。

答案 3 :(得分:0)

我使用以下Gmail代码:

Function SendMail_Gmail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String) As Boolean

        Dim mailmsg As New System.Net.Mail.MailMessage()    
        mailmsg.From = New MailAddress(strFrom)    
        mailmsg.To.Add(strTo)    
        mailmsg.Subject = strSubject    
        mailmsg.IsBodyHtml = True    
        mailmsg.Body = strBody    
        mailmsg.Priority = System.Net.Mail.MailPriority.Normal

        Dim client As New System.Net.Mail.SmtpClient()

        client.Host = "smtp.gmail.com"
        client.Port = "587"    
        client.Credentials = New System.Net.NetworkCredential("youremailid@gmail.com", "Yourpassword")

       client.EnableSsl = True

        Dim userstate As Object = mailmsg   


        client.Send(mailmsg)
        Return True
    End Function