2015-11-03 10 views
10

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

हल करने के लिए MailKit उपयोग करना चाहते हैं लेकिन मैं 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

के बजाय मेलकिट का उपयोग करना शुरू कर रहा हूं, मैं इसे कैसे ठीक कर सकता हूं?

+1

यह मेलकिट या सी # के बारे में नहीं है, यह Google के बारे में है। या तो आपके पास गलत पासवर्ड है या आपको खाते की सुरक्षा सेटिंग्स को बदलने की जरूरत है [जैसा कि यहां दिखाया गया है] (http://stackoverflow.com/questions/31231033/using-smtpclient-to-send-an-email-from-gmail) –

+1

मेरा मानना ​​है कि ऐसा इसलिए है क्योंकि आपको OAuth 2.0 के साथ कनेक्शन सेट करने की आवश्यकता है https://github.com/jstedfast/MailKit/blob/df7b0f5b9522ed355aa49cfbe56892031d65047f/FAQ.md#how-can-i-log-in-to-a- जीमेल-खाता-उपयोग-ओथ -20 –

+0

असल में, कई कारण हैं कि जीमेल एक प्रमाणीकरण त्रुटि वापस कर सकता है भले ही पासवर्ड सही दिखाई देता है - एक बार पासवर्ड, दो-कारक प्रमाणीकरण [जैसा कि यहां वर्णित है] (https: // समर्थन। google.com/accounts/answer/6009563) –

उत्तर

9

सबसे पहले आपको जो करना है वह आपके आवेदन के लिए OAuth 2.0 प्रमाण-पत्र प्राप्त करने के लिए Google's instructions का पालन करना है।

एक बार जब आप किया है कि, पहुंच टोकन प्राप्त करने के लिए सबसे आसान तरीका है गूगल के Google.Apis.Auth लाइब्रेरी का उपयोग करने के लिए है: अब आप पहुंच टोकन (credential.Token.AccessToken) है

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 

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

क्या मैं "नो जीमेल" खाते में मेल भेज सकता हूं? – Anatoly

+0

आप गैर-जीमेल सर्वर के साथ Google के OAuth2 टोकन का उपयोग नहीं कर सकते हैं। क्या आप यही पूछ रहे हैं? – jstedfast

+0

उदाहरण के लिए 'no_gmail @ yandex.ru' पर ईमेल भेजने के लिए मुझे क्या करना चाहिए? – Anatoly

17

परीक्षण निम्नलिखित कोड और मेरे लिए काम करता है:

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

आपको एक नया उत्तर बनाने के बजाय उस उत्तर में एक टिप्पणी जोड़नी चाहिए। –

+0

इसके बारे में क्षमा करें, मैं किसी अन्य उत्तर में टिप्पणी जोड़ने में सक्षम नहीं था। – Felix

+3

मैंने यह कोशिश की है और मुझे अभी भी एक प्रमाणपत्र त्रुटि मिली है, क्या कोई और है जिसके पास यह समस्या है? –

संबंधित मुद्दे