System.Net.Mail OAuth या OAuth2 का समर्थन नहीं करता है। हालांकि, आप MailKit (नोट: केवल OAuth2 का समर्थन करते हैं) SmtpClient संदेशों को भेजने के लिए जब तक आपके पास उपयोगकर्ता का ओएथ एक्सेस टोकन होता है (MailKit में कोड नहीं है जो ओथ टोकन लाएगा, लेकिन यदि आपके पास है तो इसका उपयोग कर सकते हैं यह)।
सबसे पहले आपको जो करना है वह आपके आवेदन के लिए 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));
bool result = await credential.RequestAccessTokenAsync (CancellationToken.None);
// Note: result will be true if the access token was received successfully
, आप MailKit साथ उपयोग कर सकते हैं जैसे कि यह पासवर्ड था:
using (var client = new SmtpClient()) {
client.Connect ("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
// use the access token
var oauth2 = new SaslMechanismOAuth2 ("[email protected]", credential.Token.AccessToken);
client.Authenticate (oauth2);
client.Send (message);
client.Disconnect (true);
}
महोदय, इस पुस्तकालय ने मेरा दिन बचाया और एक आकर्षण की तरह काम किया! साझा करने के लिए धन्यवाद। – user3288287
कोई समस्या नहीं, खुशी है कि यह आपके लिए काम करता है! – jstedfast
@jstedfast: बिल्कुल सही। क्या आप मुझे बता सकते हैं कि मेलकिट का उपयोग कर ईमेल के साथ अनुलग्नक कैसे जोड़ सकते हैं? –