Send an HTML email with an embedded image and a plain text message

C# Code:

MailMessage  msg = new  MailMessage(“SENDER EMAIL”, “RECEIVER EMAIL”);

msg.Subject = “This is my first email through program”;
msg.IsBodyHtml = true;

AlternateView View ;
LinkedResource resource;
SmtpClient client = new SmtpClient() ;
StringBuilder msgText = new StringBuilder();

msgText.Append(“<p>Hi there,<p>”);
msgText.Append(“<p>Welcome to the new world programming.<p><br>”);
msgText.Append(“<p>Thanks<p>”);
msgText.Append(“<p>With regards,ASHISH<p><br>”);
msgText.Append(“<img src=cid:Image1 />”);

//create an alternate view for your mail
View = AlternateView.CreateAlternateViewFromString(msgText.ToString(), null, “text/html”);

//link the resource to embedd
resource = new  LinkedResource(“C:\\Users\\Public\\Pictures\\Sample Pictures\\a.jpg”);

//name the resource
resource.ContentId = “Image1”;

//add the resource to the alternate view
View.LinkedResources.Add(resource);

//add the view to the message
msg.AlternateViews.Add(View);

//SmtpClient client = new SmtpClient();
client.Host = “smtp.gmail.com”; //write your smtp server name here
client.Port = 587;
client.EnableSsl = true ;   //enable this if your smtp server needs SSL to communicate
client.Credentials = new System.Net.NetworkCredential(“USER EMAIL”, “PASSWORD”);
client.Send(msg);

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