尝试从不同的电子邮件地址发送 Gmail 时出现 401 未经授权的错误

401 Unauthorized error when trying to send Gmail from a different email address

我正在尝试使用脚本生成 Google 文档,然后将其发送到电子邮件。 当我将我的电子邮件作为“发件人”参数时,一切正常(我使用我的 Google 帐户生成此文档)。

GmailApp.sendEmail


(
   invoice.customerEmail, 
   'title', 
   invoice.emailBody, 
   {
     attachments: [doc.getAs(MimeType.PDF)],
     name: 'subject title',
     htmlBody: invoice.emailBody, 
     from: 'myemail@gmail.com'
   }
 );

当我在 'from' 中输入其他电子邮件时出现问题。我将其发布为网络应用程序。以下是设置:

有什么办法可以解决吗?

根据 GmailApp documentation:

from > the address that the email should be sent from, which must be one of the values returned by getAliases()

因此,您可以将 sendEmail 与其他电子邮件地址一起使用 只有 如果这些电子邮件地址是执行脚本的电子邮件的别名。

但是,如果您不想将这封电子邮件添加为您的别名,您只需在该其他帐户上创建脚本并从那里执行即可。

参考