Visual Studio Developer Center > Visual Studio Forums > Visual Studio Tools for Office > distinguish between inline images and other attachments in outlook message
Ask a questionAsk a question
 

Answerdistinguish between inline images and other attachments in outlook message

  • Monday, November 02, 2009 9:01 AMLuigi Trevisant Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi everybody,
    I'm writing an outlook plugin manipulating emails attachments.

    Now I've a problem with html email, 'cause in the attachments collection I see also the inline images of the message.
    Is there away to distinguish between "regular" attachments and inline images?

    Tks in advance,
    Luigi

Answers

  • Friday, November 06, 2009 3:12 AMJose Anton Bautista Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi again Luigi!

    This may be a bit of a stretch but how about using regular expressions?
    Other than those mentioned already, I can't think of any other way to determine if an attachment is inline.
    I also thought about using the Body property of the Outlook.MailItem object but it could also be possible
    that the file name of an inline image can be typed into the e-mail*:



    Outlook.MailItem mailItem;
    Outlook.Attachment attachment = mailItem.Attachments[1];
    bool isInline;

    if (mailItem.HtmlBody.Contains(attachment.FileName))
    {
        if (mailItem.Body.Contains(attachment.FileName))
        {
            // *
        }

        else
        {
            isInline = true;
        }
    }

    else
    {
        isInline = false;
    }

All Replies

  • Monday, November 02, 2009 9:52 AMJose Anton Bautista Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

    Hi Luigi!

    If an image attachment is inline, then the file name of that attachment will be present in the e-mail's markup.
    See the sample C# code below:



    Outlook.MailtItem mailItem;

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

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

  • Tuesday, November 03, 2009 8:59 AMLuigi Trevisant Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Jose,
    I already thought of a similar algorithm, but I encountered this use case that makes it fails.
    If in the body,for example, is  written "I attach the file Image.bmp", the algorithm fails, because "image.bmg" is treated as an online image and not as attachment.

    I hope I've been clear

    Thanks,
    Luigi

     

    Hi Luigi!

    If an image attachment is inline, then the file name of that attachment will be present in the e-mail's markup.
    See the sample C# code below:



    Outlook.MailtItem mailItem;

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

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


  • Wednesday, November 04, 2009 3:40 AMJose Anton Bautista Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi again Luigi!

    Have you tried treating the HTMLBody as an XML and searching for all <img> elements?
    The <img> element has a src attribute which you can check if it contains the file name of the attachment.
    If it does, the image attachment must be inline.

  • Wednesday, November 04, 2009 8:59 AMLuigi Trevisant Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi Jose,
    I also tried something similar, but I've been blocked by cid structure

    Look at this for example:
    <img width=642 height=159 id="Immagine_x0020_1"
    src="cid:image001.png@01CA385D.FE70A6B0">
    

  • Friday, November 06, 2009 3:12 AMJose Anton Bautista Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi again Luigi!

    This may be a bit of a stretch but how about using regular expressions?
    Other than those mentioned already, I can't think of any other way to determine if an attachment is inline.
    I also thought about using the Body property of the Outlook.MailItem object but it could also be possible
    that the file name of an inline image can be typed into the e-mail*:



    Outlook.MailItem mailItem;
    Outlook.Attachment attachment = mailItem.Attachments[1];
    bool isInline;

    if (mailItem.HtmlBody.Contains(attachment.FileName))
    {
        if (mailItem.Body.Contains(attachment.FileName))
        {
            // *
        }

        else
        {
            isInline = true;
        }
    }

    else
    {
        isInline = false;
    }