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

SMTP Mailer with Authentication

Rate me:
Please Sign up or sign in to vote.
4.10/5 (10 votes)
25 Oct 20031 min read 105.4K   2.5K   32   14
.NET assembly, 100% managed code. Send mails using a SMTP server that requires authentication.

Introduction

This article describes a simple assembly to send mails programmatically using a SMTP server that requires authentication. It also works with those servers that doesn't require it.

In this release, you cannot use attachments, but I will implement it as soon as possible.

To show you how simple this component is, I'll show you it's main interface:

The main method is SendMail:

1st Overload

Used for sending a simple mail using a server without authentication. The default MailFormat is HTML.

public System.Boolean SendMail ( System.String from , System.String to , System.String subject , System.String message , System.String serverName )

2nd Overload

Used for sending a simple mail using a server with authentication. The default MailFormat is HTML.

public System.Boolean SendMail ( System.String from , System.String to , System.String subject , System.String message , System.String AUTH_Name , System.String AUTH_Password , System.String serverName )

3rd Overload

Used for sending a mail using a server with authentication. Here you can specify the MailFormat and also the server port.

public System.Boolean SendMail ( System.String from , System.String to , System.String subject , System.String message , MailFormat format , System.String AUTH_Name , System.String AUTH_Password , System.String serverName , System.Int32 port )

When the method SendMail returns false, you have an Err property that contains the Exception that originated the failure of the method call.

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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGmail Pin
HankiDesign6-Jul-07 8:17
HankiDesign6-Jul-07 8:17 
GeneralIt's working! Pin
_BlackLine_23-Apr-06 7:44
_BlackLine_23-Apr-06 7:44 
Generaldifferent views in different Browsers Pin
Ramalinga S Reddy26-Mar-06 19:47
Ramalinga S Reddy26-Mar-06 19:47 
GeneralI could not be able to run Pin
Ramalinga S Reddy26-Mar-06 19:34
Ramalinga S Reddy26-Mar-06 19:34 
GeneralTime stamp says 12/31/1969 Pin
FinishedOnTime14-Mar-04 10:57
FinishedOnTime14-Mar-04 10:57 
GeneralGet an Error Pin
Member 71462119-Nov-03 6:02
Member 71462119-Nov-03 6:02 
GeneralRe: Get an Error Pin
pjutard19-Nov-03 6:19
pjutard19-Nov-03 6:19 
QuestionHow about other encoding mail? Pin
Hardy Wang14-Nov-03 3:00
Hardy Wang14-Nov-03 3:00 
AnswerRe: How about other encoding mail? Pin
pjutard14-Nov-03 4:35
pjutard14-Nov-03 4:35 
GeneralOOP Pin
Heath Stewart26-Oct-03 18:08
protectorHeath Stewart26-Oct-03 18:08 
GeneralRe: OOP Pin
pjutard26-Oct-03 18:37
pjutard26-Oct-03 18:37 
GeneralRe: OOP Pin
Heath Stewart26-Oct-03 18:50
protectorHeath Stewart26-Oct-03 18:50 
Depends how much you use your class. A single method would be easiest and less confusing. If you keep them, attribute them with the ObsoleteAttribute and get rid of them later. For now, to make things easier, have them create a mail object and call the single overload for a mail object.

As far as recipients, I would treat this multiple ways. The important thing is consistency. you'll see a lot of classes that have both a and b above (but spell them out, as is common):
public void AddRecipient(string name, string email)
{
    this.AddRecipient(new Recipient(name, email);
}
public void AddRecipient(Recipient rcpt)
{
    // Add to internal list.
}
You could also provide the To property, but think about this carefully. It is far more expensive for string operations such as this. Perhaps it could get a read-only property (only provides a get accessor) that simply enumerates the internal list maintained for recipients and joins them with commas and returns the string. The important thing is to use string buffers (like System.Text.StringBuilder) effectively since strings are immutable in .NET.

You could provide a set accessor for a To property, but I would split the string against common delimiters (value.Split(',', ';')) and then parse each name / email pair (if name is available) and call AddRecipient to make sure that you don't have to maintain such code in multiple places.

One final word, having an AddRange like property is also convenient, such as adding an AddRecipientRange. This could either take an array of Recipient objects, or a parameter array, which is like varargs in C/C++:
public void AddRecipientRange(Recipient[] rcpts)
{
    // ...
}
// OR...
public void AddRecipientRange(params Recipient[] rcpts)
{
    // ...
}
The latter allows for a variable number of parameters like the ellipses in C/C++.

Just some suggestions. Great work, though. As an editor, though, seeing some more content would be nice, such as particularily interesting code fragments. Perhaps any SMTP authentication or - better yet (for newbies) - how you establish a connection with the SMTP server and say HELO to get things going. This would make for a much better article.

 

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
GeneralRe: OOP Pin
pjutard27-Oct-03 2:16
pjutard27-Oct-03 2:16 
GeneralRe: OOP Pin
alpy26-Mar-07 8:09
alpy26-Mar-07 8:09 

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.