1

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

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

我用谷歌搜索了很多次,但没有得到正确的解决方案。端口 587 已启用,防火墙也没有阻止。

4

4 回答 4

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             

    }
于 2012-10-30T07:18:38.397 回答
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);
于 2012-10-30T07:25:44.780 回答
1

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

于 2012-10-30T08:52:30.363 回答
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
于 2012-10-30T07:21:04.333 回答