如何使用带有服务帐号的Gmail API或C#中的Google OAuth来发送邮件? [英] How to use gmail api with service accounts or google Oauth in C# for sending mails?
本文介绍了如何使用带有服务帐号的Gmail API或C#中的Google OAuth来发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
推荐答案
只有在您拥有Google工作区域帐户的情况下,Gmail API才能使用服务帐户。
您需要将域范围delegation设置为域上的用户,然后服务帐户将能够代表其发送电子邮件。我想在这种情况下,创建一个名为no-回复的用户将是一种选择。Code是标准服务帐户授权码,添加了要委派给的gsuiteUser。
string ApplicationName = "Gmail API .NET Quickstart";
const string serviceAccount = "XXXXX@XXXX-api.iam.gserviceaccount.com";
var certificate = new X509Certificate2(@"cXXXX-api-ed4859a67674.p12", "notasecret", X509KeyStorageFlags.Exportable);
var gsuiteUser = "domainUser@XXXX.com";
var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(serviceAccount)
{
User = gsuiteUser,
Scopes = new[] { GmailService.Scope.Gmail }
}.FromCertificate(certificate);
var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);
if (!credential.RequestAccessTokenAsync(CancellationToken.None).Result)
throw new InvalidOperationException("Access token failed.");
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
应在您的网站用户本身上创建忘记密码功能。我会假设它有自己的域和邮件服务器。您应该使用它,而不是Gmail API。
这篇关于如何使用带有服务帐号的Gmail API或C#中的Google OAuth来发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文