使用Gmail和C#/ VB.Net发送电子邮件不再有效

时间:2012-01-28 10:20:43

标签: vb.net email gmail

当我尝试使用VB.Net或C#发送带有Gmail的电子邮件时,我不断收到以下消息:发送电子邮件失败 - 尝试以其访问权限禁止的方式访问套接字 - 无法访问远程服务器。 我尝试过使用多个Gmail帐户,包括过去有效的VB.Net代码,如下所示:

Message = New MailMessage(Sender, Recipient, Subject, MessageBody)

SMTPServer = New SmtpClient("smtp.gmail.com", 587)'Port 465 fails as well
SMTPServer.EnableSsl = True

SMTPServer.Credentials = New NetworkCredential("Username@gmail.com", "password")
SMTPServer.Send(Message)

(我知道web.config可以用于上面的很多内容。)

显然Gmail必须更改某些设置或类似内容?

4 个答案:

答案 0 :(得分:4)

McAfee Anti-Virus阻止了电子邮件的发送。感谢所有的帮助,抱歉浪费了每个人的时间。

答案 1 :(得分:3)

此代码适用于我:

try
     {
        MailMessage mail = new MailMessage();     //using System.Net.Mail namespace
        mail.To.Add("xyz@yahoo.com");             //Enter reciever's email address
        mail.From = new MailAddress("abc@gmail.com");  //Enter sender's email address
        mail.Subject = "Testing mail...";
        mail.Body = @"Lets-code ! Lets-code to make it simpler";
        mail.IsBodyHtml = true;                  //Body of mail supports html tags
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "pwd");
        // Your gmail username and password   
        smtp.EnableSsl = true;            //Gmail uses a encrypted connection
        smtp.Send(mail);
        Response.Write("Mail Sent Successfully");
    }

catch(Exception ex)
    {  
        Response.Write(ex.Message);
    }

如果这没有用,请在另一台机器上试试。 Windows 7有自己的防火墙。也要检查一下。

答案 2 :(得分:1)

我不知道如何评论:我认为你忘记了端口和一些东西。看看这是否有帮助:

Imports System.Net.Mail

Protected Sub btnSendEmail_Click(ByVal sender As Object, ByVal e As EventArgs)
  Dim mail As MailMessage =  New MailMessage() 
  mail.To.Add("receiversmail@gmail.com")
  mail.From = New MailAddress("yourmail@gmail.com")
  mail.Subject = "Email using Gmail"

  String Body = "Sending mail using Gmail's SMTP"
  mail.Body = Body

  mail.IsBodyHtml = True
  Dim smtp As SmtpClient =  New SmtpClient() 
  smtp.Host = "smtp.gmail.com" 
  smtp.Credentials = New System.Net.NetworkCredential
       ("yourmail@gmail.com","password")
  smtp.EnableSsl = True
  smtp.Port = 587
  smtp.EnableSsl = true
  smtp.Send(mail)
End Sub

尝试此修改

答案 3 :(得分:1)

谷歌拥有保护您的Gmail帐户的新例程,要使用此代码,您必须访问gmail设置并关闭阻止安全性较低的客户端。