【问题标题】:Sending emails with Gmail and C#/VB.Net no longer works使用 Gmail 和 C#/VB.Net 发送电子邮件不再有效
【发布时间】:2015-05-24 23:23:55
【问题描述】:

当我尝试使用 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 一定更改了某些设置或类似的东西?

【问题讨论】:

  • 关闭防火墙再试一次。
  • 我尝试关闭防火墙,但没有任何影响。

标签: vb.net email gmail


【解决方案1】:

是 McAfee Anti-Virus 阻止了电子邮件的发送。感谢大家的帮助,也很抱歉浪费大家的时间。

【讨论】:

  • ;) 我对你的问题的第一条评论
【解决方案2】:

这段代码对我来说很好用:

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 有自己的防火墙。也请检查一下。

【讨论】:

  • 我在禁用防火墙的情况下也尝试了您的代码,但仍然收到相同的错误消息,这似乎是特定于机器的问题:(
【解决方案3】:

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

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

试试这个编辑

【讨论】:

  • 我尝试了你的代码,我得到了与以前完全相同的异常,以及“无法连接到远程服务器”。上次我使用 Gmail 发送电子邮件时,我使用的是 Vista Home Premium x32,而现在我使用的是 Windows 7 Ultimate x64,除此之外我想不出有什么不同。
  • 您可以在此处查看我运行您的代码以及我自己的代码时的完整错误消息:pastebin.com/SRKQuMti
【解决方案4】:

Google 提供了保护您的 gmail 帐户的新例程,要使用此代码,您必须转到 gmail 设置并关闭对不太安全的客户端的阻止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 2015-10-24
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    相关资源
    最近更新 更多