繁体   English   中英

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

[英]Unable to connect to remote server while sending email in asp.net

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

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被点亮,防火墙阻塞也没有。

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             

    }

这段代码适合我。试试这个。

  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);

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

我使用以下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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM