通过Gmail API发送带有纯文本后缀的HTML电子邮件 [英] Send HTML email with plain text fallback via Gmail API

查看:322
本文介绍了通过Gmail API发送带有纯文本后缀的HTML电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StackOverflow上的回答问题表明添加html标记的电子邮件的身体会做的伎俩。这个解决方案是否正确?



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



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

  //设置html消息
email.setHtmlMsg(< html>我们的徽标 - < img src = \ CID: + CID + \ >< / HTML>中);
//设置替代消息
email.setTextMsg(您的电子邮件客户端不支持HTML消息);

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



PS
我对Java代码示例感兴趣。



谢谢

解决方案

要给出两个选项,您可以将 Content-Type 设置为 mixed / alternative 而不是 text / plain text / html



如果我们希望在 API资源管理器中尝试这样做,我们只是对邮件进行Base64编码,并将所有 / 替换为 _ 并将所有 + -



在开发者控制台中: (









$ example@gmail.com> \r\\\
+
To:< example@gmail.com> \r\\\
+
Content-Type:multipart / alternative; boundary = \foo_bar\\r\\\
\r\\\
+

--foo_bar\r\\ n+
Content-Type:text / plain; charset = UTF-8\r\\\
\r\\\
+

*粗体示例消息文本* \r\\\
\r\\\
+

--foo_bar\r\\\
+
Content-Type:text / html; charset = UTF-8 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ; / b>< / div> \r\\\
\r\\\
+

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

POST https://www.googleapis.com/gmail/v1/users/me/messages /发送ACCESS_TOKEN = {} YOUR_API_KEY

{
原始: U3ViamVjdDogRXhhbXBsZSBTdWJqZWN0DQpGcm9tOiA8ZXhhbXBsZUBnbWFpbC5jb20-DQpUbzogPGV4YW1wbGVAZ21haWwuY29tPg0KQ29udGVudC1UeXBlOiBtdWx0aXBhcnQvYWx0ZXJuYXRpdmU7IGJvdW5kYXJ5PSJmb29fYmFyIg0KDQotLWZvb19iYXINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsgY2hhcnNldD1VVEYtOA0KDQoqQm9sZCBleGFtcGxlIG1lc3NhZ2UgdGV4dCoNCg0KLS1mb29fYmFyDQpDb250ZW50LVR5cGU6IHRleHQvaHRtbDsgY2hhcnNldD1VVEYtOA0KDQo8ZGl2IGRpcj0ibHRyIj48Yj5Cb2xkIGV4YW1wbGUgbWVzc2FnZSB0ZXh0PC9iPjwvZGl2Pg0KDQotLWZvb19iYXItLQ ==
}?

要在Java中执行等效操作,可以执行以下操作:

 消息消息=新的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());

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


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

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

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");

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

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

Thanks

解决方案

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

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 -.

In the Developer Console:

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

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

  "*Bold example message text*\r\n\r\n" +

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

  "<div dir=\"ltr\"><b>Bold example message text</b></div>\r\n\r\n" +

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

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

这篇关于通过Gmail API发送带有纯文本后缀的HTML电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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