1

I'm testing a normally working emailing module (.Net 4.5.2, C#) based on the System.Net.Mail.SmtpClient class, to which I have added support for TLS. To do this I'm trying to connect to my personal googlemail.com account using the same smtp settings that I use in my browser. I've set up the SmtpClient object as

SmtpClient mailer = new SmtpClient(); 
mailer.Host = "tls://smtp.gmail.com"; 
mailer.Credentials = new System.Net.NetworkCredential("me@googlemail.com","myGooglemailPassword"); mailer.EnableSsl = true;
mailer.UseDefaultCredentials = true; // although msdn states that this is necessary for TLS, it doesn't actually seeme to make any difference 
mailer.Port = 587; 
var message = new MailMessage();
message.Subject="dontcare"; 
message.Body = "dontcare"
message.To.Add("me@googlemail.com"); 
message.From = new MailAddress("myBusinessEmailaddress@somewhere"); 
mailer.Send(message);

The problem is that this is rejected by gmail with the 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

Given that my gmail account works normally for me otherwise, what am I missing in the .Net implementation?

haughtonomous
  • 4,602
  • 11
  • 34
  • 52
  • Do you have more details about error ? – D4rkTiger Feb 26 '16 at 09:24
  • You might have to allow less secure apps to login to your account, see here: https://support.google.com/accounts/answer/6010255?hl=en – HaukurHaf Feb 26 '16 at 09:28
  • That's literally the entire SmtpException message. – haughtonomous Feb 26 '16 at 09:28
  • Finally, possible duplicate with [this](http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – D4rkTiger Feb 26 '16 at 09:30
  • Could it be because I have two-step authentication switched on? (although I am not getting SMS authentication codes on my phone either) – haughtonomous Feb 26 '16 at 09:30
  • you need to add mailer.EnableSsl = true; before the line "var message = new MailMessage(); – secret squirrel Feb 26 '16 at 09:31
  • He already has that line. – HaukurHaf Feb 26 '16 at 09:37
  • I've temporarily turned off two-step verification, but that hasn't helped. – haughtonomous Feb 26 '16 at 09:40
  • Just wondering - what is the effect of UseDefaultCredentials=true? What are the default credentials? – haughtonomous Feb 26 '16 at 09:42
  • the thing is, when you put mailer.SslEnabled = true and then put mailer.UseDefaultCredentials = true, it overwrites the previous Ssl setting, so you have to put that line after – secret squirrel Feb 26 '16 at 10:02
  • Do you mean I should put mailer.UseDefaultCredentials = true before mailer.SslEnabled = true? – haughtonomous Feb 26 '16 at 10:52
  • @Neil Haughton, yes because obviously if you set a credental value to true and then select the default (which is false), you're going to override the value you just set. The .NET documentation is really poor on this. – secret squirrel Feb 26 '16 at 10:54
  • Thanks. I have it working now without setting UseDefaultCredentials=true, so I'm not sure what this line accomplishes. The original problem was setting the Host property to "tls://smtp.gmail.com" (as documented on the gmail website). When I reduced that to "smtp.gmail.com" it started working. – haughtonomous Feb 26 '16 at 10:56
  • I think "UseDefaultCredentials" pretty much only sets EnableSSL to false, it doesn't do much else ;) – secret squirrel Feb 26 '16 at 10:59
  • I know the protocol actually used is negotiated between client and server, but does anyone know what the highest TLS version is that .Net 4.5.2 supports when using the SmtpClient class to send a message? I'm assuming at least a Windows 7 client machine. – haughtonomous Feb 26 '16 at 11:16

1 Answers1

0

The problem was that by default Googlemail/Gmail blocks email sources that it deems to be 'less secure' (Outlook is an example). You have to turn this off in the Gmail account settings, and then the problem disappears.

haughtonomous
  • 4,602
  • 11
  • 34
  • 52