Click here to Skip to main content
Click here to Skip to main content

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

, 24 Nov 2002
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


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;

    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;

        Response.Write("The Mail has been sent to: ");
    catch(System.Exception ex)


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!


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


About the Author

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

AnswerBCC cc PinmemberMember 84856863-Oct-12 22:37 
GeneralMy vote of 3 PinmemberMember 882172715-May-12 21:24 
QuestionGood - How to send email in or C# Pinmembermadhan2u8-May-12 22:33 
GeneralSendMail over SMTP (C#) - Solved PinmemberSurya R Praveen7-May-12 23:03 
Generalthis way work! Pinmemberberkut200612-Mar-11 10:21 
GeneralExe of Web Application PinmemberGaurav.Tekkie20-May-10 22:09 
GeneralRe: Exe of Web Application Pinmemberberkut200622-Mar-11 7:42 
Generalwin 7 Pinmemberaeolus4m4-Sep-09 3:02 
QuestionError Pinmemberratneshsoni4-Aug-09 4:28 
Generalcan't received email although follow all the step of your guide .anybody help me ! plz . Pinmemberfantasy20080810-Jul-08 22:46 
Generalcan't able to send email [modified] PinmemberSanjay k Jain4-Jun-08 2:19 
Generalhai again this is paulraj PinmemberPaul's19-Nov-07 7:46 
Generaltransport error code 0x800ccc15 PinmemberPaul's19-Nov-07 7:36 
GeneralRe: transport error code 0x800ccc15 PinmemberThe Geek25-Mar-08 3:57 
QuestionRe: transport error code 0x800ccc15 PinmemberSaurabh Tripathi30-Apr-08 0:31 
Generalcould not acess cdo.Message Pinmemberkoorasrinivas8-Jan-07 22:42 
GeneralCould not access "CDO.Message" PinmemberRamesh.Tsi10-Dec-06 21:19 
QuestionHow to find the rejected recipient addresses? Pinmemberanees7728-Jun-06 0:15 
Generalsame error Pinmemberlakshmi patil28-Dec-05 23:52 
GeneralCould not access 'CDO.Message' object Pinmemberhany mahmoud14-Sep-05 23:52 
Questionwhy can't send out my Email? Pinsusslideyong26-Apr-05 19:10 
AnswerRe: why can't send out my Email? PinmemberSpring Boy27-Apr-05 22:58 
AnswerRe: why can't send out my Email? PinmemberGaurav.Tekkie20-May-10 22:14 
QuestionHow to read from a database and send to more than one email address Pinmemberpaulpinder3-Dec-04 4:19 
AnswerRe: How to read from a database and send to more than one email address Pinmembermosn842-Jan-08 17:38 

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
Web01 | 2.8.150414.1 | Last Updated 25 Nov 2002
Article Copyright 2002 by eightfour
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid