如何使用MailKit发送电子邮件

本文关键字:电子邮件 MailKit 何使用 | 更新日期: 2023-09-27 18:20:24

根据新的谷歌政治https://googleonlinesecurity.blogspot.de/2014/04/new-security-measures-will-affect-older.html我无法发送电子邮件。不使用OAuth 2.0的谷歌应用程序被认为是"不太安全的应用程序"。

我想使用MailKit来解决这个问题

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Joey Tribbiani", "noreply@localhost.com"));
message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "mymail@gmail.com"));
message.Subject = "How you doin'?";
message.Body = new TextPart("plain"){ Text = @"Hey" };
using (var client = new SmtpClient())
{
   client.Connect("smtp.gmail.com", 587);
   ////Note: only needed if the SMTP server requires authentication
   client.Authenticate("mymail@gmail.com", "mypassword");
   client.Send(message);
   client.Disconnect(true);
}

但我有An exception of type 'MailKit.Security.AuthenticationException' occurred in MailKit.dll but was not handled in user code.Additional information: Authentication failed.

我不想更改我的安全设置。因为我希望一切都安全。这就是为什么我开始使用MailKit而不是System.Net.Mail

我该怎么修?

如何使用MailKit发送电子邮件

您需要做的第一件事是按照Google的说明为您的应用程序获取OAuth 2.0凭据。

一旦你做到了,获得访问令牌的最简单方法就是使用谷歌的Google.Apis.Auth库:

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

现在您有了访问令牌(credential.Token.AccessToken),您可以将其与MailKit一起使用,就好像它是密码一样:

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);
}

更新:

上面的解决方案适用于谷歌所说的"服务帐户",用于服务器到服务器的通信,但如果你想让OAuth2支持标准的手机或桌面应用程序,你需要遵循我在这里写的指示:https://github.com/jstedfast/MailKit/blob/master/GMailOAuth2.md

测试了以下代码并适用于我:

        // 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);
        }