An answered question建议将html-markup添加到电子邮件正文中就可以了。这个解决方案是否正确?
但是,如果收件人的电子邮件服务器/客户端不支持HTML电子邮件呢?
E.g。使用Apache commons email时,我会执行以下操作:
// 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哪些电子邮件会退回?
P.S。 我对Java代码示例特别感兴趣。
由于
答案 0 :(得分:5)
要提供两种选择,您可以将Content-Type
设置为mixed/alternative
,而不是text/plain
或text/html
:
如果我们想在API explorer中尝试这一点,我们只需对邮件进行Base64编码,并将所有/
替换为_
并将所有{{}替换为url-safe 1}}与+
。
在开发者控制台中:
-
要在Java中执行等效操作,您可以这样做:
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=="
}
答案 1 :(得分:2)
从 Throlle 添加上述答案的更新。 上面和gmail api的后续版本可能存在一些差异
发送邮件并按照默认提供的标准发送标准电子邮件:
这是grails,但你可以很容易地改变java,这个html的东西之前的控制器东西可以是found here。也许当我完成我正在做的事情并获得机会将使用所有这些示例更新github网站:
在控制器中:
def sendHTMLEmail() {
String emailBox='me@gmail.com'
String to ='someuser@domain.com'
String html="<html><body><table><tr><td><b>aa</b></td><td>bb</td></tr></table><h1>html content</h1></body></html>"
MimeMessage content = gmailService.createHTMLEmail(to,emailBox,'gmail test','testing gmail via app',html)
def message = gmailService.sendMessage(gmail,'me',content)
render "=== ${message.id}"
}
在服务中:
public static MimeMessage createHTMLEmail(String to, String from, String subject, String text, String html) {
Properties props = new Properties()
Session session = Session.getDefaultInstance(props, null)
MimeMessage email = new MimeMessage(session)
Multipart multiPart = new MimeMultipart("alternative")
email.setFrom(new InternetAddress(from))
email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to))
email.setSubject(subject)
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)
email.setContent(multiPart)
return email
}
然后他们的标准提供了sendMessage(可以在github链接上找到以下位):
public static Message sendMessage(Gmail service,String userId,MimeMessage emailContent) throws MessagingException, IOException {
try {
Message message = createMessageWithEmail(emailContent)
message = service.users().messages().send(userId, message).execute()
return message
} catch (Exception e) {
//log.error "${e}"
}
}
和createMessageWithEmail
public static Message createMessageWithEmail(MimeMessage emailContent) throws MessagingException, IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream()
emailContent.writeTo(buffer)
byte[] bytes = buffer.toByteArray()
String encodedEmail = Base64.encodeBase64URLSafeString(bytes)
Message message = new Message()
message.setRaw(encodedEmail)
return message
}