Send HTML E-mails with embedded image content using ASP.NET C#
To send emails with HTML formatting and to embed resources such as
images, in csharp, we create an alternateview to hold the embedded
resource link and then create a linked resource object for each embedded
image. The content ID of the linked resource is used to embed the image
in the html email. add the HTML-tagged content to the Mail Body. The
important thing to remember is to set the IsBodyHtml attribute to True
on the mail object.
Example for sending HTML Email with embedded content:
Example for sending HTML Email with embedded content:
MailMessage m = new MailMessage(); m.From = new MailAddress("ir@w3mentor.com", "Al Salman"); m.To.Add(new MailAddress("su@w3mentor.com", "Aman")); m.Subject = "html email with embedded image coming!"; // Create the HTML message body // Reference embedded images using the content ID string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:Pic1\"></body></html>"; AlternateView avHtml = AlternateView.CreateAlternateViewFromString (htmlBody, null, MediaTypeNames.Text.Html); // Create a LinkedResource object for each embedded image LinkedResource pic1 = new LinkedResource("pic.jpg", MediaTypeNames.Image.Jpeg); pic1.ContentId = "Pic1"; avHtml.LinkedResources.Add(pic1); // Create an alternate view for unsupported clients string textBody = "You must use an e-mail client that supports HTML messages"; AlternateView avText = AlternateView.CreateAlternateViewFromString (textBody, null, MediaTypeNames.Text.Plain); m.AlternateViews.Add(avHtml); m.AlternateViews.Add(avText); // Send the message SmtpClient client = new SmtpClient("smtp.w3mentor.com"); client.Send(m);
0 comments: