gpt4 book ai didi

gmail-api - Gmail(企业版)API 不允许从 Alias 发送电子邮件?

转载 作者:行者123 更新时间:2023-12-02 22:44:17 25 4
gpt4 key购买 nike

我想使用不同的“角色”(例如 info@、customer-support@、tech-support@、no-reply@)向我的客户发送电子邮件。

我尝试了两种方法:

  1. 我的 Gmail 企业版应用程序中有多个“用户”/帐户。
  2. 单个 Gmail具有多个别名的帐户。

我首先为我的 Gmail for Business 应用程序设置一个具有全局授权的服务帐户。

为了测试它是否有效,我设置了 2 个用户:lev@mydomain.com 和 root@mydomain.com。事实上,我可以成功地从 lev@ 和 root@ 发送电子邮件。

但是,当我尝试为我的应用程序添加 5 个不同的用户帐户时,Gmail 对机器人/滥用感到偏执,并要求我证明所有帐户都是“人类”,包括通过设置密码、登录和短信验证电话。此外,他们要求不同的账户使用不同的手机来证明是不同的人。因此,账户的设置成为一个主要问题。

我还想避免创建多个帐户,因为我要为每个帐户付费,而且从语义上讲,所有角色都只是一个帐户。所以别名似乎是一个更好的主意。

问题是,当我尝试发送电子邮件并将“发件人”字段设置为别名(例如 from:no-reply@mydomain.com)时,出现以下异常:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Delegation denied for root@mydomain.com",
"reason" : "forbidden"
} ],
"message" : "Delegation denied for root@mydomain.com"
}

有人遇到并解决了这个问题吗?

身份验证/凭证代码如下:

/*
* Set up a hashmap HashMap<String, Gmail> gmailServiceByAccount where
* gmailServiceByAccount.get(emailAccount) contains an authorized Gmail service
*/
private void prepareService(String emailAccount) throws Exception {
if (gmailServiceByAccount.containsKey(emailAccount)) {
return;
}
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(Config.getInstance().getProperty(Config.gmail_service_account))
.setServiceAccountPrivateKeyFromP12File(new File(Config.getInstance().getPathToGmailCredential()))
.setServiceAccountScopes(Arrays.asList(GmailScopes.GMAIL_COMPOSE))
.setServiceAccountUser(emailAccount)
.build();


gmailServiceByAccount.put(
emailAccount,
new Gmail.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(Config.getInstance().getProperty(Config.google_client_api_application_name))
.build());
}

发送电子邮件的代码如下:

/**
* Send an email using the parameters provided.
*
* @param fromPersonalName : the free text description of the "from" address (e.g. "Customer Suppport" or "No Reply").
* @param fromAddress : the email address of the sender, the mailbox account (e.g. customer-support@mydomain.com).
* @param to : the email address of the recepient.
* @param subject : Subject of the email.
* @param htmlContent : (may be null) The HTML-styled body text of the email.
* @param plainTextContent : (may be null) The plain text body of the email (e.g if the customer email client does not support or disables html email).
*/
public void sendMail(String fromPersonalName, String fromAddress, String to, String subject, String htmlContent, String plainTextContent)
throws Exception {
prepareService(fromAddress);
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

MimeMessage email = new MimeMessage(session);
InternetAddress tAddress = new InternetAddress(to);
InternetAddress fAddress = new InternetAddress(fromAddress);
fAddress.setPersonal(fromPersonalName);
email.setFrom(fAddress);
email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress);
email.setSubject(subject);

Multipart multiPart = new MimeMultipart("alternative");
if (!StringValidation.isEmpty(plainTextContent)) {
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(plainTextContent, "text/plain");
textPart.setHeader("Content-Type", "text/plain; charset=\"UTF-8\"");
multiPart.addBodyPart(textPart);
}

if (!StringValidation.isEmpty(htmlContent)) {
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlContent, "text/html; charset=\"UTF-8\"");
multiPart.addBodyPart(htmlPart);
}
email.setContent(multiPart);


ByteArrayOutputStream bytes = new ByteArrayOutputStream();
email.writeTo(bytes);
String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
gmailServiceByAccount.get(fromAddress).users().messages().send(fromAddress, message).execute();

}

最佳答案

经过额外研究,看起来唯一的选择是拥有多个用户。

我发布的代码确实适用于多个用户,但不适用于其他任何用户。

我尝试了多种选项,包括别名和群组电子邮件帐户。我要么收到“委托(delegate)被拒绝”或“无效授权”错误。

我已尝试联系 Google For Business 客户和技术支持,但他们不支持该 API。

有一个很好的解决方法可以创建多个用户,而无需通过电话验证。只需在您最初登录 Google For Business 时将这些用户指定为“现有用户”,并在转移域名之前激活它们即可。

对于我在没有预先存在的用户的情况下创建的帐户,我必须要求我 friend 的手机进行电话验证。

关于gmail-api - Gmail(企业版)API 不允许从 Alias 发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34348413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com