Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

SMTP Authentication using System.Web.Mail (CDOSYS)

0.00/5 (No votes)
12 Dec 2003 1  
Setting CDOSYS Schema contents to permit "tricks" with System.Web.Mail

Introduction

SMTP Authentication - expensive third party control or roll-you-own with System.Net and sockets? Not required: The CDOSYS Schema is accessible directly from your code and you can use MailMessage.Fields to access and change schema values to control the attributes of your email.

Using the code

Create your email using the MailMessage class - then set the Schema Fields of Interest to you. For example, to send via an external SMTP server on port 25 with SMTP authentication name and password:

// using System.Web.Mail;

    eMail = new MailMessage();
    eMail.BodyFormat = MailFormat.Text;
    eMail.From = _SendFrom;
    eMail.Fields[http://schemas.microsoft.com/cdo/configuration/smtsperver]

      = "SMTPServerName";
    eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 25;
    eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/sendusing"]  = 2;
    if (SMTPUser != null && SMTPPassword != null)
    {
        eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
        eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/sendusername"] = 
  "SMTPAUTHUser";
        eMail.Fields[
"http://schemas.microsoft.com/cdo/configuration/sendpassword"] = 
  "SMTPAUTHPassword";
    }
    eMail.To = "recipients";
    SmtpMail.SmtpServer = SMTPServerName;
    SmtpMail.Send(eMail);
//

Check MSDN for the CDOSYS and enumerated values.

History

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