0

I have a serious issue in sending email in C#. This is the code:

SmtpClient newSmpt = new SmtpClient()
    {
        Host = "smtp.googlemail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        Credentials = new NetworkCredential("mail@gmail.com", "password"),
        Timeout = 30000
    }

string body = 'html body';
String bodyEmail = email.Replace("@", "<span>@</span>");
bodyEmail = bodyEmail.Replace(".", "<span>.</span>");
body = body.Replace("<%email%>", bodyEmail);
body = body.Replace("<%name%>", name);
body = body.Replace("<%name%>", name);

MailMessage mailMessageMox = new MailMessage("mail@gmail.com", "info@mail.it", "Title", body);
mailMessageMox.IsBodyHtml = true;
newSmpt.Send(mailMessageMox);

With this code I managed to send mail correctly. Sometimes I catch an exception from the Send method with this message: "the smtp server requires a secure connection or the client was not authenticated". As you can see, the EnableSsl property is set to true, as told in other posts. When I go to gmail and try to login with the credentials used in the code, it asks me to enter a captcha code. After entering the captcha, I can login and then the code works fine. The problem is that this can happen too many times.

How to block captcha asking in gmail?

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Nicola Montini
  • 35
  • 1
  • 10

2 Answers2

1

Unlocking captcha for the account you are using should solve your problem:

http://www.google.com/accounts/DisplayUnlockCaptcha

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
  • Unfortunately this did not do the trick. I clicked on the link and used the account from the .NET web service within 10 minutes, but after two days it went blocked again. – Nicola Montini Jul 01 '13 at 07:38
0

I don't think your problem is with sending an email through the SmtpClient. It should work just fine

Check out this Question, for all the settings that is needed to send from Gmail via SmtpClient

Sending email in .NET through Gmail

There should be a configuration in your Gmail account that will enable you to send emails without the need to verify your credentials using captcha.

Community
  • 1
  • 1
sameh.q
  • 1,691
  • 2
  • 23
  • 48