Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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);
                // use the access token 
                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
Posted
Updated 20-May-20 4:15am

The 334 indicates an authentication error of some kind - I'd have expected extra information in the response alluding to the exact problem
 
Share this answer
 
I would recommend that you read through the following page and verify that you have everything set up correctly; sounds like the MailKit sample code may not be the same as Google provides
MailKit Issue #962: AuthenticationException 334[^]

It also told you how to decode the Additional Information, which is just Base64
Base64.Decode(Additional Info)
{"status":"400","schemes":"Bearer","scope":"https://mail.google.com/"}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900