Lee
Lee

Reputation: 8734

Sending emails with Gmail and C#/VB.Net no longer works

When I try to send an email with Gmail using VB.Net or C# I keep getting the following message: Failure sending email - An attempt was made to access a socket in a way forbidden by its access permissions - Unable to access remote server. I have tried using several Gmail accounts including VB.Net code that worked in the past, like the following:

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("[email protected]", "password")
SMTPServer.Send(Message)

(I am aware that the web.config can be used for a lot of the above).

Obviously Gmail must have changed some setting or something similar?

Upvotes: 2

Views: 5860

Answers (4)

Christian Haugland
Christian Haugland

Reputation: 11

google have new routines for securing your gmail accounts, to use this code you must go to gmail settings and turn off the blocking of less secure clients.

Upvotes: 1

Lee
Lee

Reputation: 8734

It was McAfee Anti-Virus preventing the sending of emails. Thanks for all the help, and sorry for wasting everyones time.

Upvotes: 4

Jashwant
Jashwant

Reputation: 28995

This code works fine for me:

try
     {
        MailMessage mail = new MailMessage();     //using System.Net.Mail namespace
        mail.To.Add("[email protected]");             //Enter reciever's email address
        mail.From = new MailAddress("[email protected]");  //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("[email protected]", "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);
    }

If this doesn't help, try it on another machine. Windows 7 has its own firewall. Do check it out too.

Upvotes: 3

Legendary Lambe
Legendary Lambe

Reputation: 163

I dont know how to comment so: I think you forgot the port and a few things. See if this helps:

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("[email protected]")
  mail.From = New MailAddress("[email protected]")
  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
       ("[email protected]","password")
  smtp.EnableSsl = True
  smtp.Port = 587
  smtp.EnableSsl = true
  smtp.Send(mail)
End Sub

Try this edit

Upvotes: 1

Related Questions