我正在尝试在网络应用程序(当前在本地主机)中发送电子邮件,我正在使用我用于控制台应用程序的下一个代码(及其工作方式),它应该打开一个网页询问Google帐户:

public async Task<String> SendEmail(String from, String text)
{
    try
    {
        UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "-------------",
                ClientSecret = "----------"
            },
            new[] { GmailService.Scope.GmailSend },
            "user",
            CancellationToken.None,
            new FileDataStore(this.GetType().ToString()));

        // Create Gmail API service.
        var service = new GmailService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = this.GetType().ToString()
        });

        string plainText = "To: [email protected]," + from + "\r\n" +
                           "Subject: T2S\r\n" +
                           "Content-Type: text/html; charset=us-ascii\r\n\r\n" +
                           text;

        var newMsg = new Google.Apis.Gmail.v1.Data.Message();
        newMsg.Raw = Base64UrlEncode(plainText.ToString());
        service.Users.Messages.Send(newMsg, "me").Execute();
        return "Done";
    }
    catch (Exception ex)
    {
        System.Console.WriteLine(ex.Message);
        return "False";
    }
}

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


我收到错误消息:


  Google.Apis.Requests.RequestError
  
  权限不足[403]
  
  错误[消息[权限不足]位置[-]
  原因[insufficientPermissions]域[全局]

最佳答案

谢谢您,DalmTo确实与更改完全兼容:

将“用户”更改为“ XXXX”

07-28 07:15