如何使用MailKit发送电子邮件? [英] How to send email by using MailKit?

查看:1544
本文介绍了如何使用MailKit发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据新的Google政策, https: //googleonlinesecurity.blogspot.de/2014/04/new-security-measures-will-affect-older.html 我无法发送电子邮件。 安全性较低的应用程序适用于不使用OAuth 2.0的应用程序。

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" are considered for google the application which don't use OAuth 2.0.

我想使用MailKit解决此问题。

I would like to use MailKit to solve this problem

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

但是我得到了类型为'MailKit .Security.AuthenticationException'发生在MailKit.dll中,但未在用户代码中处理。附加信息:身份验证失败。

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

I don't want to change my security settings. Because I want that everything will be secure. That's why I start to use MailKit rather than System.Net.Mail

我该如何解决它?

推荐答案

您需要做的第一件事是遵循 Google的说明获取OAuth 2.0凭证。

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

完成此操作后,获取访问令牌的最简单方法是使用Google的 Google.Apis.Auth library:

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 fiel '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,就好像它是密码一样:

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

这篇关于如何使用MailKit发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆