VSTO Outlook Insert Image MailItem

I need to embed the image as part of the email after the User Signature, and not at the end of the email, because if I send a large email reply, the embedded image will be at the bottom of the email chain

  • How to insert an image as part of email content (Not a link to an external image)?
  • How to add this image after user signature?

I work with VSTO, VS2008 Fwk3.5 and Outlook 2007

Here is my code:

public partial class ThisAddIn
{
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
    }

    private void Application_ItemSend(object Item, ref bool Cancel)
    {
        if (Item is Outlook.MailItem)
        {
            Outlook.MailItem mailMessage = (Outlook.MailItem)Item;
            //do something to add the image after the signature
        }
    }
+4
source share
1 answer

Finally, I solved the problem with this:

private void SendFormatted(Outlook.MailItem mail)
{
    if (!string.IsNullOrEmpty(mail.HTMLBody) && mail.HTMLBody.ToLower().Contains("</body>"))
    {
        //Get Image + Link
        string imagePath = @"D:\\Media\Imagenes\100MSDCF\DSC00632.JPG";
        object linkAddress = "http://www.pentavida.cl";

        //CONTENT-ID
        const string SchemaPR_ATTACH_CONTENT_ID = @"http://schemas.microsoft.com/mapi/proptag/0x3712001E";
        string contentID = Guid.NewGuid().ToString();

        //Attach image
        mail.Attachments.Add(imagePath, Outlook.OlAttachmentType.olByValue, mail.Body.Length, Type.Missing);
        mail.Attachments[mail.Attachments.Count].PropertyAccessor.SetProperties(SchemaPR_ATTACH_CONTENT_ID, contentID);

        //Create and add banner
        string banner = string.Format(@"<br/><a href=""{0}"" ><img src=""cid:{1}"" ></a></body>", linkAddress, contentID);
        mail.HTMLBody = mail.HTMLBody.Replace("</body>", banner);

        mail.Save();
    }

}
+9

All Articles