Click here to Skip to main content
15,936,989 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:
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    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", ""));
            message.To.Add(new MailboxAddress("Test", ""));
            message.Subject = "Test";
            message.Body = new TextPart("plain")
                Text = "Test"
            using (var client = new MailKit.Net.Smtp.SmtpClient())
                client.Connect("", 587);
                // use the access token 
                var oauth2 = new MailKit.Security.SaslMechanismOAuth2(credential.UserId, credential.Token.AccessToken);

What I have tried:

I tried activating the gmail's "less security options", but still doesn't work
Updated 20-May-20 4:15am

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)
Share this answer
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

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