Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
I will try to explain this as well as possible.

I need to send emails with images.
The whole email is constructed as an html string.
The problem is the image is not being displayed inline.
I am using Microsoft.Exchange.Webservices,Data.EmailMessage object to send the email

using Microsoft.Exchange.WebServices.Data;

This is what I have so far
C#
private void SendMail()
{
 //the exchange setting are set in a different method.
 EmailMessage _message = new EmailMessage(this.m_ExchangeService); _message.Subject = "TheSubject";

string Body = "<html><body><div class="WordSection1">Heloo<br>Please see image 1<br><img width="346" height="236" id="Picture_x0020_2" src="cid:image001.png@01D0624F.B3A11080"><br></div></body></html>";

_message.Body = new MessageBody(_BodyType.HTML, TheBody);
 //My byte array is too big to paste here.
byte[] array = null;

//add the attachment to the mail
_message.Attachments.AddFileAttachment("image001.png",array);
_message.Save();
PropertySet _propertySet = new PropertySet(Microsoft.Exchange.WebServices.Data.EmailMessageSchema.MimeContent);
_message.Load(_propertySet);
_message.MimeContent.Content = _message.MimeContent.Content;
_message.Update(ConflictResolutionMode.AutoResolve);
_message.Send();
}

Please help
Thanks
Posted

Here is the background: the images in HTML e-mail should be referenced using the special URI scheme "cid:" (Content-id). Please see:
http://en.wikipedia.org/wiki/URI_scheme[^],
http://tools.ietf.org/html/rfc2392[^].

The e-mail should be a multipart e-mail. Some parts could be HTML documents and some — base64-encoded images. To reference an image in one part in HTML part, HTML code <img> tag should reference the image as cid:<content-id></content-id>, where content-id should be the same as LinkedResource.ContentId of the image part to be referenced and thus rendered as inserted in HTML document.

This example illustrates using this technique for creation of e-mail with C# and .NET:
http://kosiara87.blogspot.com/2011/04/c-sending-mail-with-embedded-image.html[^].

(This is a pretty dumb code; it uses a placeholder "@@IMAGE@@" in the HTML code to be replaced with actual value for content id using string.Replace, instead of, say, using string.Format. It has nothing to do with the idea of embedding the images.)
I found this example using this query: http://bit.ly/zpvER5[^].

If you need more advanced example, try to find some more; it returned just about 227,000 results.

—SA
 
Share this answer
 
Comments
ASJ_SA 23-Mar-15 10:32am    
Thanks a lot for the explanation and the sample.
will definitely try it out
Sergey Alexandrovich Kryukov 23-Mar-15 10:35am    
Please do. And please don't forget to accept the answer formally when you realize it's the way to go.
In all cases, your follow-up questions will be welcome.
—SA
Thanks SA for guiding me in the right direction
I eventually managed to solve it. My main issue was that I was using the Microsoft.Exchange.WebServices.Data class

I just want to put up the correct code in case anyone else with similar issue ends up here:
Refer --> https://msdn.microsoft.com/en-us/library/office/hh532564(v=exchg.80).aspx

Ensure ur image name in ur string is same as the one u are setting it to.
My problem was my image name was - src="cid:image001.png@01D0624F.B3A11080"
and I was setting it to just image001.png.
once I cleaned up my string it displayed without issues
C#
if(Attachments != null )
                    {
                        for (int i = 0; i <= Attachments.Count - 1; i++ )
                        {                           
                            _message.Attachments.AddFileAttachment(Attachments[i].FileName, Attachments[i].FileData);
                            if (Attachments[i].FileName.Contains(".png") || Attachments[i].FileName.Contains(".jpg") || Attachments[i].FileName.Contains(".jpeg"))
                            {
                                _message.Attachments[i].IsInline = true;
                                _message.Attachments[i].ContentId = Attachments[i].FileName;
                            }
                        }
                    }


Attachment is a List<mailattachment> where MailAttachment is a class with properties
C#
<pre lang="cs">private string m_FileName = string.Empty;
        private byte[] m_FileData = null;

We populate these from the DB where the file data is saved

Hope this will help someone out there

Thanks again to all who guided me
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900