1

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)
sai
  • 151
  • 1
  • 2
  • 10

1 Answers1

1

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:

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 1
    Thank you so much for providing me the info. But my request is can you please say me how to do it in **python**. – sai Jan 06 '20 at 09:59
  • 1
    The outlook object model is common for all programming languages. I've described the sequence of property and methods calls for your convenience. – Eugene Astafiev Jan 06 '20 at 10:01