2015-12-08 47 views
4

An answered question在StackOverflow中,建议将html标记添加到电子邮件正文中可以解决问题。该解决方案是否正确?通过Gmail API发送带纯文本回退的HTML电子邮件

但是,如果收件人的电子邮件服务器/客户端不支持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"); 

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

P.S. 我对Java代码示例特别感兴趣。

感谢

回答

4

为了让两个选择,你可以设置Content-Typemixed/alternative,而不是要么text/plaintext/html

如果我们想在API explorer尝试了这一点,我们只是使用Base64编码的邮件,并通过全部替换/_以及所有+-来使其更安全。

在开发者控制台:

btoa(
    "Subject: Example Subject\r\n" + 
    "From: <[email protected]>\r\n" + 
    "To: <[email protected]>\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==" 
} 

要做到在Java中的等价物,你可以这样做:

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

谢谢你!看起来像是正确的做法。测试它。 –

+0

完全合作!谢谢! –

+0

@LevRatinov甜! :) 很高兴我能帮上忙。作为一个便笺, – Tholle

2

Throlle添加的更新上面的回答。 有上面可能还有一些差异与Gmail API的更新版本

发送消息,并按照其默认提供标准的发送标准的电子邮件:

这是Grails的,但你可以很容易地改变java,控制器的东西在这个html的东西可以是found here。也许当我完成了我在做什么,并有机会将与所有这些例子更新GitHub的网站:

在控制器:

def sendHTMLEmail() { 
     String emailBox='[email protected]' 
     String to ='[email protected]' 
     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}" 
     } 
    } 

and createMessag eWithEmail

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 
    } 
+1

,不确定是否从谷歌窥视留意这个名单。如果可以的话,可能值得将此示例添加到您的api doc站点的现有示例中。因为虽然这对我来说是一个相当小的过程,要弄清楚如何正确地做到这一点。我怀疑这对初学者来说可能是非常复杂的,无法将这些事情弄清楚 – Vahid

相关问题