在C#中使用gmail api发送电子邮件 [英] Send email using gmail api in C#

查看:144
本文介绍了在C#中使用gmail api发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



 C#使用Gmail OAUTH身份验证发送电子邮件







预付款



我尝试了什么:



我需要使用Oauth身份验证发送邮件。

解决方案

看看这是否有助于你.net - 使用C#通过Gmail SMTP服务器发送电子邮件 - Stack Overflow [ ^ ]


.NET快速入门  |  Gmail API  |  Google开发者 [ ^ ]



关注顶部链接:



更改此行代码

 static string [] Scopes = {GmailService.Scope。  GmailSend  }; 





以下发送邮件:



 string plainText =To:ra ****** @ gmail.com,r ***** @ infosec.com\\ n+ 
主题:主题测试\\\\ n+
内容类型:text / html; charset = us-ascii \\\\\\\ n+
< h1> Body测试< / h1>;

var newMsg = new Google.Apis.Gmail.v1.Data.Message();
newMsg.Raw = Program.Base64UrlEncode(plainText.ToString());
service.Users.Messages.Send(newMsg,me)。执行();





添加此方法:



 public static string Base64UrlEncode(string input)
{
var inputBytes = System.Text.Encoding.UTF8。 GetBytes会(输入);
返回Convert.ToBase64String(inputBytes).Replace(+, - )。Replace(/,_)。Replace(=,);
}







这对我有用任何疑问 < u> shmera55@gmail.com


vb
'''将文件.json复制到My.Application.Info.DirectoryPath



  Dim 范围= GmailService.Scope.GmailSend 
Dim ApplicationName 作为 字符串 = Gmail API .NET快速入门

Dim 凭证作为 UserCredential
Dim stream = FileStream(My.Application.Info.DirectoryPath + \ _client_secret.json, FileMode.Open,FileAccess.Read)
Dim credPath As String = My.Application.Info.DirectoryPath + \
凭证= GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets ,{Scopes}, user,CancellationToken.None, FileDataStore(credPath, True ))。结果
stream.Close()
Dim service = GmailService( BaseClientService.Initializer()使用 {.HttpClientInitializer = credential,.ApplicationName = ApplicationName})
' 定义请求的参数。
Dim request As UsersResource.LabelsResource.ListRequest = service.Users.Labels.List( me
' 列表标签。
Dim 标签作为 IList
Labels = request.Execute()。标签

如果 IsDBNull(标签)= 错误 Labels.Count> 0 然后
Dim labelItem 作为标签
对于 每个 labelItem 标签
Console.WriteLine( {0},labelItem.Name)
下一步
其他
Console.WriteLine( 未找到标签。
结束 如果
Console.Read()


< span class =code-summarycomment>''' '''''''.......


Hi Friends,

C#  Send Email using Gmail OAUTH Authentication




Advance Thanks

What I have tried:

I need to send mail using Oauth Authentication.

解决方案

See if this helps you .net - Sending email through Gmail SMTP server with C# - Stack Overflow[^]


.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


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()


''''''''''''''.......


这篇关于在C#中使用gmail api发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆