0

I am trying to send email, but I receive the following error:

An attempt was made to access a socket in a way forbidden by its access permissions 72.52.4.90:587

Code:

var mail = new MailMessage();
var client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("myemail@gmail.com", "password");

mail.From = new MailAddress("myemail@gmail.com");
mail.To.Add(new MailAddress("myemail@gmail.com"));
mail.BodyEncoding = UTF8Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; 
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

NOTE: This code is based on other questions and answers already here in StackOverflow. However, after an extensive search, it appears that no one else has experienced this particular issue so far. According to the other Q&A this code should be working.


UPDATE: After being told that this was a duplicate I've modified my code to the following, however, I'm still getting the same error.

    private static void Main(string[] args)
    {
        var From = new MailAddress("myemail@gmail.com", "Me");
        var To = new MailAddress("myemail@gmail.com", "Me Too");
        var client = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587, //25
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(From.Address, "password"),
            Timeout = 20000
        };

        using (var mail = new MailMessage(From, To)
        {
            Subject = @"this is a test email.",
            Body = @"this is my test email body"
        })
        {
            client.Send(mail);
        };
    }
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
BermudaLamb
  • 273
  • 3
  • 5
  • 24
  • 3
    Is that supposed to be `smtp.googlemail.com` or `smtp.gooflemail.com` like you have? Also I really think it is `smtp.gmail.com`. – Greg May 15 '14 at 21:41
  • Your hostname is different to what I would have thought. This article gives some good information: https://www.digitalocean.com/community/articles/how-to-use-google-s-smtp-server – Jane S May 15 '14 at 21:42
  • Yes, it should be smtp.googlemail.com. And yes, I tried both smtp.gmail.com and smtp.googlemail.com. Neither work, and both return the error message above. – BermudaLamb May 16 '14 at 16:40
  • This does not appear to be a duplicate because it is about a failure trying to send email and not about how to send the email in the first place. – JasonMArcher May 16 '14 at 17:05

0 Answers0