Send Email in ASP.NET 2.0

Method 1
This is a simple article which tells you how you can embed images in emails.This can be very useful when you want to send emails from your website.A common scenario would be when a new user registers with your site and you want to send a welcome email with your site’s logo on top.

I searched the net when I faced this issue. I am grateful to many authors from where I could make a  start. I have used their code while writing this.Due credit goes to them all. The intention is to make the code available for the others too.  To use this all you have to so is copy the code and paste it in any button click event.

This may only work with ASP.NET 2.0 & C#. I have tested it too.It will not run in ASP.NET 1.1.

Namespaces Used

using System.Net.Mail;
using System.Net.Mime;


// send mail to the new user who has registered.
protected void yourButton_Click(object sender, EventArgs e)

string strMailContent = “Welcome new user”;
string fromAddress = “”;
string toAddress = “”;
string contentId  = “image1”;

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

MailMessage mailMessage = new MailMessage( fromAddress, toAddress );
mailMessage.Bcc.Add(“”); // put your id here
mailMessage.Subject = “Welcome new User”;

LinkedResource logo = new LinkedResource(path);
logo.ContentId = “companylogo”;

// done HTML formatting in the next line to display my logo

AlternateView av1 = AlternateView.CreateAlternateViewFromString
(“<html><body><img src=cid:companylogo/><br></body></html>” +
strMailContent, null, MediaTypeNames.Text.Html);


mailMessage.IsBodyHtml = true;

//use this if you are in the development server

SmtpClient mailSender = new SmtpClient(“localhost”);



Method 2 :
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 class)

SmtpDirect smtp = new SmtpDirect(“localhost”);





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s