Insufficient Permission [403] sending email

I'm trying to send emails in my web app (currently in localhost), I'm using the next code that I used (and its working) for a console app, it should open a webpage asking for google account:

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("=", "");
}

I'm getting the error:

Google.Apis.Requests.RequestError

Insufficient Permission [403]

Errors [Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]

Upvotes: 2

Views: 832

Answers (1)

thank you DalmTo indeed it worked perfectly fine with the change:

change "user", to "XXXX"

Upvotes: 0

Related Questions