通过 Gmail API 发送带有纯文本回退的 HTML 电子邮件

2022-01-23 00:00:00 email gmail-api gmail java

一个已回答的问题 在 StackOverflow 建议添加 html- 电子邮件正文的标记将起到作用.该解决方案正确吗?

An answered question at StackOverflow suggests that adding html-markup to the body of the email will do the trick. Is that solution correct?

但是如果收件人的电子邮件服务器/客户端不支持 HTML 电子邮件怎么办?

But what if the recipient's email server/client does not support HTML email?

例如使用 Apache 公共电子邮件 时,我执行以下操作:

E.g. when using Apache commons email, I do the following:

// set the html message
email.setHtmlMsg("<html>Our logo - <img src="cid:"+cid+""></html>");
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");

如果收件人电子邮件服务器/客户端不支持 HTML,有没有办法告诉 Gmail API 回退到哪个电子邮件?

Is there any way to tell Gmail API what email to fall back to if the recipient email server/client does not support HTML?

附:我对 Java 代码示例特别感兴趣.

P.S. I'm especially interested in Java code samples.

谢谢

推荐答案

要提供两种选择,您可以将 Content-Type 设置为 mixed/alternative 而不是其中任何一种text/plaintext/html:

To give two alternatives, you can set the Content-Type to mixed/alternative instead of either text/plain or text/html:

如果我们想在 API explorer,我们只是对邮件进行 Base64 编码,并将所有 / 替换为 _ 并将所有 + 替换为 <代码>-.

If we want to try this out in the API explorer, we just Base64-encode the mail, and make it url-safe by replacing all / with _ and all + with -.

在开发者控制台中:

btoa(
  "Subject: Example Subject
" +
  "From: <example@gmail.com>
" +
  "To: <example@gmail.com>
" +
  "Content-Type: multipart/alternative; boundary="foo_bar"

" +

  "--foo_bar
" +
  "Content-Type: text/plain; charset=UTF-8

" +

  "*Bold example message text*

" +

  "--foo_bar
" +
  "Content-Type: text/html; charset=UTF-8

" +

  "<div dir="ltr"><b>Bold example message text</b></div>

" +

  "--foo_bar--" 
).replace(/+/g, '-').replace(///g, '_');

POST https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token={YOUR_API_KEY}

{
 "raw": "U3ViamVjdDogRXhhbXBsZSBTdWJqZWN0DQpGcm9tOiA8ZXhhbXBsZUBnbWFpbC5jb20-DQpUbzogPGV4YW1wbGVAZ21haWwuY29tPg0KQ29udGVudC1UeXBlOiBtdWx0aXBhcnQvYWx0ZXJuYXRpdmU7IGJvdW5kYXJ5PSJmb29fYmFyIg0KDQotLWZvb19iYXINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsgY2hhcnNldD1VVEYtOA0KDQoqQm9sZCBleGFtcGxlIG1lc3NhZ2UgdGV4dCoNCg0KLS1mb29fYmFyDQpDb250ZW50LVR5cGU6IHRleHQvaHRtbDsgY2hhcnNldD1VVEYtOA0KDQo8ZGl2IGRpcj0ibHRyIj48Yj5Cb2xkIGV4YW1wbGUgbWVzc2FnZSB0ZXh0PC9iPjwvZGl2Pg0KDQotLWZvb19iYXItLQ=="
}

要在 Java 中做同样的事情,你可以这样做:

To do the equivalent in Java, you could do:

Message message = new MimeMessage(session);
Multipart multiPart = new MimeMultipart("alternative");

MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(text, "utf-8");

MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, "text/html; charset=utf-8");

multiPart.addBodyPart(textPart); 
multiPart.addBodyPart(htmlPart);
message.setContent(multiPart);

ByteArrayOutputStream output = new ByteArrayOutputStream();
message.writeTo(output);
String rawEmail = Base64.encodeBase64URLSafeString(output.toByteArray());

Message message = new Message();
message.setRaw(rawEmail);
message = service.users().messages().send(userId, message).execute();

相关文章