【问题标题】:Retrieve email/message body in html using Gmail API使用 Gmail API 在 html 中检索电子邮件/消息正文
【发布时间】:2014-08-17 04:07:42
【问题描述】:

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

我已经阅读了message.get 文档。尝试将format 参数更改为fullminimalraw。但这没有帮助。它返回邮件正文的纯文本。


格式值说明:

“full”:在payload字段中返回解析后的邮件内容,不使用raw字段。 (默认)

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

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


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

【问题讨论】:

  • 是的。它以 html 形式提供电子邮件正文。我使用的库是这个github.com/charlierguo/gmail。这可行,但只想知道 GMail API 是否提供此功能。

标签: python gmail-api


【解决方案1】:

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

var part = message.parts.filter(function(part) {
  return part.mimeType == 'text/html';
});
var html = urlSafeBase64Decode(part.body.data);

【讨论】:

  • 好的。在python中我得到的是msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))mime_msg = email.message_from_string(msg_str)。当我在 html 中呈现返回的消息 mime_msg 字符串时,它会显示纯文本。它甚至没有被格式化。你能帮忙吗?
  • @Kartik 我试过你的,但没有用..我可以打印出 message['sn-p'],但仍然无法获取电子邮件正文
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    这是完整的教程:

    1- 假设您已经创建了所有凭据 here

    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");
    

    我希望这会有所帮助:-)

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2017-03-07
      • 1970-01-01
      • 2020-02-05
      • 2018-07-28
      • 2017-11-01
      • 2020-09-27
      • 2015-01-02
      • 2015-12-24
      相关资源
      最近更新 更多