The following code creates 2 views Plain Text view mai HTML view and posts 2 images with the email
protected void SendMail_Click(object s, EventArgs e)
{
System.Net.Mail.MailAddress sender = new System.Net.Mail.MailAddress("sender@mysite.com", "SENDER");
System.Net.Mail.MailAddress recipient = new System.Net.Mail.MailAddress("recipient@mysite.com", "RECIPIENT");
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(sender, recipient);
m.Subject = "TEST MESSAGE";
// Define the plain text alternate view and add to message
string plainTextBody = "You must use an email client that supports HTML messages";
System.Net.Mail.AlternateView plainTextView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(plainTextBody, null, System.Net.Mime.MediaTypeNames.Text.Plain);
// Add the Plain view
m.AlternateViews.Add(plainTextView);
// Add the HTML view
//---------------------------------------------------------------------------
// To embed images, we need to use the prefix 'cid' in the img src value
string htmlBody = "<strong>This is a test mail containing two image file as attachments</strong>";
htmlBody += "<img alt='Alternate Text1' src='cid:uniqueId1' /> <img alt='Alternate Text1' src='cid:uniqueId2' />";
htmlBody += "<strong>End of Mail</strong>";
System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
// Create the image resource from image path using LinkedResource class..
System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource("c:\\attachment\\image1.jpg", System.Net.Mime.MediaTypeNames.Text.Html);
imageResource.ContentId = "uniqueId1";
imageResource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
// Adding the image1 linked to htmlView...
htmlView.LinkedResources.Add(imageResource);
// Create the image resource from image path using LinkedResource class..
imageResource = new System.Net.Mail.LinkedResource("c:\\attachment\\image2.jpg", "image/jpeg");
imageResource.ContentId = "uniqueId2";
imageResource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
// Adding the image2 linked to htmlView...
htmlView.LinkedResources.Add(imageResource);
//---------------------------------------------------------------------------
m.AlternateViews.Add(htmlView);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient
{
Host = "smtp.mysmtp.com",
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("username", "password")
};
smtp.Send(m);
}