-2

I'm trying to send a mail with my gmailaccount from a server with the following code.

            var fromAddress = new MailAddress("xxxxx@gmail.com", "xxxxxx");
        var toAddress = new MailAddress(toMailAddress);
        const string fromPassword = "xxxxx";

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            IsBodyHtml = true,
            Subject = mailSubject,
            Body = htmlBody
        })
        {
            smtp.Send(message);
        }

This gives the next result when I deploy it on my windows server although I have an Inobound Rule for port 587 on the firewall. It works perfect when I test it local

connectionFailed

Lieven
  • 41
  • 4
  • 1
    take a look at this question also learn to use the debugger to step through your code - [Sending Mail to Gmail Account using C#](https://stackoverflow.com/questions/29251189/sending-mail-using-gmail-smtp-from-csharp-net) – MethodMan Jul 17 '17 at 19:13
  • Possible duplicate of [Sending email through Gmail SMTP server with C#](https://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – S.Dav Jul 17 '17 at 20:21
  • Per OPs own comment on the strangely accepted answer below, I'm voting to close this as too localised, because it was solved by the OPs hosting company opening a port in the firewall. – J. Steen Jul 21 '17 at 08:29

1 Answers1

-1

I too have faced this problem. Actually Gmail does not allow to access gmail for sending mails for less secure apps. Perform the following steps: if you are getting exception like

"An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional information: The SMTP server requires a secure connection or the 
client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
  1. Go to your google account "My Account".
  2. under "Sign-in & security" go to the bottom, you will find "Allow less secure apps: OFF", you need to turn it on by clicking the button next to it.
  3. Now your gmail account will be accessible from your code.
Pranav Mishra
  • 456
  • 2
  • 7
  • 14