Send HTML email with plain text fallback via Gmail API

前端 未结 2 570
予麋鹿
予麋鹿 2021-01-12 14:24

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 recipien

2条回答
  •  死守一世寂寞
    2021-01-12 15:18

    To add an update to above answer from Throlle. There were some differences in above and possibly with the later versions of gmail api

    to send a message and follow their default provided standards for sending a standard email :

    This is in grails but you can easily change for java, the controller stuff pre this html stuff can be found here. Maybe when I am finished with what I am doing and get a chance will update the github site with all these examples:

    In controller:

      def sendHTMLEmail() {
            String emailBox='me@gmail.com'
            String to ='someuser@domain.com'
            String html="
    aabb

    html content

    " MimeMessage content = gmailService.createHTMLEmail(to,emailBox,'gmail test','testing gmail via app',html) def message = gmailService.sendMessage(gmail,'me',content) render "=== ${message.id}" }

    In Service:

    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
        }
    

    Then their standard provided sendMessage (below bits can be found on github link):

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

提交回复
热议问题