6

I'm working on an ASP.NET Web Forms app and I'm trying to programmatically send an email to myself. I'm using Gmail's SMTP client, and all is well except that when I send my message, I get this error:

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

If I go into my gmail account settings and enable an option that allows me to allow access for "less secure apps", everything works fine. I'm wondering how I can send my email with having this option enabled.

protected void sendEmail(object sender, EventArgs e)
{
    var client = new SmtpClient("smtp.gmail.com", 587)
    {
        Credentials = new System.Net.NetworkCredential("myusername@gmail.com", "mypassword"),
        DeliveryMethod = SmtpDeliveryMethod.Network,
        EnableSsl = true
    };
    
    MailAddress from = new MailAddress("myusername@gmail.com", "Torchedmuffinz");
    MailAddress to = new MailAddress("myusername@gmail.com", "Torchedmuffinz");
    MailMessage message = new MailMessage(from, to);
    message.Subject = "test";
    message.Body = "test";
    Attachment attachFile = new Attachment(@"pathtofile");
    message.Attachments.Add(attachFile);

    try { client.Send(message); }
    catch (Exception email_exception)
    {
        System.Diagnostics.Debug.WriteLine(email_exception);
    }
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
CodyJamesCasey
  • 159
  • 3
  • 12
  • Unsure what you mean. You are using a system (GMAIL) that has **set requirements** in order for you to use it... – EdSF Aug 07 '14 at 14:36
  • @EdSf I was unsure of what those requirements were. After digging a bit more, I found an article [link](http://www.ghacks.net/2014/07/21/gmail-starts-block-less-secure-apps-enable-access/) that suggests that they switched to "OAuth 2.0" at some point but only recently started enforcing it. Assuming that my problem comes from that, I am now attempting to learn how to make my app comply to that. – CodyJamesCasey Aug 07 '14 at 14:45
  • @Torchedmuffinz - I tried your code and it works for me if I change the port to 25 instead of 587. –  Aug 07 '14 at 14:46
  • @user1 Thank you for the suggestion. I tried that, but it doesn't work for me. I still get the same error. – CodyJamesCasey Aug 07 '14 at 14:49
  • 1
    Have you tried using an email client (like Outlook) and making sure you can successfully send email using it? I only ask because Gmail does have some funny authentication gotchas. That would at least verify if there's an issue with your code, or your account. – Mike Aug 07 '14 at 14:52
  • 1
    @Torchedmuffinz - I poked through my gmail settings and noticed that I have both POP and Imap enabled under the "Forwarding and POP/IMAP" tab. Not sure if this has any effect on allowing my code to send an email. Also try allowing access to applications using this link https://accounts.google.com/DisplayUnlockCaptcha –  Aug 07 '14 at 14:54
  • @Mike Nice suggestion. I tried that and it worked perfectly. So I am fairly confident that my code is okay. I think I can narrow it down being a gmail specific problem. – CodyJamesCasey Aug 07 '14 at 15:00
  • @user1 Definitely some good thoughts. Unfortunately, I too have those same options set and the link didn't work. – CodyJamesCasey Aug 07 '14 at 15:06
  • @Torchedmuffinz Did you find a solution? Please, post an answer –  Jan 18 '16 at 16:25
  • @ivan_petrushenko Hmm, I think I ended up finding a solution to this, I'll try to dig around when I'm not at work and post it soon! – CodyJamesCasey Jan 19 '16 at 18:07

2 Answers2

0

Gmail port 587 does not support SSL.

I think the following code should work for you.

MailMessage msg = new MailMessage();
msg.From=new MailAddress("yourmail@gmail.com");
msg.To.Add("receiver@receiverdomain.com");
msg.Subject="Your Subject";
msg.Body="Message content is going to be here";
msg.IsBodyHtml=false; //if you are going to send an html content, you have to make this true

SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port=587;

NetworkCredential credential=new NetworkCredential("yourmail@gmail.com","your gmail password");
client.UseDefaultCredentials=false;
client.Credentials=credential;
client.Send(msg);
Omer Harmansa
  • 29
  • 1
  • 5
0

There is a possibility to use Google SMTP servers without 'Allow less secure apps' option, but you cannot use your standard google username and password. See my instructions on other post:

Is there a way to use ASP.NET to send email through a Google Apps acccount without selecting the 'Allow less secure apps' option?

Community
  • 1
  • 1
David Najman
  • 487
  • 4
  • 7