2015-11-03 23 views
10

Yeni google politikalarına göre https://googleonlinesecurity.blogspot.de/2014/04/new-security-measures-will-affect-older.html Bir e-posta gönderemiyorum. OAuth 2.0 kullanmayan uygulama için google için "Daha az güvenli uygulamalar" düşünülüyor.MailKit kullanarak e-posta nasıl gönderilir?

Bu sorunu

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

çözmek için MailKit kullanmak istiyorum Ama An exception of type 'MailKit.Security.AuthenticationException' occurred in MailKit.dll but was not handled in user code.Additional information: Authentication failed.

benim güvenlik ayarlarını değiştirmek istemiyorsanız var. Çünkü ben her şeyin güvende olmasını istiyorum. Bu yüzden MailKit'i System.Net.Mail

yerine kullanmaya başladım. Bunu nasıl giderebilirim?

+1

Bu, posta seti veya C# ile ilgili değil, Google ile ilgili. Ya yanlış şifreye sahipsiniz ya da hesabın güvenlik ayarlarını değiştirmelisiniz [burada gösterildiği gibi] (http://stackoverflow.com/questions/31231033/using-smtpclient-to-send-an-email-from-gmail –

+1

Bunun nedeni, Oauth 2.0 ile bağlantı kurmanız gerektiğine inanıyorum https://github.com/jstedfast/MailKit/blob/df7b0f5b9522ed355aa49cfbe56892031d65047f/FAQ.md#how-can-i-log-in-to-a- gmail-account-using-oauth-20 –

+0

Aslında, GMail'in bir parola doğru görünse bile bir kimlik doğrulama hatası döndürmesi için birden fazla neden vardır: bir kez parolalar, iki faktörlü kimlik doğrulama [burada açıklandığı gibi] (https: // destek. google.com/accounts/answer/6009563) –

cevap

9

Yapmanız gereken ilk şey, uygulamanız için OAuth 2.0 kimlik bilgilerini almak üzere Google's instructions numaralı telefonu takip etmektir. Bunu yaptıktan sonra

, bir erişim belirteci elde etmek kolay yolu Google'ın Google.Apis.Auth kütüphane kullanmaktır: Artık bir erişim belirteci (credential.Token.AccessToken) sahip olduğunu

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 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 

, sen MailKit ile kullanabilirsiniz sanki şifre vardı:

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

"Hayır gmail" hesabına posta gönderebilir miyim? – Anatoly

+0

Google'ın OAuth2 jetonlarını GMail olmayan sunucularla kullanamazsınız. Sorduğun bu mu? – jstedfast

+0

Örneğin "no_gmail @ yandex.ru" adresine bir e-posta göndermek için ne yapmalıyım? – Anatoly

17

test aşağıdaki kod ve benim için çalışıyor:

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

Yeni bir cevap oluşturmak yerine bu cevaba bir yorum eklemelisiniz. –

+0

Maalesef, başka bir cevaba yorum ekleyemedim. – Felix

+3

Bunu denedim ve hala bir sertifika hatası alıyorum, bu sorunu yaşayan başka biri var mı? –

İlgili konular