ASP.NETCore控制台应用程序中仅使用服务帐户详细信息发送电子邮件的方法是什么?
创始人
2024-09-17 22:31:30
0

要使用服务帐户详细信息发送电子邮件,需要在ASP.NET Core应用程序中安装Google.Apis.Auth和Google.Apis.Gmail.v1 NuGet包。在代码示例中,以下信息需要替换为自己的服务帐户详细信息:

  • serviceAccountEmail:服务帐户电子邮件地址
  • privateKey:服务帐户的私钥

代码示例:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace ConsoleApp
{
    class Program
    {
        static string[] Scopes = { GmailService.Scope.GmailSend };
        static string ApplicationName = "Gmail API .NET Core";

        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            var service = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            Message message = new Message();

            message.Raw = Base64UrlEncode(CreateMessageWithEmail(CreateEmail()));

            try
            {
                Message sendMessageResponse = service.Users.Messages.Send(message, "me").Execute();
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
            }
        }

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

相关内容

热门资讯

wpk微扑克智能辅助!德州nz... wpk微扑克智能辅助!德州nzt软件透明挂,x-poker果然真的有挂(详细辅助挂教程)1、wpk微...
微扑克ai辅助会封号!智星德州... 微扑克ai辅助会封号!智星德州菠萝软件透明挂,德州扑克一般真的有挂(详细代打辅助教程)微扑克ai辅助...
wpk微扑克免费辅助!智星德州... wpk微扑克免费辅助!智星德州菠萝软件透明挂,aapOker其实真的有挂(详细辅助脚本教程);1、用...
aapoker ai!aapo... aapoker ai!aapoKER软件透明挂,x-poker本来真的有挂(详细辅助黑科技教程)1、...
wepower有辅助软件!约局... 您好,wepower有辅助软件这款游戏可以开挂的,确实是有挂的,需要了解加微【757446909】很...
德州nzt软件!扑克王软件透明... 德州nzt软件!扑克王软件透明挂,菠萝德州一贯真的有挂(详细辅助脚本教程)1、德州nzt软件ai机器...
wepoke挂真的假的!轰趴大... wepoke挂真的假的!轰趴大菠萝软件透明挂,aAPOKER的确真的有挂(详细代打辅助教程);1、w...
德州ai辅助代理!wepoke... 德州ai辅助代理!wepoker软件透明挂,德扑平台一贯真的有挂(详细辅助黑科技教程);1、构建自己...
微扑克辅助机器人!fish p... 微扑克辅助机器人!fish poker软件透明挂,红龙扑克的确是有挂的(详细辅助插件教程)亲,关键说...
wpk辅助器是真的!德州nzt... wpk辅助器是真的!德州nzt软件透明挂,智星德州菠萝其实真的有挂(详细ai辅助教程)1、wpk辅助...