Can send emails through Gmail account only if acco

2020-07-06 07:00发布

问题:

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 = "myAccount@gmail.com";
        var sentFrom = "myAccount@gmail.com";
        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);
    }
}

回答1:

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.



回答2:

I think it "Less secure" only means that you are giving credentials to third party and they do not use two step verification.

About Google "Less Secure" Settings

It should be noted that Google's statement of Less Secure should not be read as Insecure. Less Secure Apps is a label describing a behavioral issue and not a technical issue. Lots of things can go wrong when you give your credentials to a third party to give to the authentication authority: the third party might keep the credentials in storage without telling you, they might use your credentials for purposes outside the stated scope of the application, they might send your credentials over a network without encryption, etc. Ultimately, it is only Less Secure if the third party in question has malicious intent and therefore you should always be vigilant in knowing who you are sending your credentials to. COMPanion Corp stores your credentials only for the purpose of utilizing Googles SMTP Email service and they are stored using the most up-to-date security.

Source: http://www.goalexandria.com/v7Docs/index.php/Using_Gmail_as_Your_SMTP_Server