Gmail(用于企业)API不允许从Alias发送电子邮件吗? [英] Gmail (for business) API doesn't allow to send email from Alias?

查看:190
本文介绍了Gmail(用于企业)API不允许从Alias发送电子邮件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to email my customers using different "roles" (e.g. info@ , customer-support@, tech-support@, no-reply@).

我尝试了2种方法:

  1. 我的Gmail商业应用程序中有多个用户"/帐户.
  2. 单个gmail 具有多个别名的帐户.
  1. Multiple "users"/accounts in my Gmail for business application.
  2. Single gmail account with multiple aliases.

我首先为我的Gmail for Business应用程序设置了具有全球授权的服务帐户.

I started by setting up a Service Account with global delegation for my Gmail for Business application.

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

To test that it works, I've set up 2 users: lev@mydomain.com and root@mydomain.com. Indeed, I can successfully send email both from lev@ and root@.

但是,当我尝试为我的应用程序添加5个不同的用户帐户时,Gmail感到僵尸程序/滥用行为偏执,并要求我证明所有帐户都是人为"的,包括设置密码,登录和通过短信验证电话.而且,他们需要使用不同的电话来注册不同的帐户,以证明它是不同的人.因此,帐户的设置成为一个主要问题.

However, when I tried adding 5 distinct user accounts for my application, Gmail got paranoid of bots/abuse and asked me to prove that all the accounts are "human" including setting up passwords, signing in and SMS-text validation via phone. Moreover, they require different phones for different accounts to prove it's a different person. So the setup of the accounts becomes a major issue.

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

I also want to avoid creating multiple accounts since I'm paying for each one, and since semantically, all the roles are just a single account. So aliases seem like a better idea.

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

The problem is that when I'm trying to send email and set the "from" field to the alias (e.g. from:no-reply@mydomain.com), I'm getting the following exception:

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"
}

有人面对并解决了这个问题吗?

Anyone faced and solved this issue?

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

The authentication/credential code is as follows:

/*
 * 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());
}

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

And the code which sends the email is as follows:

/**
 * 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();

}

推荐答案

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

After additional research, it looks like the only option is to have multiple users.

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

The code I've posted indeed works for multiple users, but not for anything else.

我尝试了多个选项,包括别名和组电子邮件帐户.我会收到授权被拒绝"或无效授予"错误.

I've tried multiple options including aliases and group email accounts. I'd either get "delegation denied" or "invalid grant" errors.

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

I've tried contacting Google For Business customer and tech support, but they don't support the API.

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

There's a great workaround to creating several users without having to go through phone validation. Just specify these users as "existing users" when you're signing into Google For Business initially, and activate them before you even transfer the domain.

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

For the account I've created without pre-existing users, I had to ask my friend's phones for phone validation.

这篇关于Gmail(用于企业)API不允许从Alias发送电子邮件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆