4

我想使用不同的“角色”(例如 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();

}
4

3 回答 3

1

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

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

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

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

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

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

于 2015-12-22T13:45:58.353 回答
0

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

这仅适用于企业版 Gmail。

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

在 gsuite-gmail 中捕获所有域别名

于 2018-02-26T16:18:13.480 回答
0

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

于 2021-02-26T01:39:10.047 回答