Gemäß der neuen Google-Richtlinie https://googleonlinesecurity.blogspot.de/2014/04/new-security-measures-will-affect-older.html Ich kann keine E-Mail senden. "Weniger sichere Apps" werden für die Google-Anwendung berücksichtigt, die nicht OAuth 2.0.
Ich möchte MailKit verwenden, um dieses Problem zu lösen
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);
}
Aber ich habe An exception of type 'MailKit.Security.AuthenticationException' occurred in MailKit.dll but was not handled in user code.Additional information: Authentication failed.
Ich möchte meine Sicherheitseinstellungen nicht ändern. Weil ich will, dass alles sicher ist. Deshalb verwende ich MailKit anstelle von System.Net.Mail
Wie kann ich es reparieren?
Als Erstes müssen Sie Anweisungen von Google befolgen, um OAuth 2.0-Anmeldeinformationen für Ihre Anwendung zu erhalten.
Sobald Sie dies getan haben, können Sie ein Zugriffstoken am einfachsten mit der Google.Apis.Auth -Bibliothek von Google erhalten:
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
Jetzt, da Sie ein Zugriffstoken haben (credential.Token.AccessToken
) können Sie es mit MailKit verwenden, als wäre es das Passwort:
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);
}
Hat folgenden Code getestet und funktioniert bei mir:
// 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);
}