article

Sending complex emails in .NET 1.1

Email
Submitted on: 1/4/2015 3:50:00 AM
By: Florentin BADEA (from psc cd)  
Level: Intermediate
User Rating: By 8 Users
Compatibility: C#
Views: 964
 
     This article describes complex issues about sending emails in .NET 1.1 (such as using a SMTP server that requires authentication).

 
				

Introduction

System.Web.Mail can be used to send emails from .NET 1.1 applications. Sending simple emails is very easy. More complicated is 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#:

// build the email message
MailMessage msg = new MailMessage();
msg.From = "from.email@domain.com";
msg.To = "to.email@domain.com";
msg.Subject = "Subject";
msg.Body = "Body";
// send the message
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:

// set SMTP server name
msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp.server.com";
// set SMTP server port
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;
// set SMTP username
msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "username";
// set SMTP user password
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:

/// 
/// EnhancedMailMessage is a class that provides more features for email sending in .NET
/// 
public class EnhancedMailMessage : MailMessage
{
	private string fromName;
	private string smtpServerName;
	private string smtpUserName;
	private string smtpUserPassword;
	private int smtpServerPort;
	public EnhancedMailMessage()
	{
		fromName = string.Empty;
		smtpServerName = string.Empty;
		smtpUserName = string.Empty;
		smtpUserPassword = string.Empty;
		smtpServerPort = 25;
	}
	/// 
	/// The display name that will appear in the recipient mail client
	/// 
	public string FromName 
	{
		set 
		{
			fromName = value;
		}
		get 
		{
			return fromName;
		}
	}
	/// 
	/// SMTP server (name or IP address)
	/// 
	public string SMTPServerName 
	{
		set 
		{
			smtpServerName = value;
		}
		get 
		{
			return smtpServerName;
		}
	}
	/// 
	/// Username needed for a SMTP server that requires authentication
	/// 
	public string SMTPUserName 
	{
		set 
		{
			smtpUserName = value;
		}
		get 
		{
			return smtpUserName;
		}
	}
	
	/// 
	/// Password needed for a SMTP server that requires authentication
	/// 
	public string SMTPUserPassword 
	{
		set 
		{
			smtpUserPassword = value;
		}
		get 
		{
			return smtpUserPassword;
		}
	}
	
	/// 
	/// SMTP server port (default 25)
	/// 
	public int SMTPServerPort 
	{
		set 
		{
			smtpServerPort = value;
		}
		get 
		{
			return smtpServerPort;
		}
	}
	
	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));			
		}
		// set SMTP server name
		this.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = smtpServerName;
		// set SMTP server port
		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;
			
			// set SMTP username
			this.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = smtpUserName;
			// set SMTP user password
			this.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = smtpUserPassword;
		}
		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();

Also, you can send emails using just on line of code:

EnhancedMailMessage.QuickSend("smtp.server.com", 
"to.email@domain.com", 
"from.email@domain.com",
"Subject",
"Body",
MailFormat.Html);

History

27 Feb 2006 - first draft of the article
28 Feb 2006 - created a wrapper class for all the code, created properties
01 Mar 2006 - more comments in the class, performance and coding style improvements

The conclusion

Complex things can be done using simple .NET Framework classes. More things about email sending in one of the next articles.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.