Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can send emails through Gmail account only if account has "Access for less secure apps" enabled

If my Gmail account has Access for less secure apps disabled, then my application can't send emails through this account. Instead I get "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required" exception.

Here Google explains that by disabling Access for less secure apps, only apps that use modern security standards can sign in.

What are those modern security standards my code needs to implement and can you show me how to implement them with an example ( not sure if it matters, but my app and Gmail account aren't using 2-step verification )?

Here's the code I'm currently using:

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        var credentialUserName = "[email protected]";
        var sentFrom = "[email protected]";
        var pwd = "myPwd";

        System.Net.Mail.SmtpClient client = 
            new System.Net.Mail.SmtpClient("smtp.gmail.com");

        client.Port = 587;
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;

        System.Net.NetworkCredential credentials = 
            new System.Net.NetworkCredential(credentialUserName, pwd);

        client.EnableSsl = true;
        client.Credentials = credentials;

        var mail = 
            new System.Net.Mail.MailMessage(sentFrom, message.Destination);

        mail.Subject = message.Subject;
        mail.Body = message.Body;

        return client.SendMailAsync(mail);
    }
}
like image 563
bckpwrld Avatar asked Aug 25 '14 17:08

bckpwrld


1 Answers

Considering that the asp.net-identity-2 tag is applied to this question, and considering that Google requires use of OAuth 2.0 to avoid having to use the Access for less secure applications option, it appears that one option is to use the OWIN middleware able to be found by searching for the term Oauth 2.0 at www.asp.net.

This site hosts an article titled Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on that might be of some interest. The article appears to show many screenshots that walk a developer through the process of getting the resource, creating an app, and authenticating with a Google server.

like image 157
kbulgrien Avatar answered Oct 27 '22 00:10

kbulgrien