我想使用不同的“角色”(例如 info@、customer-support@、tech-support@、no-reply@)向我的客户发送电子邮件。
我尝试了两种方法:
- 我的 Gmail 业务应用程序中有多个“用户”/帐户。
- 具有多个别名的单个 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();
}