Multipart Mime Email Messages in Dot Net

Here I am posting complete code related to my previous post ‘Embedding Images in Email‘.


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

// other code stuff goes here

protected void SendEmail()
{

MailMessage mail = new MailMessage("FromEmail@gmail.com", "ToEmail@gmail.com");
mail.Subject = "...and life goes on ...";

//create the image resource from image path using LinkedResource class..
LinkedResource imageResource1 = new LinkedResource ("C:\images\myImg.jpg", "image/jpeg");
imageResource1.ContentId = "uniqueId1";
imageResource1.TransferEncoding = TransferEncoding.Base64;

StringBuilder htmlBody = new StringBuilder ();

string emailMsg = "Your Email message";

htmlBody.Append(emailMsg);
htmlBody.AppendLine("<img alt=\"AltText\" hspace=0 src=\"cid:uniqueId1\" border=0 >");

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody.ToString(), null, "text/html");

//adding the imaged linked to htmlView...
htmlView.LinkedResources.Add(imageResource1);

mail.AlternateViews.Add(htmlView);

SmtpClient smtp = new SmtpClient ("smtp.gmail.com", 587);
// If smtp server requires SSL
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("yourEmail@gmail.com", "yourPassword");
smtp.Send(mail);

}

Advertisements

~ by UTS on May 22, 2009.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

 
%d bloggers like this: