I never realized this but you can set the default SMTP information in the .config file (Thanks Viktar!). This applies to the new System.Net.Mail.* classes (Not the deprecated System.Web.Mail.* classes). MSDN defines the elements here and here.
<configuration> <system.net> <mailSettings> <smtp deliveryMethod="network" from="appointments@TrepanationNation.com"> <network host="smtp.TrepanationNation.com" port="25" defaultCredentials="true"/> </smtp> </mailSettings> </system.net>
The code is the same but you no longer have to specify the from and SMTP server details:
using (MailMessage message = new MailMessage()) { message.IsBodyHtml = true; message.To.Add("someguy@someserver.com"); message.Subject = "Trepanation Appointment"; message.Body = @"<b>This confirms your Trepanation appointment this Friday.</b>"; SmtpClient mailClient = new SmtpClient(); mailClient.Send(message); }