Click here to Skip to main content
15,889,861 members
Articles / Web Development / ASP.NET
Article

SMTP Authentication using System.Web.Mail (CDOSYS)

Rate me:
Please Sign up or sign in to vote.
4.75/5 (49 votes)
12 Dec 2003 368.6K   70   38
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:

C#
// 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


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Difficulties in setting Fields Pin
xlouk23-Feb-05 20:30
xlouk23-Feb-05 20:30 
QuestionHow about a little wrapper? Pin
JasonSmith23-Mar-04 14:41
JasonSmith23-Mar-04 14:41 
AnswerRe: How about a little wrapper? Pin
C.Shattock23-Mar-04 17:40
C.Shattock23-Mar-04 17:40 
GeneralNice tip Pin
CreProDes20-Feb-04 19:28
CreProDes20-Feb-04 19:28 
GeneralRe: Nice tip Pin
spidur124-Feb-04 5:33
spidur124-Feb-04 5:33 
GeneralRe: Nice tip Pin
FatihSever27-May-05 9:50
FatihSever27-May-05 9:50 
Generalsmtpserverport Pin
mattski5-Jan-04 12:58
mattski5-Jan-04 12:58 
GeneralRe: smtpserverport Pin
C.Shattock5-Jan-04 18:47
C.Shattock5-Jan-04 18:47 
Sorry - can't really help with this: The port number is expressed as a long - which is carried through the System.Web.Mail.CdoSysHelp and LateBoundAccessHelper to CDOSYS. The Error is exposed as an ADO 2.7 error. CODSYS uses ADO (1.5 I think) as it is exposed as an OLE DB object. The "Status" referred to of the Field objects I can't access through C# (i.e. Interop) - and you'd almost certainly have to write an un-managed C++ COM object to access these status fields.

There is nothing I can find (SDK, MSDN, Google etc.) referring to a limit for CDO smtpserverport. The fact that it fails if greater than 9999 indicates there is some code buried in CDOSYS that is throwing an unhandled exception (why on x270F? - all appears to be defined as long or DBTYPE_I4?).

I would say you have two options - either: 1. Ask MS if this is a bug in CDOSYS they can deal with or 2. (given that the chances of an MS response are infinitesmal) look at your architecture/requirement and find a workaround (What is your requirement and proposed run-time architecture?). Sorry that I can't offer more help.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.