.NET: Sending a HTML Email with an Attachment

A reader who read the earlier post titled ColdFusion & .NET: Send an Email, asked a couple of questions on sending the email in .NET.  He was asking how you send an HTML email and secondly, add an attachment.  So I put together a quick sample of doing this.  I have also included the full code behind this time.  Also remember that all my .NET samples are using C#.

<code lang="csharp[lines]"> using System;<br></br>
using System.Net;<br></br> using System.Net.Mail;

public partial class htmlemail : System.Web.UI.Page
protected void Page
Load(object sender, EventArgs e)
string BodyText = "

Row 1Row 2

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.Subject = "Your Test HTML Email";
message.From = new System.Net.Mail.MailAddress("from@email.com");

message.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(BodyText, null, "text/html");

Attachment att = new Attachment(Server.MapPath("harvick.jpg"));

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.someserver.com");
smtp.Credentials = new NetworkCredential("username@email.com", "password");



comments powered by Disqus