【问题标题】:Gmail (for business) API doesn't allow to send email from Alias?Gmail (for business) API 不允许从 Alias 发送电子邮件?
【发布时间】:2016-03-24 17:41:54
【问题描述】:

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

我尝试了两种方法:

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

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

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

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

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

问题是,当我尝试发送电子邮件并将“发件人”字段设置为别名(例如 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();

}

【问题讨论】:

  • 你能显示代码吗?你所说的应该有效。
  • 感谢@Tholle,我已将代码添加到我的问题正文中。这有点涉及,但我希望易于阅读。让我知道你的想法。
  • 抱歉没有回复你列夫。我看不出有什么不对。希望专业人士可以加入。:)
  • 是的,@Tholle,恐怕这是一个失败的原因。我决定只使用多个用户。别名不起作用。

标签: gmail-api google-oauth google-oauth-java-client


【解决方案1】:

经过进一步研究,似乎唯一的选择是拥有多个用户。

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

我尝试了多种选择,包括别名和群组电子邮件帐户。我会收到“委派被拒绝”或“无效授权”错误。

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

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

对于我在没有预先存在用户的情况下创建的帐户,我不得不要求我朋友的手机进行手机验证。

【讨论】:

  • 我认为 sendgrid 是一个更好的解决方案,每月 12,000 封电子邮件免费/。亚马逊,我相信还有很多其他的,也提供这种服务。
  • 感谢@BenBrahim,我已经实现了 gMail 解决方案并使用了一段时间。我会记住 SendGrid。亚马逊是一个不错的选择,但我记得我研究过它并更喜欢 gMail。
【解决方案2】:

您现在可以使用别名发送电子邮件,只要这些别名是为您正在使用其登录凭据的用户定义的。

这仅适用于企业版 Gmail。

为不存在的地址设置别名可能会很棘手,因此请查看如何设置包罗万象的路由:

catchall-for-domain-aliases-in-gsuite-gmail

【讨论】:

    【解决方案3】:

    除了 Ladi 的帖子,现在设置似乎更容易了。创建一个别名帐户并对其进行配置,以便您可以发送电子邮件 (https://support.google.com/domains/answer/9437157?hl=en&ref_topic=6293345) 并将消息上的“发件人”字段设置为别名(但在 API 调用中仍使用“我”)

    【讨论】:

      猜你喜欢
      • 2022-07-17
      • 2021-09-11
      • 2014-10-30
      • 2011-03-07
      • 2017-05-25
      • 2015-10-23
      • 2018-01-26
      • 1970-01-01
      相关资源
      最近更新 更多