0

I'm trying to send an email via C# but I keep on getting this error:

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...

The stack trace can be found here:

System.Net.Mail.SmtpException: 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

at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)

at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)

at System.Net.Mail.SmtpClient.Send(MailMessage message)

at Repetrel.View.EmailForm.btnSend_Click(Object sender, EventArgs e) in c:\2015 Hyrel Projects\Repetrel\src\Repetrel\View\EmailForm.cs:line 238

Here is my code:

            NetworkCredential loginInfo = new NetworkCredential(Properties.Settings.Default.MyEmailAddress, Properties.Settings.Default.MyEmailPassword);
            //System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(Properties.Settings.Default.SMTPAddress);
            ////Trace.WriteLine("smtp = " + Properties.Settings.Default.SMTPAddress);
            //smtpClient.Port = 587;
            //smtpClient.EnableSsl = true;
            //smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            ////smtpClient.UseDefaultCredentials = false;
            //smtpClient.Credentials = loginInfo;

            var smtpClient = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = true,
                Credentials = loginInfo,
                Timeout = 20000
            };

            //smtpClient.SendAsync(message, "test");
            smtpClient.Send(message);

            //Output to log
            OutputToLog(message);

            //Close Window
            Close();
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.ToString());
            MessageBox.Show("An error occurred.  Please see the trace log for more information.");
        }

I have tried everything mentioned in previous posts(there are many of them), but nothing seems to work. I have tried with and without default credentials and setting the flag to false/true. Nothing seems to work. Might anyone be able to point me in the right direction?

Thanks in advance!

Community
  • 1
  • 1
Dan Gifford
  • 886
  • 1
  • 9
  • 33
  • It's unfortunate you stopped your quote at `Learn more at...`. Have you tried to learn more at that location? Can you disclose that location? – Frédéric Hamidi Jul 01 '15 at 19:17
  • The quote gets cutoff in the error on my machine. You are seeing everything I see on my machine. – Dan Gifford Jul 01 '15 at 19:21
  • Ah, your trace parameters are not very tolerant it seems -- perhaps you could dump `ex.ToString()` to a file, and read that file as well as the links it contains? You can always get back to us after you have exhausted the solutions proposed there. – Frédéric Hamidi Jul 01 '15 at 19:26
  • I just posted the ex.ToString() contents. – Dan Gifford Jul 01 '15 at 19:33
  • Excellent. So the message does end at *Learn more at*. How useless. Anyway, if `UseDefaultCredentials = false` did not fix the problem, now is the time to check the network. Good luck. *(Now that I think of it, have you double-checked `Properties.Settings.Default.MyEmailAddress` and `Properties.Settings.Default.MyEmailPassword` are what you think they are? Time to fire up the good old debugger.)* – Frédéric Hamidi Jul 01 '15 at 19:39
  • I send mail through Gmail in a very similar method in one of my projects without issue, and after comparing your code to what I did, the only difference I can see is that I set my code to use port 25 instead of port 587. I did use `UseDefaultCredentials = false;` and pass in my google credentials like you did. – mituw16 Jul 01 '15 at 19:50
  • @FrédéricHamidi - I checked the inputs, they are correct. Any other thoughts? – Dan Gifford Jul 01 '15 at 19:59
  • @mituw16 - I tried port 25 both with and without SSL. I got the same error except when I used no SSL I got back: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. i6sm3059064ywd.47 - gsmtp – Dan Gifford Jul 01 '15 at 20:03
  • @Dan, talk to the network admins on both sides (especially yours)? Seriously, I would try my code on another network (e.g. at home). If it fails, it's me. If it succeeds, I have to report that to our sysadm and ask them what they're seeing from their side. – Frédéric Hamidi Jul 01 '15 at 20:03
  • @DanGifford Did you set `UseDefaultCredentials = false` after switching to port 25? – mituw16 Jul 01 '15 at 20:07
  • 1
    @DanGifford I don't know if this will help you any, but here is a pastebin from one of my projects that sends email through gmail without issue. http://pastebin.com/DaqY911k Frederic might be right, I would also suggest trying this from a different network. – mituw16 Jul 01 '15 at 20:11
  • @mituw16 - The link didn't help. Thanks though. I'm going to try this from a different network later this evening and report back. – Dan Gifford Jul 01 '15 at 20:20
  • @Frederic & mituw16 - I found a solution and posted it. I can't say its the answer until tomorrow. Thanks again for all the help. – Dan Gifford Jul 02 '15 at 16:55

2 Answers2

1

Apparently GMAIL was blocking my email address from using less secure devices. You can turn off the blocking at: https://www.google.com/settings/security/lesssecureapps.

Dan Gifford
  • 886
  • 1
  • 9
  • 33
0

Perhaps UseDefaultCredentials = false, instead?

Edit: This seems similar to an older StackOverflow question: SmtpClient with Gmail

Community
  • 1
  • 1