try { using (var stream = new FileStream("credential.json", FileMode.Open, FileAccess.Read)) { string credPath = "token.json"; credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes = new[] { "https://mail.google.com/" }, "user@gmail.com", CancellationToken.None, new FileDataStore(credPath, true)).Result; Console.WriteLine("Credential file saved to: " + credPath); } var service = new GmailService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); var message = new MimeKit.MimeMessage(); message.From.Add(new MimeKit.MailboxAddress("User", "user@gmail.com")); message.To.Add(new MimeKit.MailboxAddress("Test", "test@gmail.com")); message.Subject = "Test"; message.Body = new MimeKit.TextPart("plain") { Text = "Test" }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587); // use the access token var oauth2 = new MailKit.Security.SaslMechanismOAuth2(credential.UserId, credential.Token.AccessToken); client.Authenticate(oauth2); client.Send(message); client.Disconnect(true); } } catch (Exception e) { MessageBox.Show(e.Message); }
await credential.RefreshTokenAsync(CancellationToken.None);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)