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

SendMail 101 - How to send e-mails over SMTP (C# and ASP.NET)

Rate me:
Please Sign up or sign in to vote.
4.23/5 (24 votes)
24 Nov 20022 min read 315K   10.1K   103   38
This sample shows you how to send e-mails over SMTP using the Mail-Class in C# and ASP.NET.

Sample Image - SendMail01.gif

Introduction

In this article I will show you how easy it is to create your own mailer. This sample is for beginners and provides just one To:, Cc: and Bcc: - there is also no parser for the From: - TextBox, so you don't have to use white spaces for the From-Name.

Using the code

The code is very small and easy to read, so I don't have many things to explain. At first I have created some TextBoxes using Drag & Drop (VisualStudio). Then I double-clicked on the submit-button and Visual Studio created an event for it. All the important source is inside the click-event.

1. Create an instance of SmtpMail.
2. Set the properties with text from the TextBoxes.
3. Try to send the mail and write info to the screen.

protected void ButtonSubmit_Click(object sender, System.EventArgs e)
{
    Server.ScriptTimeout = 1000;
    Response.Flush();

    SmtpMail.SmtpServer = "localhost";
    MailMessage mail = new MailMessage();
    mail.To = this.TextBoxTo.Text;
    mail.Cc = this.TextBoxCc.Text;
    mail.Bcc = this.TextBoxBcc.Text;
    mail.From = this.TextBoxFrom.Text;
    mail.Subject = this.TextBoxSubject.Text;
    mail.Body = this.TextBoxBody.Text;

    try
    {
        SmtpMail.Send(mail);
        Response.Write("The Mail has been sent to: ");
        Response.Write(mail.To);
        Response.Write(mail.Cc);
        Response.Write(mail.Bcc);
    }
    catch(System.Exception ex)
    {
        Response.Write(ex.Message);
    }

    Response.Flush();
}

Points of Interest

For the Demo I have created an Installer (msi-file). So you just extract the file and start the installer. The virtual directory etc. creates the installer for you - also a shortcut on your desktop, so you just doubleclick it and see the demo. But if it doesn't work (an error occurs) you have to change the options for your smtp-server. Normally the server is running but doesn't accept any mails from any computer - you have to change this setting.

Here how you can do it (sorry but I have a german Windows, so the text is maybe different from yours):

First open your IIS and click with the right mouse-button on your SMTP-server:

Image 2

Then select "Eigenschaften" or "Properties" (I think it's called in English):

Image 3

A new Window opens. Here go to the second tab and click on the last button:

Image 4

And here is one more window. You have the choice between adding a special computer (ip, name) which is allowed to send mails, and to give this permission to all computers (second option):

Image 5

That's it - now the Demo has to work!

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
Germany Germany
1978-I was born
1989-My parents (++me, ++myBrother) moved to Germany
1990-I've got my first Computer (Amstrad CPC-464)
1991-I've bought a Commodore C64 by myself
1992-I've bought my first PC (486sx25)
1993-My first Website (static HTML) was ready
1993-PHP, ASP, SQL, C++, JavaScript, Python
2001-eightfour goes csharp

Comments and Discussions

 
GeneralRelaying vs. Authentication Pin
yigitatli25-Aug-03 17:19
yigitatli25-Aug-03 17:19 
General[Message Removed] Pin
Mojtaba Vali10-May-06 19:34
Mojtaba Vali10-May-06 19:34 
GeneralAbout multi-sending Pin
Eric Kwong3-Jul-03 5:48
Eric Kwong3-Jul-03 5:48 
GeneralRe: About multi-sending Pin
praetorion1-Jun-05 22:16
praetorion1-Jun-05 22:16 
GeneralError :- Could not access 'CDO.Message' object. Pin
deepmala25-Mar-03 0:41
deepmala25-Mar-03 0:41 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
Peter Kiss4-Apr-03 0:09
Peter Kiss4-Apr-03 0:09 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
deepmala6-Apr-03 17:52
deepmala6-Apr-03 17:52 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
ScottyWakefield9-Dec-03 19:24
ScottyWakefield9-Dec-03 19:24 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
Pham Ngoc Quyen16-Jan-05 22:46
Pham Ngoc Quyen16-Jan-05 22:46 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
praetorion1-Jun-05 22:15
praetorion1-Jun-05 22:15 
Generallocalhost does not work Pin
tgueth11-Feb-03 7:42
professionaltgueth11-Feb-03 7:42 
GeneralRe: localhost does not work Pin
Steve McLenithan22-Feb-03 3:31
Steve McLenithan22-Feb-03 3:31 
GeneralRe: localhost does not work Pin
tgueth22-Feb-03 11:14
professionaltgueth22-Feb-03 11:14 

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.