Sending Image Embedded HTML Mail with C#

Posted: 11 Nisan 2012 in .NET

If you are not familiar with .Net Mail, have a look here.

First of all, create LinkedResource Object for the image or images; you want to embed in your Mail. Assign a unique name to ContentId Property of LinkedResource Object.

//Creating LinkedSource for embedded picture
LinkedResource myPhoto = new LinkedResource(“c:\\27_n.jpg”);
myPhoto.ContentId = “me”;

Use this unique ContentId in < img src=cid:me > Tag to create a HTML AlternateView.

//Creating AlternateView for Plain Text Mail
AlternateView normalView = AlternateView.CreateAlternateViewFromString(“Hello, Welcome to the party.”, null, “text/plain”);

//Creating AlternateView for HTML Mail
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(“<img src=cid:me>Hello, Welcome to the party.”, null, “text/html”);

Add LinkedResource Object to HTML AlternateView. You need to add AlternateViews to MailMessage as well.

//Adding LinkedSource to AlternateView
htmlView.LinkedResources.Add(myPhoto);

//Adding AlternateViews to MailMessage
mail.AlternateViews.Add(normalView); mail.AlternateViews.Add(htmlView);

Image embedded Mail is ready to sent. The following one is complete code snippet to send image embedded mail.
MailMessage mail = new MailMessage();
SmtpClient smtpServer = new SmtpClient();
//SmtpClient Configuration
smtpServer.Credentials = new System.Net.NetworkCredential(“samigoncu@gmail.com”, “password”);
smtpServer.Port = 587; smtpServer.Host = “smtp.gmail.com”; smtpServer.EnableSsl = true;
//MailMessage Configuration mail.From = new MailAddress(“samigoncu@gmail.com”, “Sami GÖNCÜ”, System.Text.Encoding.UTF8);
mail.To.Add(“samigoncu@yahoo.com”); mail.Subject = “Mail with embedded image”;
//Creating AlternateView for Plain Text Mail
AlternateView normalView = AlternateView.CreateAlternateViewFromString(“Hello, Welcome to the party.”, null, “text/plain”);
//Creating AlternateView for HTML Mail
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(“<img src=cid:me>Hello, Welcome to the party.”, null, “text/html”);
//Creating LinkedSource for embedded picture LinkedResource myPhoto = new LinkedResource(“c:\\27_n.jpg”); myPhoto.ContentId = “me”;
//Adding LinkedSource to AlternateView
htmlView.LinkedResources.Add(myPhoto);
//Adding AlternateViews to MailMessage
mail.AlternateViews.Add(normalView); mail.AlternateViews.Add(htmlView);
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; mail.ReplyTo = new MailAddress(“samigoncu@gmail.com”);
smtpServer.Send(mail);

Bir Cevap Yazın

Aşağıya bilgilerinizi girin veya oturum açmak için bir simgeye tıklayın:

WordPress.com Logosu

WordPress.com hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Connecting to %s