none
How to copy an Outlook mail item with inbedded attachments RRS feed

  • Question

  • Hi,

    I am creating a separate email for every address in To of an Outlook mail item.

    I do this an OutlookItemSend event.

     

    Dim SentEMail As Microsoft.Office.Interop.Outlook.MailItem = TryCast(Sender, Microsoft.Office.Interop.Outlook.MailItem)

     

    The body format of SentEMail is Outlook.OlBodyFormat.olFormatHTML and it contains embedded attachments.

    e.g.

    <img width=1 height=50 id="Picture_x0020_1" src="cid:image001.gif@01CCAF50.F7840290" alt="Description: Description: http://www.mywebsite.com/images/bar50.gif">

     

    I get the content ID from this tag in variable MyContentID

    Dim MyContentID As String = "image001.gif@01CCAF50.F7840290"

     

    I get the Uri from this tag in variable MyUri

    Dim MyUri As New System.Uri("http://www.mywebsite.be/images/bar50.gif")

     

    I saved the attachment like this:

    Dim sPath As String = "some place on a server or HD\image001.gif"

    SentEMail.Attachments.Item(1).SaveAsFile(sPath)

     

    For the construction of a separate email based on SentEMail I do it like this:

     

    Dim anEmail As New MailMessage

    With anEmail

    'Code for adding the From, To, CC and BCC addresses, Subject, …

    '…

    'Code for adding the body

    Dim avHTMLBody As AlternateView = AlternateView.CreateAlternateViewFromString(sHTMLBody, Nothing, MediaTypeNames.Text.Html)

    Dim lrImage As LinkedResource = New LinkedResource(File.OpenRead(sPath), "text/html")

    lrImage.ContentId = MyContentID

    lrImage.ContentLink = MyUri

    avHTMLBody.LinkedResources.Add(lrImage)

    .AlternateViews.Add(avHTMLBody)

    End With

    'Code for sending

    Dim oSMtPClient As SmtpClient = New SmtpClient("myMailServer")

    oSMtPClient.Send(anEmail)

     

    The emails are properly sent except the embedded attachments are not there.

     

    I tried also to add the linked source like this:

     

    Dim lrImage As LinkedResource = New LinkedResource(sFilePath)

     

    That doesn’t work either.

    Anybody has an idea how to solve this?

     

    Thanks

     

    RuWel


    • Edited by RuWel Thursday, December 1, 2011 3:47 PM
    Thursday, December 1, 2011 10:43 AM

Answers

  • Is Content-ID MIME header correctly set when you look at the MIME source of the mesage?
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    • Marked as answer by RuWel Thursday, December 1, 2011 3:22 PM
    Thursday, December 1, 2011 2:23 PM
  • Hi Dmitry, thanks

    Indeed, the ContentID was one character too short. Didn't see it right away.

    I also dropped the Uri in the code.

    So this works:

    Dim SentEMail As Microsoft.Office.Interop.Outlook.MailItem = TryCast(Sender, Microsoft.Office.Interop.Outlook.MailItem)

     

    Dim MyContentID As String = "image001.gif@01CCAF50.F7840290"

     

    Dim sPath As String = "some place on a server or HD\image001.gif"

    SentEMail.Attachments.Item(1).SaveAsFile(sPath)

     

    Dim anEmail As New MailMessage

    With anEmail

    'Code for adding the From, To, CC and BCC addresses, Subject, …

    '…

    'Code for adding the body

    Dim avHTMLBody As AlternateView = AlternateView.CreateAlternateViewFromString(sHTMLBody, Nothing, MediaTypeNames.Text.Html)

    Dim lrImage As LinkedResource = New LinkedResource(sFilePath)

    lrImage.ContentId = MyContentID

    avHTMLBody.LinkedResources.Add(lrImage)

    .AlternateViews.Add(avHTMLBody)

    End With

    'Code for sending

    Dim oSMtPClient As SmtpClient = New SmtpClient("myMailServer")

    oSMtPClient.Send(anEmail)

     

     

     

    RuWel


    • Edited by RuWel Thursday, December 1, 2011 3:51 PM
    • Marked as answer by RuWel Thursday, December 1, 2011 3:54 PM
    Thursday, December 1, 2011 3:24 PM

All replies

  • Is Content-ID MIME header correctly set when you look at the MIME source of the mesage?
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    • Marked as answer by RuWel Thursday, December 1, 2011 3:22 PM
    Thursday, December 1, 2011 2:23 PM
  • Hi Dmitry, thanks

    Indeed, the ContentID was one character too short. Didn't see it right away.

    I also dropped the Uri in the code.

    So this works:

    Dim SentEMail As Microsoft.Office.Interop.Outlook.MailItem = TryCast(Sender, Microsoft.Office.Interop.Outlook.MailItem)

     

    Dim MyContentID As String = "image001.gif@01CCAF50.F7840290"

     

    Dim sPath As String = "some place on a server or HD\image001.gif"

    SentEMail.Attachments.Item(1).SaveAsFile(sPath)

     

    Dim anEmail As New MailMessage

    With anEmail

    'Code for adding the From, To, CC and BCC addresses, Subject, …

    '…

    'Code for adding the body

    Dim avHTMLBody As AlternateView = AlternateView.CreateAlternateViewFromString(sHTMLBody, Nothing, MediaTypeNames.Text.Html)

    Dim lrImage As LinkedResource = New LinkedResource(sFilePath)

    lrImage.ContentId = MyContentID

    avHTMLBody.LinkedResources.Add(lrImage)

    .AlternateViews.Add(avHTMLBody)

    End With

    'Code for sending

    Dim oSMtPClient As SmtpClient = New SmtpClient("myMailServer")

    oSMtPClient.Send(anEmail)

     

     

     

    RuWel


    • Edited by RuWel Thursday, December 1, 2011 3:51 PM
    • Marked as answer by RuWel Thursday, December 1, 2011 3:54 PM
    Thursday, December 1, 2011 3:24 PM