Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Friends,

C#  Send Email using Gmail OAUTH Authentication

Advance Thanks

What I have tried:

I need to send mail using Oauth Authentication.
Updated 21-Aug-17 9:13am

Share this answer
[no name] 18-Jan-17 3:29am    
Thanks for reply but this is not right one. I need to send mail using Oauth.
Ashwin. Shetty 18-Jan-17 4:22am    
Google have a documentation for OAuth, did you check this
.NET Quickstart  |  Gmail API  |  Google Developers[^]

Follow top link:

Change this line of code
static string[] Scopes = { GmailService.Scope.GmailSend };

Below one for send mail:

string plainText = "To: ra******,r*****\r\n" +
                               "Subject: subject Test\r\n" +
                               "Content-Type: text/html; charset=us-ascii\r\n\r\n" +
                               "<h1>Body Test </h1>";
var newMsg = new Google.Apis.Gmail.v1.Data.Message();
newMsg.Raw = Program.Base64UrlEncode(plainText.ToString());
service.Users.Messages.Send(newMsg, "me").Execute();

Add this method:

public static string Base64UrlEncode(string input)
          var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
          return Convert.ToBase64String(inputBytes).Replace("+", "-").Replace("/", "_").Replace("=", "");

It's working for me any doubt
Share this answer
Comments 7-Feb-18 11:11am    
I get error

//Bad Request[400]
// Message[Bad Request] Location[- ] Reason[failedPrecondition] Domain[global]
Member 12700305 6-Aug-19 1:07am    
Given multilingual subject, draft is created but the characters are not decoded correctly while creating draft. Any solution to it ?
vb '''copy file .json to My.Application.Info.DirectoryPath

Dim Scopes = GmailService.Scope.GmailSend
        Dim ApplicationName As String = "Gmail API .NET Quickstart"

        Dim credential As UserCredential
        Dim stream = New FileStream(My.Application.Info.DirectoryPath + "\client_secret.json", FileMode.Open, FileAccess.Read)
        Dim credPath As String = My.Application.Info.DirectoryPath + "\"
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, {Scopes}, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
        Dim service = New GmailService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = ApplicationName})
        ' Define parameters of request.
        Dim request As UsersResource.LabelsResource.ListRequest = service.Users.Labels.List("me")
        ' List labels.
        Dim Labels As IList
        Labels = request.Execute().Labels

        If IsDBNull(Labels) = False And Labels.Count > 0 Then
            Dim labelItem As Label
            For Each labelItem In Labels
                Console.WriteLine("{0}", labelItem.Name)
            Console.WriteLine("No labels found.")
        End If

Share this answer
Graeme_Grant 21-Aug-17 17:30pm    
Any particular reason that you are answering a question where an answer is already accepted? Please don't. There are many other unanswered questions waiting for you.
Nelek 21-Aug-17 17:31pm    
Eight months old and already solved... seriously?

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