How to extract Email Message Body Images

Hi,

I am using Aspose email component. I want to extract email message body images.

Please can you send me the code for doing this.

For your refernce i have attached the .msg file.

Attachment not supporting the .msg file hence i renamed the extension to .zip. Please rename the msg file extension .zip to .msg and try this attached file and send me the sample code.

Thanks

P. Saravanan.

Hi Saravanan,


Thanks for writing to Aspose.Email support team.

Could you please give a try to the following code which extracts all the images from the message body?

static void ExtractInlineImages()
{
int count;
string dir = @“D:\Aspose”;
var message = MapiMessage.FromFile(dir + “Sample2.msg”);
var attachments = message.Attachments;
count = attachments.Count;
Console.WriteLine(“Total attachments " + count);
for (int i = 0; i < attachments.Count; i++)
{
var attachment = attachments[i];
if (IsInlineAttachment(attachment, message.BodyType))
{
Console.WriteLine(attachment.LongFileName + " is inline attachment”);
attachment.Save(dir + “Output\” + attachment.LongFileName);
}
}
}

static bool IsInlineAttachment(MapiAttachment att, BodyContentType messageBodyType)
{
switch (messageBodyType)
{
case BodyContentType.PlainText:
// ignore indications for plain text messages
return false;
case BodyContentType.Html:
// check the PidTagAttachFlags property
if (att.Properties.Contains(0x37140003))
{
long? attachFlagsValue = att.GetPropertyLong(0x37140003);
if (attachFlagsValue != null && (attachFlagsValue & 0x00000004) == 0x00000004)
{
// check PidTagAttachContentId property
if (att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID) ||
att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID_W))
{
return true;
}

// check PidTagAttachContentId property
if (att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID) ||
att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID_W))
{
return true;
}
// check PidTagAttachContentLocation property
if (att.Properties.Contains(0x3713001E) ||
att.Properties.Contains(0x3713001F))
{
return true;
}
}
}
else if ((att.Properties.Contains(0x3716001F) && att.GetPropertyString(0x3716001F) == “inline”)
|| (att.Properties.Contains(0x3716001E) && att.GetPropertyString(0x3716001E) == “inline”))
{
return true;
}
return false;
case BodyContentType.Rtf:
// If the body is RTF, then all OLE attachments are inline attachments.
// OLE attachments have 0x00000006 for the value of the PidTagAttachMethod property
if (att.Properties.Contains(MapiPropertyTag.PR_ATTACH_METHOD))
{
return att.GetPropertyLong(MapiPropertyTag.PR_ATTACH_METHOD) == 0x00000006;
}
return false;
default:
throw new ArgumentOutOfRangeException();
}
}

Please feel free to write us back if you have any query related to Aspose.Email.

Hi Iqbal,

Thanks for the information!.

It works well. But It is not extracted all Message Body Images. Please can you try with my attached .zip file in this queue of the first message.

Please rename the attachment extension .zip to .msg and try to run your sample code.

I could not able to open all the .jpg files.

Please test and let me know your findings. So that I will implement those things in my code.

It is very urgnet. Please help me out.

Thanks in advacne.

P. Saravanan.

Hi Saravanan,


What issue are you facing? At my end, all the 10 inline images in the body of the email message are extracted properly and saved as JPGs that open fine when clicked. Could you please elaborate what you mean by “I could not able to open all the .jpg files”? We’ll try to assist you further as soon as possible.

Hi Iqbal,

Please use this code snipet and see the output of the images not extracting properly.

Please use the first attachment of this email chain. I have attached as a .zip extension. Please renmae to .msg of the attachment and try this sample code below

If there is any changes need to work properly of this below code please let me know.

In my sampel attachment is not working. Please try my attachmed sample .zip file.

static void ExtractInlineImages()

{

int count;

string dir = @"C:\Email\Test\";

var message = Aspose.Email.Outlook.MapiMessage.FromFile(@"C:\Email\InBox\FW Claim # 026172514Hartford # SBB403675.msg");

var attachments = message.Attachments;

count = attachments.Count;

Console.WriteLine("Total attachments " + count);

for (int i = 0; i < attachments.Count; i++)

{

var attachment = attachments[i];

if (IsInlineAttachment(attachment, message.BodyType))

{

Console.WriteLine(attachment.LongFileName + " is inline attachment");

attachment.Save(dir + attachment.LongFileName);

}

}

}

static bool IsInlineAttachment(Aspose.Email.Outlook.MapiAttachment att, Aspose.Email.Outlook.BodyContentType messageBodyType)

{

switch (messageBodyType)

{

case Aspose.Email.Outlook.BodyContentType.PlainText:

// ignore indications for plain text messages

return false;

case Aspose.Email.Outlook.BodyContentType.Html:

// check the PidTagAttachFlags property

if (att.Properties.Contains(0x37140003))

{

long? attachFlagsValue = att.GetPropertyLong(0x37140003);

if (attachFlagsValue != null && (attachFlagsValue & 0x00000004) == 0x00000004)

{

// check PidTagAttachContentId property

if (att.Properties.Contains(Aspose.Email.Outlook.MapiPropertyTag.PR_ATTACH_CONTENT_ID) ||

att.Properties.Contains(Aspose.Email.Outlook.MapiPropertyTag.PR_ATTACH_CONTENT_ID_W))

{

return true;

}

// check PidTagAttachContentId property

if (att.Properties.Contains(Aspose.Email.Outlook.MapiPropertyTag.PR_ATTACH_CONTENT_ID) ||

att.Properties.Contains(Aspose.Email.Outlook.MapiPropertyTag.PR_ATTACH_CONTENT_ID_W))

{

return true;

}

// check PidTagAttachContentLocation property

if (att.Properties.Contains(0x3713001E) ||

att.Properties.Contains(0x3713001F))

{

return true;

}

}

}

else if ((att.Properties.Contains(0x3716001F) && att.GetPropertyString(0x3716001F) == "inline")

|| (att.Properties.Contains(0x3716001E) && att.GetPropertyString(0x3716001E) == "inline"))

{

return true;

}

return false;

case Aspose.Email.Outlook.BodyContentType.Rtf:

// If the body is RTF, then all OLE attachments are inline attachments.

// OLE attachments have 0x00000006 for the value of the PidTagAttachMethod property

if (att.Properties.Contains(Aspose.Email.Outlook.MapiPropertyTag.PR_ATTACH_METHOD))

{

return att.GetPropertyLong(Aspose.Email.Outlook.MapiPropertyTag.PR_ATTACH_METHOD) == 0x00000006;

}

return false;

default:

throw new ArgumentOutOfRangeException();

}

}

Thanks

P. Saravanan.

Hi Saravanan,


I could not understand from your above message what is the actual issue that you are facing. I would request you to please have a look at the attached CSharp project that not only retrieves the images from the body of your message file but also those images that are regular attachments in your sample message file. If you are still facing issue, I would request you to please point us to the exact problem that you are facing so that we can guide you in appropriate direction.


Hi Iqbal,

You have sent some sample code right. Please run that code using my attachment (FW Claim # 026172514Hartford # SBB403675.zip) which is attached above in the first page of this chain.

In that attached .msg file Email Message body has 10 jpg images are there.

Rename the file FW Claim # 026172514Hartford # SBB403675.zip to FW Claim # 026172514Hartford # SBB403675.msg. and run your sample code and see the Output folder How the .jpg images are created. It is not creating properly most of the images, I could not able to open or view those images.

Message body images are extracting but i could not able to view or open some images.

Pleaes let me know why it is not opening. And let me know what i have to do with this.

Your sample code

static void ExtractInlineImages()
{
int count;
string dir = @"D:\Aspose\";
var message = MapiMessage.FromFile(dir + "Sample2.msg");
var attachments = message.Attachments;
count = attachments.Count;
Console.WriteLine("Total attachments " + count);
for (int i = 0; i < attachments.Count; i++)
{
var attachment = attachments[i];
if (IsInlineAttachment(attachment, message.BodyType))
{
Console.WriteLine(attachment.LongFileName + " is inline attachment");
attachment.Save(dir + "Output\\" + attachment.LongFileName);
}
}
}

static bool IsInlineAttachment(MapiAttachment att, BodyContentType messageBodyType)
{
switch (messageBodyType)
{
case BodyContentType.PlainText:
// ignore indications for plain text messages
return false;
case BodyContentType.Html:
// check the PidTagAttachFlags property
if (att.Properties.Contains(0x37140003))
{
long? attachFlagsValue = att.GetPropertyLong(0x37140003);
if (attachFlagsValue != null && (attachFlagsValue & 0x00000004) == 0x00000004)
{
// check PidTagAttachContentId property
if (att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID) ||
att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID_W))
{
return true;
}
// check PidTagAttachContentId property
if (att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID) ||
att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID_W))
{
return true;
}
// check PidTagAttachContentLocation property
if (att.Properties.Contains(0x3713001E) ||
att.Properties.Contains(0x3713001F))
{
return true;
}
}
}
else if ((att.Properties.Contains(0x3716001F) && att.GetPropertyString(0x3716001F) == "inline")
|| (att.Properties.Contains(0x3716001E) && att.GetPropertyString(0x3716001E) == "inline"))
{
return true;
}
return false;
case BodyContentType.Rtf:
// If the body is RTF, then all OLE attachments are inline attachments.
// OLE attachments have 0x00000006 for the value of the PidTagAttachMethod property
if (att.Properties.Contains(MapiPropertyTag.PR_ATTACH_METHOD))
{
return att.GetPropertyLong(MapiPropertyTag.PR_ATTACH_METHOD) == 0x00000006;
}
return false;
default:
throw new ArgumentOutOfRangeException();
}
}

Thanks

P. Saravanan.

Hi Saravanan,

In the attached sample project, I do have used your sample MSG file that you attached in the first post on this thread as .Zip file. I renamed your sample file and used it for extraction of the JPG images. You can find the extracted attached images in the bin/debug/Email_483524 folder and all the images open fine. If you want to extract only inline images, modify the ExtractInlineImages function as follow. Please open that project and you will see that it uses your sample MSG as input for extraction of images. Let us know if there is still some confusion.

static void ExtractInlineImages()
{
    int count;
    string dir = "EMAIL_483524\\";
    var message = MapiMessage.FromFile(dir + "FW+Claim+%23+026172514Hartford+%23+SBB403675.msg");
    var attachments = message.Attachments;
    count = attachments.Count;
    Console.WriteLine("Total attachments " + count);

    for (int i = 0; i < attachments.Count; i++)
    {
        var attachment = attachments[i];
        if (IsInlineAttachment(attachment, message.BodyType))
        {
            Console.WriteLine(attachment.LongFileName + " is inline attachment");
            attachment.Save(dir + attachment.LongFileName);
        }
        else
            Console.WriteLine(attachment.LongFileName + " is not inline attachment");
    }
}