WPF C# Outlook Mailitem insert image in email body

Asked

Viewed 178 times

0

I am using the code below and can insert an image into the body of the email. How would you add more images? Is it necessary to create multiple "imageCid"? The code below only shows the last image.

Microsoft.Office.Interop.Outlook.Attachment attachment = msg.Attachments.Add(@"D:\Users\chart.jpeg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
attachment = msg.Attachments.Add(@"D:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
attachment = msg.Attachments.Add(@"D:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
attachment = msg.Attachments.Add(@"D:\Users\Public\Pictures\Sample Pictures\Penguins.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");

string imageCid1 = "image001.jpg@123";
string imageCid2 = "image002.jpg@123";
string imageCid3 = "image003.jpg@123";
string imageCid4 = "image004.jpg@123";

attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid1);
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid2);
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid3);
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid4);

msg.HTMLBody = String.Format("<body><img src=\"cid:{0}\"><br/><img src=\"cid:{1}\"><br/><img src=\"cid:{2}\"><br/><img src=\"cid:{3}\"></body>", imageCid1, imageCid2, imageCid3, imageCid4);

1 answer

0

I was overwriting the file.

Microsoft.Office.Interop.Outlook.Attachment attachment1 = msg.Attachments.Add(@"D:\Users\chart.jpeg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
Microsoft.Office.Interop.Outlook.Attachment attachment2 = msg.Attachments.Add(@"D:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
Microsoft.Office.Interop.Outlook.Attachment attachment3 = msg.Attachments.Add(@"D:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
Microsoft.Office.Interop.Outlook.Attachment attachment4 = msg.Attachments.Add(@"D:\Users\Public\Pictures\Sample Pictures\Penguins.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");

string imageCid1 = "image001.jpg@123";
string imageCid2 = "image002.jpg@123";
string imageCid3 = "image003.jpg@123";
string imageCid4 = "image004.jpg@123";

attachment1.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid1);
attachment2.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid2);
attachment3.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid3);
attachment4.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid4);

msg.HTMLBody = String.Format("<body><img src=\"cid:{0}\"><br/><img src=\"cid:{1}\"><br/><img src=\"cid:{2}\"><br/><img src=\"cid:{3}\"></body>", imageCid1, imageCid2, imageCid3, imageCid4);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.