20

有没有办法使用 GMail api 以 html 形式检索邮件正文?

我已经浏览了message.get文档。尝试将format参数更改为full, minimal& raw。但这并没有帮助。它返回邮件正文的纯文本。


格式值说明:

“full”:在有效负载字段中返回解析的电子邮件内容,不使用原始字段。(默认)

“minimal”:仅返回电子邮件元数据,例如标识符和标签,不返回电子邮件标头、正文或有效负载。

“raw”:将原始字段中的整个电子邮件内容作为字符串返回,并且不使用有效负载字段。这包括标识符、标签、元数据、MIME 结构和小正文部分(通常小于 2KB)。


我们不能简单地以 html 形式获取邮件正文,还是有任何其他方法可以做到这一点,以便当他们在我的应用程序或 GMail 中看到邮件时,邮件显示在屏幕上的差异非常小?

4

3 回答 3

22

同时具有 HTML 和纯文本内容的电子邮件消息将具有多个有效负载部分,而 mimeType 为“text/html”的部分将包含 HTML 内容。您可以使用以下逻辑找到它:

var part = message.parts.filter(function(part) {
  return part.mimeType == 'text/html';
});
var html = urlSafeBase64Decode(part.body.data);
于 2014-06-26T14:29:02.287 回答
7

FULL 和 RAW 都会根据您的喜好返回任何文本/html 部分。如果你使用 FULL,你会得到一个解析的表示,它是嵌套的 json 字典,你必须走过去寻找 text/html 部分。如果您选择 RAW 格式,您将在 Message.raw 字段中获得 RFC822 格式的整个电子邮件。您可以将其传递给您选择的语言的 mime 库,然后使用它来查找您感兴趣的部分。 Mime 很复杂,您可能会有一个顶级的“多部分”类型,其中 text/html 是其中之一它的直接孩子,但不能保证,它是一个任意深度的树结构!:)

于 2014-06-26T16:05:57.037 回答
5

这是完整的教程:

1-假设您已经在这里完成了所有凭据创建

2- 这是您检索 Mime 消息的方式:

 public static String getMimeMessage(String messageId)
            throws Exception {

           //getService definition in -3
        Message message = getService().users().messages().get("me", messageId).setFormat("raw").execute();

        Base64 base64Url = new Base64(true);
        byte[] emailBytes = base64Url.decodeBase64(message.getRaw());

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        MimeMessage email = new MimeMessage(session, new ByteArrayInputStream(emailBytes));

        return getText(email); //getText definition in at -4
    }

3- 这是创建 Gmail 实例的部分:

private static Gmail getService() throws Exception {
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    // Load client secrets.
    InputStream in = SCFManager.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    if (in == null) {
        throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
    }
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
            .setAccessType("offline")
            .build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");

    return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

4-这就是您解析 Mime 消息的方式:

 public static String getText(Part p) throws
            MessagingException, IOException {
        if (p.isMimeType("text/*")) {
            String s = (String) p.getContent(); 
            return s;
        }

        if (p.isMimeType("multipart/alternative")) {
            // prefer html text over plain text
            Multipart mp = (Multipart) p.getContent();
            String text = null;
            for (int i = 0; i < mp.getCount(); i++) {
                Part bp = mp.getBodyPart(i);
                if (bp.isMimeType("text/plain")) {
                    if (text == null) {
                        text = getText(bp);
                    }
                    continue;
                } else if (bp.isMimeType("text/html")) {
                    String s = getText(bp);
                    if (s != null) {
                        return s;
                    }
                } else {
                    return getText(bp);
                }
            }
            return text;
        } else if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) p.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                String s = getText(mp.getBodyPart(i));
                if (s != null) {
                    return s;
                }
            }
        }

        return null;
    }

5-如果您想知道如何获取电子邮件 ID,这就是您列出它们的方式:

 public static List<String> listTodayMessageIds() throws Exception {
        ListMessagesResponse response = getService()
                .users()
                .messages()
                .list("me") 
                .execute();  

        if (response != null && response.getMessages() != null && !response.getMessages().isEmpty()) {
            return response.getMessages().stream().map(Message::getId).collect(Collectors.toList());
        } else {
            return null;
        }
    }

笔记:

如果在此之后您想以“一种 Java Script 方式”查询该 html 正文,我建议您探索 jsoup 库.. 非常直观且易于使用:

Document jsoup = Jsoup.parse(body);

Elements tds = jsoup.getElementsByTag("td");
Elements ps = tds.get(0).getElementsByTag("p");

我希望这有帮助 :-)

于 2019-07-16T19:51:33.563 回答