How to send email by using MailKit?

前端 未结 2 1456
长情又很酷
长情又很酷 2020-12-04 16:48

According to the new google politics https://googleonlinesecurity.blogspot.de/2014/04/new-security-measures-will-affect-older.html I can\'t sent an email. \"Less secure apps

相关标签:
2条回答
  • 2020-12-04 17:32

    Tested following code and works for me:

            // STEP 1: Navigate to this page https://www.google.com/settings/security/lesssecureapps & set to "Turn On"
    
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("Joey Tribbiani", "YOU_FROM_ADDRESS@gmail.com"));
            message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "YOU_TO_ADDRESS@gmail.com"));
            message.Subject = "How you doin'?";
    
            message.Body = new TextPart("plain")
            {
                Text = @"Hey Chandler,I just wanted to let you know that Monica and I were going to go play some paintball, you in?-- Joey"
            };
    
            using (var client = new SmtpClient())
            {
                client.Connect("smtp.gmail.com", 587);
    
    
                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");
    
                // Note: only needed if the SMTP server requires authentication
                client.Authenticate("YOUR_GMAIL_NAME", "YOUR_PASSWORD");
    
                client.Send(message);
                client.Disconnect(true);
            }
    
    0 讨论(0)
  • 2020-12-04 17:34

    The first thing you need to do is follow Google's instructions for obtaining OAuth 2.0 credentials for your application.

    Once you've done that, the easiest way to obtain an access token is to use Google's Google.Apis.Auth library:

    var certificate = new X509Certificate2 (@"C:\path\to\certificate.p12", "password", X509KeyStorageFlags.Exportable);
    var credential = new ServiceAccountCredential (new ServiceAccountCredential
        .Initializer ("your-developer-id@developer.gserviceaccount.com") {
        // Note: other scopes can be found here: https://developers.google.com/gmail/api/auth/scopes
        Scopes = new[] { "https://mail.google.com/" },
        User = "username@gmail.com"
    }.FromCertificate (certificate));
    
    //You can also use FromPrivateKey(privateKey) where privateKey
    // is the value of the field 'private_key' in your serviceName.json file
    
    bool result = await credential.RequestAccessTokenAsync (cancel.Token);
    
    // Note: result will be true if the access token was received successfully
    

    Now that you have an access token (credential.Token.AccessToken), you can use it with MailKit as if it were the password:

    using (var client = new SmtpClient ()) {
       client.Connect ("smtp.gmail.com", 587);
    
       // use the OAuth2.0 access token obtained above
       var oauth2 = new SaslMechanismOAuth2 ("mymail@gmail.com", credential.Token.AccessToken);
       client.Authenticate (oauth2);
    
       client.Send (message);
       client.Disconnect (true);
    }
    

    Update:

    The above solution is for what Google refers to as "Service Accounts" that are used for server-to-server communication, but if you want OAuth2 support for standard Phone or Desktop apps, for example, you'll need to follow the directions I've written here: https://github.com/jstedfast/MailKit/blob/master/GMailOAuth2.md

    0 讨论(0)
提交回复
热议问题