Click here to Skip to main content
14,335,049 members
Rate this:
Please Sign up or sign in to vote.
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.
Posted
Updated 21-Aug-17 9:13am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Comments
Ramesh Balasubramani 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 https://developers.google.com/gmail/xoauth2_protocol
Rate this:
Please Sign up or sign in to vote.

Solution 2

.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******@gmail.com,r*****@infosec.com\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 shmera55@gmail.com
   
Comments
kiquenet.com 7-Feb-18 11:11am
   
I get error

Google.Apis.Requests.RequestError
//Bad Request[400]
//Errors[
// 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 ?
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
        stream.Close()
        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)
            Next
        Else
            Console.WriteLine("No labels found.")
        End If
        Console.Read()


''''''''''''''.......
   
v2
Comments
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100