Send an Email in C# with Inline attachments
Visits: 103034
Monday, August 28, 2006

This is a SMTP client implementation in C# permits to send Inline attachment in your email messages (using the MailMessage class of System.Net.Mail namespace is possible only to send normal attachments).

To show an image inside the body of the email without link to external site is necessary to add the attachment in the header of message and call it from the HTML section of the body. Using the MailMessage and the SmtpClient classes is not enough set the Inline property to True to show the image:

// creating the attachment
    System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"c:\\test.png");
    inline.ContentDisposition.Inline = true;
    // sending the message
    MailMessage email = new MailMessage();
    // set the information of the message (subject, body ecc...)
    // send the message
    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");


In this way, the sent message misses the right Content-Type section in the header (to have an Inline attachment is necessary the "multipart/related" Content-Type). To resolve this situation is possible to by-pass the SmtpClient and use one custom class for SMTP client. This class provides to connect/communicate with the SMTP server and to add the right Content-Type to the message.

using System;    
    using System.Collections.Generic;    
    using System.Text;    
    using System.IO;    
    using System.Net.Mail;    
    using System.Net.Mime;    
    namespace devSmtp {    
    class Program {    
    static void Main(string[] args)
    // This example show you how to send one email message with an INLINE attachment.
    // You can use this example also without the support of CDO or other type of SmtpClient.
    // creating the email message
    MailMessage email = new MailMessage("test@yourdomain.something", "test@yourdomain.something");
    // information
    email.Subject = "INLINE attachment TEST";
    email.IsBodyHtml = true;
    email.Body = "<div style=\"font-family:Arial\">This is an INLINE attachment:<br /><br /><img src=\"@@IMAGE@@\" alt=\"\"><br /><br />Thanks for downloading this example.</div>";
    // generate the contentID string using the datetime
    string contentID = Path.GetFileName(attachmentPath).Replace(".", "") + "@zofm";
    // create the INLINE attachment
    string attachmentPath = Environment.CurrentDirectory + @"\test.png";
    Attachment inline = new Attachment(attachmentPath);
    inline.ContentDisposition.Inline = true;
    inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
    inline.ContentId = contentID;
    inline.ContentType.MediaType = "image/png";
    inline.ContentType.Name = Path.GetFileName(attachmentPath);
    // replace the tag with the correct content ID
    email.Body = email.Body.Replace("@@IMAGE@@", "cid:" + contentID);
    // sending the email with the SmtpDirect class (not using the System.Net.Mail.SmtpClient
    SmtpDirect smtp = new SmtpDirect("localhost");

Click here to download the SMTP class for Inline attachments Click here to download the example of this article


Well, this is for sure a solution, but I wouldn't recommend it.
Microsoft has actually added a lot of features in the 2.0 framework while changing System.Web.Mail to System.Net.Mail, and there are types and methods that allows you to send e-mails with embedded/inline attachments.

Please refer to for more details.
Written by Troels Thomsen - Friday, October 20, 2006 at 5:25 PM

I want to send a outlook calendar request even when i dont have outlook on my machin. I tried to send a vacalender file , its going as attachment, not as a calender request. Tell me how can do the same , so that i can able to send the meeting request as same as we send outlook.
Written by Pravin - Wednesday, November 15, 2006 at 7:48 AM

Nice work!
Written by Nemo - Saturday, November 18, 2006 at 8:56 PM

I do not agree with Troels Thomsen, this sample uses .NET 2.0 classes in a nice way. works good for me, I like it much better than Troels ThomsenĀ“s sample.
Written by Markus Fischer - Thursday, March 08, 2007 at 6:26 PM

Thank you very much (o "Grazie molte", visto che sei italiano come me ;) ) for this great example! It will be very useful to me, and, in the future, i'll try to implement simple smtp authentication.
Written by Korgull - Friday, March 23, 2007 at 5:23 PM

Can anyone tell me why if you change the literal in email.Subject = "INLINE attachment TEST"; to anything else this why this stops working?
Written by Kirk - Friday, July 27, 2007 at 12:04 PM

Can anyone tell me why if you change the literal in email.Subject = "INLINE attachment TEST"; to anything else why this stops working? Oh and the example presented at doesn't work at all. Sorry about the quasi dbl post I just used a lot of things from the other site noted with little luck.
Written by Kirk - Friday, July 27, 2007 at 12:23 PM

please disregard my last post. It was working find just going into my junk email folder, wow hehe.
Written by Kirk - Friday, July 27, 2007 at 5:31 PM

Hi Thanks a lot for this example! It works fine for me. Now I would like to send a html document instead (a word documet that I have saved as html). Can anybode give me some hints. please? Best regrads
Written by Kersta - Thursday, November 08, 2007 at 5:20 PM

Thanks for the valuable input. It really helped me to add the logo in the mail.
Written by Yogesh Murgude - Wednesday, January 30, 2008 at 2:43 PM

This Example saved me. System.Net.Mail doesnt wrap the email correctly when using LinkedResources and AlternateViews. It creates a multipart/mixed email but it is malformed. This example fixes that problem. Thank You
Written by Chris - Monday, February 04, 2008 at 6:51 AM

I wonder why the size of the mail of this example bigger that the size of a mail in Word mail merge. I used a sample image for both ways. One (A) manually send using Word mail merge, one (B) used this code. But the size of the mails were significantly different. A=1/4B So I want to know what shoud we do to reduce the size in coding.
Written by Boom - Thursday, April 10, 2008 at 5:17 PM

I have been migrating from System.Web.Mail to System.Net.Mail this afternoon, and the code in your example has been of great help. Many thanks. Mike.
Written by Mike Irving - Friday, May 02, 2008 at 6:23 PM

It doesn't work me. It doesn't have the ability to connect to an SMTP server that requires authentication and SSL or I missing something?
Written by Kaz - Tuesday, May 20, 2008 at 12:03 PM

Excellent work.. thanks a lot.. it saved lot of time for me..
Written by periyasamy - Monday, August 18, 2008 at 3:12 PM

That is exacly what I was looking at :) Thanks a lot!
Written by biosys - Wednesday, December 10, 2008 at 12:46 PM

Hi, I tried this example. I just changed the file path, and MediaType as "image/mhtml". But it doesn't work for me. Here is my there any thing missing Dim filename As String "E:\test\MyReport1.mhtml" Dim email As New System.Net.Mail.MailMessage("from", "to") email.Subject = "INLINE attachment TEST" email.IsBodyHtml = True email.Body = "
This is an INLINE attachment:

Thanks for downloading this example.
" ' generate the contentID string using the datetime Dim contentID As String = Path.GetFileName(filename).Replace(".", "") & "@zofm" ' create the INLINE attachment Dim attachmentPath As String = filename Dim inline As New Attachment(attachmentPath) inline.ContentDisposition.Inline = True inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline inline.ContentId = contentID inline.ContentType.MediaType = "image/mhtml" inline.ContentType.Name = Path.GetFileName(attachmentPath) email.Attachments.Add(inline) ' replace the tag with the correct content ID email.Body = email.Body.Replace("@@IMAGE@@", "cid:" & contentID) 'send email
Written by Eva - Monday, January 26, 2009 at 11:47 PM

nice example
Written by jay - Tuesday, March 17, 2009 at 4:06 PM

O Bad Luck! it's show me '500-SMTP'
Written by Frankey - Friday, April 03, 2009 at 7:28 AM

Hi, what is the assembly for SmtpDirect I didn't find it in .NET2 ? Thanks
Written by Emmanuel - Thursday, April 09, 2009 at 2:18 AM


You can find it in the ZIP file at this address:
Written by ZofM - Thursday, April 09, 2009 at 9:53 AM

cool. This works. Can anyone give me pointer for embeding css file also?
Written by Hament Verma - Tuesday, June 02, 2009 at 5:57 AM

So, this is work only with your smtp library? Pity bad.
Written by Hyperborean - Monday, June 08, 2009 at 10:00 AM

I mean pity bad for MS ;)
Written by Hyperborean - Monday, June 08, 2009 at 10:14 AM

ZofM, do you know how to send meeting request in html format with embedded images? I was try to do this, but unfortunately.
Written by Hyperborean - Monday, June 08, 2009 at 2:53 PM

if I want to send mail by different mail ID(not mine) then I use the below code. but i send mail it will add the ":**SPAM**:" in subject How will i remove this spam MailAddress strto =new MailAddress(emailTo); msg.To.Add(strto); msg.From = new MailAddress(emailFrom,sendername); msg.Sender=new MailAddress emailFrom,sendername); msg.Headers.Add("Reply-To", replyTo); //and for authentication SmtpClient smtp = new SmtpClient(smtpServer); //Check Authentication System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(userEmail,password); smtp.UseDefaultCredentials = false; smtp.Credentials = SMTPUserInfo; smtp.Send(msg); when i user my email id in msg.From = new MailAddress(userEmail,username); then its working Fine
Written by poonam - Tuesday, August 11, 2009 at 9:54 AM

You should have a look at: ... and remember if it still fails - change your mail.Priority to High objMail.Priority = System.Web.Mail.MailPriority.High this has never failed me :-)
Written by chire - Friday, August 28, 2009 at 9:17 PM

nice example really help me :) thanks a lot !
Written by controlsys - Tuesday, October 06, 2009 at 10:26 AM

Can anyone help me how do I download an Inline attachment, which is HTML file??
Written by Tsovinar Hovhannisyan - Tuesday, January 26, 2010 at 10:04 AM

Hi, I needed a help with the code for, verifying if an email id exists or not, but without sending mail. Can you help me out with this? Thanks.
Written by kummi - Saturday, February 06, 2010 at 8:45 PM

I'm not sure this works well with email hosts requiring credentials and SSL (e.g., gmail).
Written by Brent - Wednesday, May 05, 2010 at 11:26 PM

Post new comment

Remember my information:
Code verification: Password verification


-- Comment preview --

Versione italiana Versione italiana


Ajax (2)
ASP.NET (11)
C# Code (4)
IIS (1)
Silverlight (1)
Sql Server 2000 (1)
Varie (4)
Visual Studio (4)


April 2009 (1)
May 2008 (1)
April 2008 (1)
January 2008 (4)
December 2007 (1)
May 2007 (1)
February 2007 (1)
December 2006 (3)
October 2006 (1)
September 2006 (3)
August 2006 (1)
June 2006 (1)

ABOUT ME [curriculum]

Curriculum ZofM