Authorization Error in Mailing

Asked 2 months ago, Updated 2 months ago, 3 views

When I copied the code to send Gmail from the GAS code that I was able to execute before, I encountered a transmission error.The code is as follows:

function send_mail(){
  var recipient='[email protected]';
  var subject = 'foo';
  var body='baz\n';
  var options = {name:'bar'};
  GmailApp.sendEmail(recipient, subject, body, options);
}

When you run the above function, you will be asked if you want to give me permission to run it, and you will get the following error:

Access denied::Missing access token for authorization.Request:
MailboxService.SendMessage. (line 143, file "SpreadSheetCode")

line 143 is

GmailApp.sendEmail(recipient, subject, body, options);

This is the part of

The appssscript.json is as follows.

{
  "timeZone": "Asia/Tokyo",
  "dependencies": {
  },
  "webapp": {
    "access": "DOMAIN",
    "executeAs": "USER_ACCESSING"
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://www.googleapis.com/auth/spreadsheets.currentonly",
          "https://www.googleapis.com/auth/script.send_mail",
          "https://www.googleapis.com/auth/spreadsheets",
          "https://www.googleapis.com/auth/gmail.modify",
          "https://www.googleapis.com/auth/gmail.send",
          "https://www.googleapis.com/auth/gmail.compose",
          "https://mail.google.com/",
          "https://www.googleapis.com/auth/gmail.addons.current.action.compose"]
}

The mail sender and recipient belong to the same organization.

How can I send an email without any errors?
Thank you for your cooperation.

add
MailApp.sendEmail() instead of GmailApp.sendEmail() worked.However, it used to work with GmailApp, and I don't know the difference between the two.

javascript google-apps-script gmail

2022-09-30 14:02

1 Answers

In the article Change to Mail Service in Apps Script dated March 10, 2016,

Starting on September 13, 2016, [omitted] Google Apps customer who does not use Gmail, but uses Apps Script installed, can send emails through MailApp but not GmailApp.

[Direct translation] Google Apps users who use Apps Script instead of using Gmail can send emails via MailApp but not via GmailApp after September 13, 2016.

It says

If it was made before September 12, 2016, it is highly likely that the above article is related.
If you take a Google account based on the article and set the primary mail address to be Gmail, you will be able to send mail via MailApp or GmailApp.


2022-09-30 14:02

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.