gmail-api – Gmail(for business)API不允许从Alias发送电子邮件?

我想通过不同的“角色”(例如info @,customer-support @,tech-support @,no-reply @)向我的客户发送电子邮件.

我试过两种方法:

>我的Gmail for Business应用程序中有多个“用户”/帐户.
>单个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();

}

最佳答案 经过进一步的研究,看起来唯一的选择是拥有多个用户.

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

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

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

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

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

点赞