Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display an image and add it as an attachment to an email

Tags:

c#

email

I am creating a System.Net.Mail message with an html body. I attach a jpeg image (from a byte aray) of the customer's signature to the email and send to a service center. The service center wanted to see the signature without having to open the attachment so I changed the html and added the image again as a LinkedResource. The problem is, now the attachment is larger than before and attempting to view the attachment displays an error. If I save the attachment and open the file in a hex editor the entire file is empty. I can comment the code that adds the linked resource and the attachment works again. How can I include the inline image, and the attachment?

This is the tag I embed to image in: <p><img src=cid:CustomerSignature /></p>

This is my code:

System.IO.MemoryStream ms = new System.IO.MemoryStream(insuranceClaim.Signature, 0, insuranceClaim.Signature.Length);
ms.Position = 0;
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
contentType.Name = "signature.jpg";
System.Net.Mail.Attachment imageAttachment = new System.Net.Mail.Attachment(ms, contentType);
System.Net.Mime.ContentDisposition disposition = imageAttachment.ContentDisposition;
mailMessage.Attachments.Add(imageAttachment);
System.Net.Mail.LinkedResource signature = new System.Net.Mail.LinkedResource(ms, new System.Net.Mime.ContentType("image/jpeg"));
signature.ContentId = "CustomerSignature";
System.Net.Mail.AlternateView aView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailMessage.Body, new System.Net.Mime.ContentType("text/html"));
aView.LinkedResources.Add(signature);
mailMessage.AlternateViews.Add(aView);
like image 573
jac Avatar asked Oct 08 '12 19:10

jac


People also ask

How do I embed a photo in an email instead of as an attachment?

Insert a picture into the body of an email messagePosition your cursor where you want the image in your message. Select Insert > Pictures. Browse your computer or online file locations for the picture you want to insert. Select the picture, then select Insert.

How do I get an attachment to show in the body of an email?

If you want to always insert attachment in email body while editing email, you can click File > Options > Mail and select Rich Text in the drop down list of Compose message in this format in Compose message section.


1 Answers

I don't like answering my own questions, but if someone else makes the same mistake I did, here is the solution. I created a new MemoryStream with the image data, and tried to share the stream between the attachment, and the LinkedResource. Once I created 2 MemoryStreams it worked.

System.IO.MemoryStream ms = new System.IO.MemoryStream(insuranceClaim.Signature, 0, insuranceClaim.Signature.Length);
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
contentType.Name = "signature.jpg";
System.Net.Mail.Attachment imageAttachment = new System.Net.Mail.Attachment(ms, contentType);
mailMessage.Attachments.Add(imageAttachment);

System.IO.MemoryStream embeddedMs = new System.IO.MemoryStream(insuranceClaim.Signature, 0, insuranceClaim.Signature.Length);
System.Net.Mail.LinkedResource signature = new System.Net.Mail.LinkedResource(embeddedMs, new System.Net.Mime.ContentType("image/jpeg"));
signature.ContentId = "CustomerSignature";
System.Net.Mail.AlternateView aView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailMessage.Body, new System.Net.Mime.ContentType("text/html"));
aView.LinkedResources.Add(signature);
mailMessage.AlternateViews.Add(aView);
like image 133
jac Avatar answered Oct 08 '22 22:10

jac