Click here to Skip to main content
11,635,705 members (78,794 online)
Click here to Skip to main content

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

, 24 Nov 2002 276.2K 9.8K 103
Rate this:
Please Sign up or sign in to vote.
This sample shows you how to send e-mails over SMTP using the Mail-Class in C# and ASP.NET.
<!-- Download Links --> <!-- Article image -->

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:

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

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

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):

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

Share

About the Author

eightfour
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

You may also be interested in...

Comments and Discussions

 
AnswerBCC cc Pin
Member 84856863-Oct-12 21:37
memberMember 84856863-Oct-12 21:37 
GeneralMy vote of 3 Pin
Member 882172715-May-12 20:24
memberMember 882172715-May-12 20:24 
QuestionGood - How to send email in asp.net or C# Pin
madhan2u8-May-12 21:33
membermadhan2u8-May-12 21:33 
GeneralSendMail over SMTP (C#) - Solved Pin
Surya R Praveen7-May-12 22:03
memberSurya R Praveen7-May-12 22:03 
GeneralExe of Web Application Pin
Gaurav.Tekkie20-May-10 21:09
memberGaurav.Tekkie20-May-10 21:09 
Generalwin 7 Pin
aeolus4m4-Sep-09 2:02
memberaeolus4m4-Sep-09 2:02 
QuestionError Pin
ratneshsoni4-Aug-09 3:28
memberratneshsoni4-Aug-09 3:28 
Generalcan't received email although follow all the step of your guide .anybody help me ! plz . Pin
fantasy20080810-Jul-08 21:46
memberfantasy20080810-Jul-08 21:46 
Generalcan't able to send email [modified] Pin
Sanjay k Jain4-Jun-08 1:19
memberSanjay k Jain4-Jun-08 1:19 
Generalhai again this is paulraj Pin
Paul's19-Nov-07 6:46
memberPaul's19-Nov-07 6:46 
Generaltransport error code 0x800ccc15 Pin
Paul's19-Nov-07 6:36
memberPaul's19-Nov-07 6:36 
GeneralRe: transport error code 0x800ccc15 Pin
The Geek25-Mar-08 2:57
memberThe Geek25-Mar-08 2:57 
QuestionRe: transport error code 0x800ccc15 Pin
Saurabh Tripathi29-Apr-08 23:31
memberSaurabh Tripathi29-Apr-08 23:31 
Generalcould not acess cdo.Message Pin
koorasrinivas8-Jan-07 21:42
memberkoorasrinivas8-Jan-07 21:42 
GeneralCould not access "CDO.Message" Pin
Ramesh.Tsi10-Dec-06 20:19
memberRamesh.Tsi10-Dec-06 20:19 
QuestionHow to find the rejected recipient addresses? Pin
anees7727-Jun-06 23:15
memberanees7727-Jun-06 23:15 
Generalsame error Pin
lakshmi patil28-Dec-05 22:52
memberlakshmi patil28-Dec-05 22:52 
GeneralCould not access 'CDO.Message' object Pin
hany mahmoud14-Sep-05 22:52
memberhany mahmoud14-Sep-05 22:52 
Questionwhy can't send out my Email? Pin
lideyong26-Apr-05 18:10
susslideyong26-Apr-05 18:10 
AnswerRe: why can't send out my Email? Pin
Spring Boy27-Apr-05 21:58
memberSpring Boy27-Apr-05 21:58 
AnswerRe: why can't send out my Email? Pin
Gaurav.Tekkie20-May-10 21:14
memberGaurav.Tekkie20-May-10 21:14 
QuestionHow to read from a database and send to more than one email address Pin
paulpinder3-Dec-04 3:19
memberpaulpinder3-Dec-04 3:19 
AnswerRe: How to read from a database and send to more than one email address Pin
mosn842-Jan-08 16:38
membermosn842-Jan-08 16:38 
GeneralOpen Server Relay Pin
Chris Porter20-Aug-04 5:57
memberChris Porter20-Aug-04 5:57 
Questioncannot send email using SMTP???? Pin
choonchi26-Dec-03 6:04
memberchoonchi26-Dec-03 6:04 
GeneralRelaying vs. Authentication Pin
yigitatli25-Aug-03 17:19
memberyigitatli25-Aug-03 17:19 
General[Message Removed] Pin
Mojtaba Vali10-May-06 19:34
memberMojtaba Vali10-May-06 19:34 
GeneralAbout multi-sending Pin
NilzoneTW3-Jul-03 5:48
memberNilzoneTW3-Jul-03 5:48 
GeneralRe: About multi-sending Pin
praetorion1-Jun-05 22:16
memberpraetorion1-Jun-05 22:16 
GeneralError :- Could not access 'CDO.Message' object. Pin
deepmala25-Mar-03 0:41
memberdeepmala25-Mar-03 0:41 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
Agyklon4-Apr-03 0:09
memberAgyklon4-Apr-03 0:09 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
deepmala6-Apr-03 17:52
memberdeepmala6-Apr-03 17:52 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
ScottyWakefield9-Dec-03 19:24
memberScottyWakefield9-Dec-03 19:24 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
Pham Ngoc Quyen16-Jan-05 22:46
memberPham Ngoc Quyen16-Jan-05 22:46 
GeneralRe: Error :- Could not access 'CDO.Message' object. Pin
praetorion1-Jun-05 22:15
memberpraetorion1-Jun-05 22:15 
Generallocalhost does not work Pin
tgueth11-Feb-03 7:42
membertgueth11-Feb-03 7:42 
GeneralRe: localhost does not work Pin
Steve McLenithan22-Feb-03 3:31
memberSteve McLenithan22-Feb-03 3:31 
GeneralRe: localhost does not work Pin
tgueth22-Feb-03 11:14
membertgueth22-Feb-03 11:14 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 25 Nov 2002
Article Copyright 2002 by eightfour
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid