How to fully export a html email with the gmail api in java

别来无恙 提交于 2019-12-25 00:40:06

问题


I want to extract a specific spam mail with the gmail api and display it with my webapp but the styling is lost during the export. The problem is the styling is not provided inline and I can't find the source of the styling.

I am using spring boot and the gmail java api to extract the email. I tested some other emails that contain inline styling and they work. I extracted the html code of the email manually and tired display it via a html file but it only get's displayed without styling.


//How I get the message
Message message = service.users().messages().get(USER, messages.get(0).getId()).setFormat("full").execute();

//How I get the content
 private String getMessageContent(Message message) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            if(message.getPayload().getParts() != null) {
                handleEmailMainContent(message.getPayload().getParts(), stringBuilder);
                byte[] bodyBytes = Base64.decodeBase64(stringBuilder.toString());
                //System.out.println(new String(bodyBytes, "UTF-8"));
                String untrustedHTML = new String(bodyBytes, "UTF-8");
                // sanitize content before saving it
                System.out.println(untrustedHTML);
                return sanitizeContent(untrustedHTML);
            } else {
                return "";
            }
        } catch (UnsupportedEncodingException e) {
           System.out.println("UnsupportedEncoding: " + e.toString());
            return message.getSnippet();
        }
    }

    private void handleEmailMainContent(List<MessagePart> messageParts, StringBuilder stringBuilder) {
        for (MessagePart messagePart : messageParts) {
            switch (messagePart.getMimeType()) {
                case "text/plain":
                    handleMimeTypeTextPlain(messagePart, stringBuilder);
                    break;
                case "text/html":
                    handleMimeTypeTextHtml(messagePart, stringBuilder);
                    break;
                default:
                    break;
            }
        }
    }

private void handleMimeTypeTextPlain(MessagePart messagePart, StringBuilder stringBuilder) {
        stringBuilder.append(messagePart.getBody().getData());
        stringBuilder.append(System.lineSeparator());
    }

    private void handleMimeTypeTextHtml(MessagePart messagePart, StringBuilder stringBuilder) {
        stringBuilder.append(messagePart.getBody().getData());
        stringBuilder.append(System.lineSeparator());
    }

Snippet of what I extract (unsanitized)

[image: logo]
Become part of the Heally family on *Facebook*
<https://tech.us10.list-manage.com/track/click?u=699dcccc14610bc14c55eb9d6&id=fc3bc83b82&e=8396d913ca>
!
- LET'S TALK
Celebraing Mom
*...*
Moms run the world. They literally give us life, take on one of the hardest
roles, and do it all without an end to their workday or any time off. While
moms deserve to be celebrated year-round, Mother’s Day is a special time to
make sure they know they’re loved and appreciated.
LEARN MORE
<https://tech.us10.list-manage.com/track/click?u=699dcccc14610bc14c55eb9d6&id=b05060d980&e=8396d913ca>


Looking for CBD only Products?
PROMO CODE: *SAVECBD20*

I sanitize the html code for security reasons but I even tried the unsanitized version and had no success.

I would expect that I can extract everything I need through the gmail API to display the email like it get's displayed at my gmail client but the email lacks the needed styling.

来源:https://stackoverflow.com/questions/56257871/how-to-fully-export-a-html-email-with-the-gmail-api-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!