Click here to Skip to main content
11,490,003 members (50,254 online)
Click here to Skip to main content
Add your own
alternative version

Sending complex emails in .NET 1.1

, 15 Mar 2006 127.9K 945 105
This article describes complex issues about sending emails in .NET 1.1 (such as using a SMTP server that requires authentication).
email_sending_in_c__src.zip
SampleSendMail
App.ico
bin
SampleSendMail.csproj.user
Utils
bin
Utils.csproj.user
using System;
using System.Web.Mail;

namespace Utils
{
	/// <summary>
	/// EnhancedMailMessage is a class that provides more features for email sending in .NET
	/// </summary>
	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;
		}

		/// <summary>
		/// The display name that will appear in the recipient mail client
		/// </summary>
		public string FromName 
		{
			set 
			{
				fromName = value;
			}
			get 
			{
				return fromName;
			}
		}

		/// <summary>
		/// SMTP server (name or IP address)
		/// </summary>
		public string SMTPServerName 
		{
			set 
			{
				smtpServerName = value;
			}
			get 
			{
				return smtpServerName;
			}
		}

		/// <summary>
		/// Username needed for a SMTP server that requires authentication
		/// </summary>
		public string SMTPUserName 
		{
			set 
			{
				smtpUserName = value;
			}
			get 
			{
				return smtpUserName;
			}
		}
		
		/// <summary>
		/// Password needed for a SMTP server that requires authentication
		/// </summary>
		public string SMTPUserPassword 
		{
			set 
			{
				smtpUserPassword = value;
			}
			get 
			{
				return smtpUserPassword;
			}
		}
		
		/// <summary>
		/// SMTP server port (default 25)
		/// </summary>
		public int SMTPServerPort 
		{
			set 
			{
				smtpServerPort = value;
			}
			get 
			{
				return smtpServerPort;
			}
		}
		
		/// <summary>
		/// If SMTP server requires SSL
		/// </summary>
		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));			
			}

			// 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;
			}

			// ssl if needed
			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();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author


| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 16 Mar 2006
Article Copyright 2006 by FlorentinB
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid