Introduction
System.Web.Mail
can be used to send emails from .NET 1.1 applications. Sending simple emails is very easy. It is more complicated when you try to send emails using a SMTP server that requires authentication, or even when you just need to format the From name of the email you want to send.
Background
Here is the most simple piece of code that will send an email using C#:
MailMessage msg = new MailMessage();
msg.From = "from.email@domain.com";
msg.To = "to.email@domain.com";
msg.Subject = "Subject";
msg.Body = "Body";
SmtpMail.SmtpServer = "smtp.server.com";
SmtpMail.Send(msg);
Problems and the immediate solution
Things need to get more complicated if instead of displaying the From email address, you want to display a name that the recipient of the email will see. For that, a custom header needs to be added:
string sFromName = "From display name";
string sFromAddress = "from.email@domain.com";
msg.Headers.Add("From", string.Format("{0} <{1}>",
sFromName, sFromAddress));
Even more complicated will be to send an email using a SMTP server that requires authentication. For that, the Fields
collection of the MailMessage
object needs to be used. Here is the sample piece of code that will help you solve your problems:
msg.Fields["http://schemas.microsoft.com" +
"/cdo/configuration/smtpserver"] = "smtp.server.com";
msg.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/smtpserverport"] = 25;
msg.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendusing"] = 2;
msg.Fields["http://schemas.microsoft.com/cdo/" +
"configuration/smtpauthenticate"] = 1;
msg.Fields["http://schemas.microsoft.com/cdo" +
"/configuration/sendusername"] = "username";
msg.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendpassword"] = "password";
The better solution
A better solution for the enhancements described above would be to create a new class that is inherited from MailMessage
and has the extra features. Here is the content of the new class:
public class EnhancedMailMessage : MailMessage
{
private string fromName;
private string smtpServerName;
private string smtpUserName;
private string smtpUserPassword;
private int smtpServerPort;
private bool smtpSSL;
public EnhancedMailMessage()
{
fromName = string.Empty;
smtpServerName = string.Empty;
smtpUserName = string.Empty;
smtpUserPassword = string.Empty;
smtpServerPort = 25;
smtpSSL = false;
}
public string FromName
{
set
{
fromName = value;
}
get
{
return fromName;
}
}
public string SMTPServerName
{
set
{
smtpServerName = value;
}
get
{
return smtpServerName;
}
}
public string SMTPUserName
{
set
{
smtpUserName = value;
}
get
{
return smtpUserName;
}
}
public string SMTPUserPassword
{
set
{
smtpUserPassword = value;
}
get
{
return smtpUserPassword;
}
}
public int SMTPServerPort
{
set
{
smtpServerPort = value;
}
get
{
return smtpServerPort;
}
}
public bool SMTPSSL
{
set
{
smtpSSL = value;
}
get
{
return smtpSSL;
}
}
public void Send()
{
if (smtpServerName.Length == 0)
{
throw new Exception("SMTP Server not specified");
}
if (fromName.Length > 0)
{
this.Headers.Add("From",
string.Format("{0} <{1}>",
FromName, From));
}
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/smtpserver"] = smtpServerName;
this.Fields["http://schemas.microsoft.com/cdo" +
"/configuration/smtpserverport"] = smtpServerPort;
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendusing"] = 2;
if (smtpUserName.Length >0 && smtpUserPassword.Length > 0)
{
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/smtpauthenticate"] = 1;
this.Fields["http://schemas.microsoft.com" +
"/cdo/configuration/sendusername"] = smtpUserName;
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendpassword"] = smtpUserPassword;
}
if (smtpSSL)
{
this.Fields.Add("http://schemas.microsoft" +
".com/cdo/configuration/smtpusessl", "true");
}
SmtpMail.SmtpServer = smtpServerName;
SmtpMail.Send(this);
}
public static void QuickSend(
string SMTPServerName,
string ToEmail,
string FromEmail,
string Subject,
string Body,
MailFormat BodyFormat)
{
EnhancedMailMessage msg = new EnhancedMailMessage();
msg.From = FromEmail;
msg.To = ToEmail;
msg.Subject = Subject;
msg.Body = Body;
msg.BodyFormat = BodyFormat;
msg.SMTPServerName = SMTPServerName;
msg.Send();
}
}
As you can see from the code above, you can send emails using SMTP servers that require authentication. Here is a sample usage code:
EnhancedMailMessage msg = new EnhancedMailMessage();
msg.From = "from.email@domain.com";
msg.FromName = "From display name";
msg.To = "to.email@domain.com";
msg.Subject = "Subject";
msg.Body = "Body";
msg.SMTPServerName = "smtp.server.com";
msg.SMTPUserName = "username";
msg.SMTPUserPassword = "password";
msg.Send();
There are SMTP servers that require SSL. One very well known example is Gmail. In order to send emails using the Gmail SMTP server, you need to specify that and also set the correct port. The following example can be used to send emails using the Gmail SMTP:
EnhancedMailMessage msg = new EnhancedMailMessage();
msg.From = "your.address@gmail.com";
msg.FromName = "Your name";
msg.To = "to.email@domain.com";
msg.Subject = "Test email from gmail";
msg.Body = "Gmail is great";
msg.SMTPServerName = "smtp.gmail.com";
msg.SMTPUserName = "your.address@gmail.com";
msg.SMTPUserPassword = "yourpassword";
msg.SMTPServerPort = 465;
msg.SMTPSSL = true;
msg.Send();
Also, you can send emails using just one line of code:
EnhancedMailMessage.QuickSend("smtp.server.com",
"to.email@domain.com",
"from.email@domain.com",
"Subject",
"Body",
MailFormat.Html);
Conclusion
Complex things can be done using simple .NET Framework classes. I will explain more things about email sending in one of my next articles.
History
- 27 Feb 2006 - first draft of the article.
- 28 Feb 2006 - created a wrapper class for all the code, and created properties.
- 01 Mar 2006 - more comments in the class, performance and coding style improvements.
- 16 Mar 2006 - added support for SMTP servers that require SSL.