新しいGoogleポリティクスによると https://googleonlinesecurity.blogspot.de/2014/04/new-security-measures-will-affect-older.html メールを送信できません。 「安全性の低いアプリ」は、GoogleでOAuth 2.0。
MailKitを使用してこの問題を解決したい
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Joey Tribbiani", "[email protected]"));
message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "[email protected]"));
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("[email protected]", "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.
セキュリティ設定を変更したくありません。私はすべてが安全であることを望んでいるからです。だから、System.Net.MailではなくMailKitを使い始めた
どうすれば修正できますか?
最初に行う必要があるのは、 Googleの指示 を取得することですOAuthアプリケーションの2.0認証情報。
それが完了したら、アクセストークンを取得する最も簡単な方法は、Googleの Google.Apis.Auth ライブラリを使用することです。
var certificate = new X509Certificate2 (@"C:\path\to\certificate.p12", "password", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential (new ServiceAccountCredential
.Initializer ("[email protected]") {
// Note: other scopes can be found here: https://developers.google.com/gmail/api/auth/scopes
Scopes = new[] { "https://mail.google.com/" },
User = "[email protected]"
}.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 ("[email protected]", credential.Token.AccessToken);
client.Authenticate (oauth2);
client.Send (message);
client.Disconnect (true);
}
次のコードをテストし、私のために動作します:
// 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", "[email protected]"));
message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "[email protected]"));
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);
}