使用 Google OAuth 2 和 ASP.Net MVC 发送/接收电子邮件



>我在类中使用了以下内容:

public class AppFlowMetaData : FlowMetadata
{
    private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
    {
        ClientSecrets = new ClientSecrets
        {
            ClientId = "nnnnnnnnnnnnn.apps.googleusercontent.com",
            ClientSecret = "nnnnnnn-nnnnnnnnnnnnnnn"
        },
        Scopes = new[] { CalendarService.Scope.Calendar },
        DataStore = new FileDataStore("Calendar.Api.Auth.Store")
    });
    public override string GetUserId(System.Web.Mvc.Controller controller)
    {
        var user = controller.Session["user"];
        if (user == null)
        {
            user = Guid.NewGuid();
            controller.Session["user"] = user;
        }
        return user.ToString();
    }
    public override IAuthorizationCodeFlow Flow
    {
        get { return flow; }
    }
}

控制器上的以下内容:

public async Task<ActionResult> TestAsync(CancellationToken token)
    {
        var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetaData()).AuthorizeAsync(token);   
        if(result.Credential != null)
        {
            var service = new CalendarService(new BaseClientService.Initializer
            {
                HttpClientInitializer = result.Credential,
                ApplicationName = "Calendar Test"
            });
            IList<CalendarListEntry> results = service.CalendarList.List().Execute().Items;
            List<Event> model = new List<Event>();
            foreach (var calendar in results)
            {
                Google.Apis.Calendar.v3.EventsResource.ListRequest request = service.Events.List(calendar.Id);
                var events = request.Execute().Items;
                foreach (var e in events)
                {
                    model.Add(e);
                }
            }
            return View(model);
        }
        else
        {
            return new RedirectResult(result.RedirectUri);
        }
    }

这效果很好,我希望能够使用相同的过程使用 gmail 发送邮件,但我在 Google API 文档中找不到范围或任何细节(坦率地说,这很糟糕)基于相同的过程执行此操作。 我已经看到第三方库在其他地方使用 Imap,但这依赖于传递我不想做的用户名/密码。

谁能帮我更改上述内容以用于Gmail?

我意识到这个问题现在已经有一年了,但它是我发现的为数不多的问这个问题之一。我设法使用以下代码使用 Gmail API 发送电子邮件。当用户登录到我的站点时,我将保存访问令牌和刷新令牌,然后将它们插入下面的方法。感谢Jason Pettys为我指出正确的方向(http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/)。

public string SendMail(string fromAddress, string toEmail, string subject, string body)
    {
        //Construct the message
        var mailMessage = new System.Net.Mail.MailMessage(fromAddress, toEmail, subject, body);
        mailMessage.ReplyToList.Add(new MailAddress(fromAddress));
        //Specify whether the body is HTML
        mailMessage.IsBodyHtml = true;
        //Convert to MimeMessage
        MimeMessage message = MimeMessage.CreateFromMailMessage(mailMessage);
        var rawMessage = message.ToString();
        var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
            {
                ClientId = "GoogleClientId",
                ClientSecret = "GoogleClientSecret"
            },
            Scopes = new[] { GmailService.Scope.GmailCompose }
        });
        var token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse()
            {
                AccessToken = MethodToRetrieveSavedAccessToken(),
                RefreshToken = MethodToRetrieveSavedRefreshToken()
            };
        //In my case the username is the same as the fromAddress
        var gmail = new GmailService(new Google.Apis.Services.BaseClientService.Initializer()
            {
                ApplicationName = "App Name",
                HttpClientInitializer = new UserCredential(flow, fromAddress, token)
            });
        var result = gmail.Users.Messages.Send(new Message
            {
                Raw = Base64UrlEncode(rawMessage)
            }, "me").Execute();
        return result.Id;
    }
    /// <summary>
    /// Converts input string into a URL safe Base64 encoded string. Method thanks to Jason Pettys.
    /// http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    private static string Base64UrlEncode(string input)
    {
        var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
        // Special "url-safe" base64 encode.
        return Convert.ToBase64String(inputBytes)
          .Replace('+', '-')
          .Replace('/', '_')
          .Replace("=", "");
    }

相关内容

  • 没有找到相关文章

最新更新