Outlook: Embed an image in outgoing mail

Add-in Express™ Support Service
That's what is more important than anything else

Outlook: Embed an image in outgoing mail
 
Andrey Yemelyanov




Posts: 70
Joined: 2009-04-21
Hi all!

We are using Addin Express for Office and .NET 2010 Professional. (don't know if it is relevant:))

I was wondering if you have a VB/C# example of how, using Extended MAPI in AddinExpress, to embed
an image in an outgoing message in Outlook. That is, how to programmatically set the following:

Content-Type: image/png
Content-Transfer-Encoding: base64
Content-ID: <image>
and also set image encoded in base64

Then we could embed an image by setting the following tag in HTMLBody of Mail object:
<img src="cid:image"/>

Can I do it without CDO or Redemption? Just with Addin Express...

Hopefully, you have some ready-made code snippets or maybe can just pitch in some ideas of the top of your heads.

/Andrey
Posted 04 Jun, 2010 03:45:46 Top
Eugene Astafiev


Guest


Hi Andrey,

Nope. We don't have ready-made code snippets.

You can try using the http://www.add-in-express.com/products/mapi-store-events.php.
Posted 04 Jun, 2010 06:47:27 Top
Andrey Yemelyanov




Posts: 70
Joined: 2009-04-21
Hi Eugene!

Here is what I am trying in my ItemSend event handler:

mailItem.Attachments.Add("c:\\linux.jpg", null, 1, "Attach");
mailItem.Save();
AddinExpress.MAPI.ADXMAPIPropertyTag[] tags = new AddinExpress.MAPI.ADXMAPIPropertyTag[]
{
AddinExpress.MAPI.ADXMAPIPropertyTag._PR_ATTACH_MIME_TAG,
AddinExpress.MAPI.ADXMAPIPropertyTag._PR_ATTACH_ENCODING,
AddinExpress.MAPI.ADXMAPIPropertyTag._PR_ATTACH_CONTENT_ID
};

MapiItem mapiItem = adxmapiStoreAccessor1.GetMapiItem(mailItem);
mapiItem.SetUseTNEF(false);

AddinExpress.MAPI.Attachment attachment = mapiItem.Attachments.[0];
System.Object[] values = new object[] { "image/png", "base64", "image" };
int[] hResults = attachment.SetProperties(tags, values);
mailItem.Save();
mailItem.HTMLBody = "<img src='cid:image'/>";

However, it seems like the mime headers are never set in the email. Am I doing something wrong?

/Andrey
Posted 04 Jun, 2010 10:19:10 Top
Andrey Yemelyanov




Posts: 70
Joined: 2009-04-21
By the by, I have tried setting MIME headers on the attachment outside ItemSend event, but still to no avail.
Essentially, the code above is my attempt at porting to MAPI Store Accessor API CDO code for embedding images
(available at http://www.outlookcode.com/d/code/htmlimg.htm)

Too bad it is not working... :(

Hope you can help me out here.

/Andrey
Posted 07 Jun, 2010 04:14:12 Top
Eugene Astafiev


Guest


Hi Andrey,

Please try using the MAPI Store Accessor outside the ItemSend event handler. Does it work now?
Posted 07 Jun, 2010 04:27:33 Top
Andrey Yemelyanov




Posts: 70
Joined: 2009-04-21
I did try to use it outside ItemSend handler. Did not work, unfortunately.
Posted 07 Jun, 2010 04:31:40 Top
Eugene Astafiev


Guest


Hi Andrey,

Could you show me the code?
Posted 07 Jun, 2010 04:55:29 Top
Andrey Yemelyanov




Posts: 70
Joined: 2009-04-21
Hi Eugene!

Sure, here comes the code...

This event handler is run when I click on a ribbon button in active inspector (creating new mail).
Then I click Send, the message is sent away, but the image is always a regular attachment, no
headers are set as requested.

void adxRibbonButton1_OnClick(object sender, AddinExpress.MSO.IRibbonControl control, bool pressed)
{
Object item = OutlookApp.ActiveInspector().CurrentItem;
Outlook.Attachment attach = null;
if (item is Outlook.MailItem)
{

Outlook.MailItem mailItem = (Outlook.MailItem)item;
attach=mailItem.Attachments.Add("c:\\linux.jpg", Outlook.OlAttachmentType.olByValue,
1, "linux.jpg");

mailItem.Save();

AddinExpress.MAPI.ADXMAPIPropertyTag[] tags = new AddinExpress.MAPI.ADXMAPIPropertyTag[]
{
AddinExpress.MAPI.ADXMAPIPropertyTag._PR_ATTACH_MIME_TAG,
AddinExpress.MAPI.ADXMAPIPropertyTag._PR_ATTACH_CONTENT_ID
};

MapiItem mapiItem = adxmapiStoreAccessor1.GetMapiItem(mailItem);
mapiItem.SetUseTNEF(false);

AddinExpress.MAPI.Attachment attachment = mapiItem.Attachments[0];
System.Object[] values = new object[] { "image/png", "<image>" };
int[] hResults = attachment.SetProperties(tags, values);

mailItem.Save();

attach = null;
attachment = null;
mapiItem = null;
WebstampMessageBox.Show(WindowStyle.Information, "Attachment set", null);
}
Marshal.ReleaseComObject(item);
}
Posted 07 Jun, 2010 05:00:12 Top
Eugene Astafiev


Guest


Hi Andrey,

First of all, you don't release COM objects properly. Please have a look at our http://www.add-in-express.com/creating-addins-blog/2008/10/30/releasing-office-objects-net/.

Did you try to debug? Did you check out the hResults array?
Posted 07 Jun, 2010 10:53:50 Top
Andrey Yemelyanov




Posts: 70
Joined: 2009-04-21
Hi Eugene!

Thanks for a tip. I'll give it a try and get back to you.

I did try to debug and the hResults array always contains zeros.

In fact, if, after setting desired properties, I invoke attachment.getProperty(AddinExpress.MAPI.ADXMAPIPropertyTag._PR_ATTACH_MIME_TAG) for example, then I get a correct value. But if in the same method after setting properties, I retrieve mapi item again and then do get property on its attachment, then I always get null in return, which indicates that the property was not set in the actual mail item.

This is what I am puzzled by.

/Andrey
Posted 08 Jun, 2010 00:57:22 Top