Click here to Skip to main content
15,881,413 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.5K   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

 
AnswerRe: Can't connect? Pin
Adrian ~ Foobar Software22-Apr-04 3:21
Adrian ~ Foobar Software22-Apr-04 3:21 
GeneralDifficulties in setting Fields Pin
im_tct20-Apr-04 7:58
im_tct20-Apr-04 7:58 
GeneralRe: Difficulties in setting Fields Pin
Adrian ~ Foobar Software22-Apr-04 0:36
Adrian ~ Foobar Software22-Apr-04 0:36 
GeneralRe: Difficulties in setting Fields Pin
im_tct22-Apr-04 7:35
im_tct22-Apr-04 7:35 
GeneralRe: Difficulties in setting Fields Pin
Member 163625028-Jan-05 17:04
Member 163625028-Jan-05 17:04 
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 

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.