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);
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With