I´m trying to send a email using Oauth2, but i´m getting a error when i authenticate my user. This is the error "An unhandled exception of type 'MailKit.Security.AuthenticationException' occurred in mscorlib.dll
Additional information: 334: eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ== "
Here's my code:
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"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 MimeMessage();
message.From.Add(new MailboxAddress("User", "user@gmail.com"));
message.To.Add(new MailboxAddress("Test", "test@gmail.com"));
message.Subject = "Test";
message.Body = new TextPart("plain")
{
Text = "Test"
};
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect("smtp.gmail.com", 587);
var oauth2 = new MailKit.Security.SaslMechanismOAuth2(credential.UserId, credential.Token.AccessToken);
client.Authenticate(oauth2);
client.Send(message);
client.Disconnect(true);
}
What I have tried:
I tried activating the gmail's "less security options", but still doesn't work