Click here to Skip to main content
Click here to Skip to main content
Go to top

Send an email with an embedded image in its body

, 8 Apr 2012
Rate this:
Please Sign up or sign in to vote.
How to send an email with image within its body and not as an attachment.

Introduction

The article is about how to send an email with an image within its body and not as an attachment (like a company's logo). The only possible solution is to format the body of the message as an "HTML" document. We will have to use the .NET classes LinkedResource and AlternateView as merely setting the src of the HTML <img> tag won't help.

Background

Before going through this article, we need to have a basic understanding of ASP.NET 2.0 (and ofcourse, HTML).

Using the code

First of all, we need to import the namespace System.Net.Mail. Then in the configSections of the web.config file, add the following:

<appSettings>
	<add key="SMTP" value="SMTP Address"/>  //like mail.gmail.com
	<add key="MailFrom" value="Mail From"/>
	<add key="Port" value="Port Number"/> //usually 25
	<add key="MailUserName" value="user name of the mail from which the mail will be sent"/>
	<add key="MailPwd" value="Password"/>
	<add key="To" value="To whom you're sending the mail"/>
	<add key="CC" value="If there's any cc to add"/>
</appSettings>

Now in the .NET class file, write the following code. (Note that here we are not concerned about validations.)

MailMessage msg = new MailMessage();
	
string To, Cc;
string strMailContent = "You can include your text here";

To = ConfigurationManager.AppSettings["To"].ToString();
Cc = ConfigurationManager.AppSettings["CC"].ToString();

//checking that if there exist a Cc
if (Cc != "" && Cc != String.Empty && Cc != null)
{
  msg.CC.Add(Cc);
}
msg.To.Add(To);
msg.From = new MailAddress(ConfigurationManager.AppSettings["MailUserName"].ToString());
msg.Subject = sub;

string path = Server.MapPath(@"images/Logo.jpg"); // my logo is placed in images folder

LinkedResource logo = new LinkedResource(path);
logo.ContentId = "companylogo";

//now do the HTML formatting
AlternateView av1 = AlternateView.CreateAlternateViewFromString(
      "<html><body><img src=cid:companylogo/>" + 
      "<br></body></html>" + strMailContent, 
      null, MediaTypeNames.Text.Html);

//now add the AlternateView
av1.LinkedResources.Add(logo);

//now append it to the body of the mail
msg.AlternateViews.Add(av1);
msg.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"].ToString();

smtp.Port = Int32.Parse(ConfigurationManager.AppSettings["Port"]);

//for gmail
smtp.EnableSSL=true;
smtp.Credentials = new System.Net.NetworkCredential(
     ConfigurationManager.AppSettings["MailUserName"].ToString(), 
     ConfigurationManager.AppSettings["MailPwd"].ToString());

smtp.Send(msg);

The LinkedResource class represents an embedded external resource in an email attachment, such as an image in an HTML attachment. More details. The AlternateView class represents the format to view an email message. More details.

Points of Interest

In this way we can actually format the body of the message with other HTML properties also.

History

No updates yet.

License

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

Share

About the Author

piyush_singh
Software Developer
India India
No Biography provided
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 1034846724-Nov-13 19:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 8 Apr 2012
Article Copyright 2012 by piyush_singh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid