C#, Email, Programming, Web

Programmatically Send An Email Message With An Html Body (C#)

I have used the following snippet many times in the past. I would recommend validating your string values if the values are dynamic. I would also recommend placing your email attempt in a try/catch so you will not break your web application if there is some kind of issue on the email server itself. Anyway, hope this helps.

using System.Net.Mail;

// server settings
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "exchangeserver.yourdomain.com";

// declasre mail message
MailMessage mm = new MailMessage();

// from
mm.From = new MailAddress("donotreply@yourdomain.com");

// to
string toemail = "";
toemail = "persontosendto@yourdomain.com";
mm.To.Add(new MailAddress(toemail));

// cc
string toemailcc = "";
toemailcc = "another_person@yourdomain.com";
mm.CC.Add(new MailAddress(toemailcc));

// subject
mm.Subject = "The All Important Subject";

// body
string message = "";
message = "This is the all important email body message. <br /><br />";
message = message + "Sincerely yours, <br /><br />";
mm.Body = message;
mm.IsBodyHtml = true;

// send the message
client.Send(mm);