电子邮件 Outlook 获取正文

huangapple go评论85阅读模式
英文:

Email outlook getting body

问题

我正在使用Microsoft Outlook。在Outlook中,我会收到大量的邮件。我的任务是阅读Outlook邮件并将其中的数据存储在一个数组或任何文件中。我能够阅读邮件正文,其中包括文本和图像。我能够阅读邮件中的文本,但如果邮件中有图像,我无法阅读图像。我正在使用Python。请告诉我如何从正文中获取图像和文本。

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print(body_content)
英文:

I am using Microsoft outlook.In outlook I will be having plenty of mails.My task is to read the outlook mail and store that data in an array or any file.I am able to read body of the mail which consists of both text and images. I am able to read the text in the mail but if there is any images in the mail I can't able to read the images. I am using python. Please say me how to get images and text from that body.

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print(body_content)

答案1

得分: 1

Body 属性返回一个表示消息正文的纯文本字符串。相反,您可以使用以下方式:

  1. HTMLBody 属性 - 表示指定项目的 HTML 正文的字符串。
  2. Word 编辑器 - 显示的消息的 Microsoft Word 文档对象模型。Inspector 类的 WordEditor 属性返回 Word 对象模型中的 Document 类的实例,您可以使用它来设置消息正文。
    您可以在 第 17 章:处理项目正文 文章中了解更多有关所有这些方式的信息。选择如何处理消息正文取决于您。

您可以在消息正文中找到所有 img 元素。请注意,嵌入的图像作为附件存储在消息中。要查找它们,您可以使用以下代码(C#):

Outlook.MailItem mailItem;

foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
    bool attachmentIsInline = false;
    string fileName = attachment.FileName;

    if (mailItem.HTMLBody.Contains(fileName))
    {
        attachmentIsInline = true;
    }
}

基本上,您只需要找出消息正文是否包含附加文件的名称。查看以下线程以获取更多信息:

英文:

The Body property returns a plain text string that represents the message body. Instead, you can use the following ways:

  1. The HTMLBody property - a string representing the HTML body of the specified item.
  2. The Word editor - the Microsoft Word Document Object Model of the message being displayed. The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model which you can use to set up the message body.
    You can read more about all these ways in the Chapter 17: Working with Item Bodies article. It is up to you which way is to choose to deal with the message body.

You can find all img elements in the message body. Be aware, embedded images are stored as attachments in the message. To find them you can use the following code (C#):

Outlook.MailtItem mailItem;

foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
    bool attachmentIsInline = false;
    string fileName = attachment.FileName;

    if (mailItem.HTMLBody.Contains(fileName))
    {
        attachmentIsInline = true;
    }
}

Basically, you just need to find whether a message body contains an attached file's name. Take a look at the following threads for more information:

huangapple
  • 本文由 发表于 2020年1月6日 14:39:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定