This project has moved. For the latest updates, please go here.

Outlook, attach inline image from local resource.

Sep 30, 2014 at 9:16 PM
Hello everyone.

I am using C# with VS 2013. I have some images saved as resources (MyProject.Properties.Resources.SomeImage, as a System.Drawing.Bitmap), for email template generation. I was wondering if it was possible to add these images to a MailItem without having to save them to a temporary file on my computer, then upload the file again into an attachment (giving its path), then delete the temporary file once the email has been sent.

Is it possible to use base64 encoding, and reference it with a cid or something similar?

Could somebody post a short (2-3 line) code example?

I've been struggling with this all day, any help would be greatly appreciated.
Coordinator
Sep 30, 2014 at 10:08 PM
you want to use an image to a contact/mail item?

let me know what you try do so far(in code) to pin point the problem
please explain in detail what do you want. change the image for a contact? just add a attachmen for a mailt. its not clearly for me. please help me to help you.
i need an error message and code, this is my begin... "works not" is not helpful for me.

*sebastian
Sep 30, 2014 at 10:40 PM
Hi Sebastian,

Sorry if I wasn't very clear. What I want to do is really simple.

I have a System.Drawing.Bitmap object. I want to attach it to a MailItem, and set its content id to some value.

Example of what I would like to do is something like this:
// I have a variable called img that is of type System.Drawing.Bitmap

var att = mailItem.Attachments.Add( /* Add img here somehow? */);
att.SetSomeProperty("someId"); // Set the cid of att somehow?
mailItem.HTMLBody = "<img src='cid:someId' />";
I'm not getting a specific error, I just have no idea how to do this. Can I attach a System.Net.Mail.LinkedResource, or a Stream somehow?

I hope this clarifies what I'm trying to do.

Thanks for taking the time to answer my question. :)
Oct 1, 2014 at 6:15 PM
Update:

This isn't the ideal solution, because I won't be able to guarantee that the client always has write access, but I wrote an extension method:
        public static string AttachInlineImage(this MailItem mail, Image img) {
            string fn = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".bmp";
            img.Save(fn);
            var att = mail.Attachments.Add(fn, OlAttachmentType.olByValue, mail.Body.Length, Type.Missing);
            System.IO.File.Delete(fn);
            return att.FileName;
        }
This will allow me to add an inline image by doing the following:
mailItem.HTMLBody += "<img src='cid:" + mailItem.AttachInlineImage((Image)App.Properties.Resources.SomeImage) + "' />";