Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Send email by using MailKit with Gmail
- using (var emailClient = new SmtpClient())
- {
- // https://console.developers.google.com
- var secrets = new ClientSecrets
- {
- ClientId = "",
- ClientSecret = ""
- };
- // Generating a refresh token - https://www.youtube.com/watch?v=hfWe1gPCnzc
- var token = new TokenResponse { RefreshToken = "YourRefreshToken" };
- var googleCredentials = new UserCredential(new GoogleAuthorizationCodeFlow(
- new GoogleAuthorizationCodeFlow.Initializer
- {
- ClientSecrets = secrets
- }), SmtpUsername, token);
- emailClient.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
- await googleCredentials.GetAccessTokenForRequestAsync();
- var oauth2 = new SaslMechanismOAuth2(SmtpUsername, googleCredentials.Token.AccessToken);
- emailClient.Authenticate(oauth2);
- await emailClient.SendAsync(message); // MimeMessage
- emailClient.Disconnect(true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement