How to enable SMTP Authentication using System.Web.Mail

Article translations Article translations
Article ID: 555287 - View products that this article applies to.
Author: Carlos Walzer MVP
Expand all | Collapse all

SUMMARY

This article describes how to send e-mails from a .NET application through an SMTP server that requires authentication.

SYMPTOMS

Exchange Server requires, by default, authenticated users to send mail messages via SMTP. If you are developing a .NET application you will find that System.Web.Mail does not contain an API that allows you to provide credentials for connecting to an SMTP server. System.Web.Mail internally calls Microsoft® Collaboration Data Objects CDOSYS. SMTP Authentication is possible by defining configuration settings using the Configuration object associated to the Message CDO object.
 

MORE INFORMATION

The System.Web.Mail namespace provides an API that enables you to create and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered through an SMTP server.
Most of the SMTP servers installations requires authentication. The main reasoning for this is due to people abusing SMTP servers for SPAMing (i.e., sending out unsolicited junk mail).
The System.Web.Mail namespace classes don't expose an API for providing credentials to an SMPT server. Anyway you can achieve this by defining configuration settings for the Configuration object associated to the Message CDO. Configuration settings are made up of a set of fields (properties) that are simply name/value pairs. Most configuration fields used for messaging are in the http://schemas.microsoft.com/cdo/configuration/ namespace.
You can set fields to the message by adding them to the System.Web.Mail.MailMesage.Fields collection.
 
CDO Fields for configuring SMTP Authentication
To configure Message objects, you modify fields in the associated Configuration object. Most of the field names reside in the http://schemas.microsoft.com/cdo/configuration/ namespace:
  • smtpserver: SMTP server name.
  • smtpserverport: SMTP server port (default: 25).
  • sendusing: cdoSendUsingPort, value 2, for sending the message using the network.
  • smtpauthenticate: Specifies the mechanism used when authenticating to an SMTP service over the network. Possible values are:
    - cdoAnonymous, value 0. Do not authenticate.
    - cdoBasic, value 1. Use basic clear-text authentication. When using this option you have to provide the user name and password through the sendusername and sendpassword fields.
    - cdoNTLM, value 2. The current process security context is used to authenticate with the service.
  • sendusername: User name
  • sendpassword: Password

 
Example
The following example shows how to send an e-mail through an SMTP server that requires authentication server over the network by providing credentials in clear-text.
 
using System;
using System.Web.Mail;
namespace SMTPAuthentication
{
 public class SMTPAuthenticationExample
 {
  public static void SendMail()
  {
   string smtpServer = "smtp.domain.com";
   string userName = "johnDoe";
   string password = "pass";
   int cdoBasic = 1;
   int cdoSendUsingPort = 2;
   MailMessage msg = new MailMessage();
   if (userName.Length > 0)
   {
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25) ;
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort) ;
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); 
   }
   msg.To = "someone@domain.com";
   msg.From = "me@domain.com";
   msg.Subject = "Subject";
   msg.Body = "Message";
   SmtpMail.SmtpServer = smtpServer;
   SmtpMail.Send(msg);
  }
 }
}
 
Note:  The classes in the System.Web.Mail namespace can be used from ASP.NET or from any managed application. Be sure that your project references the System.Web.dll assembly.
 
References:
For more information about Collaboration Data Objects visit:
http://msdn.microsoft.com/library/en-us/exchanchor/htms/msexchsvr_cdo_top.asp
 
For more information about the System.Web.Mail namespace visit:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebmail.asp

Properties

Article ID: 555287 - Last Review: April 7, 2005 - Revision: 1.0
APPLIES TO
  • Microsoft .NET Framework 1.1
Keywords: 
kbpubmvp kbpubtypecca kbhowto KB555287
COMMUNITY SOLUTIONS CONTENT DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com