5,427,303 members and growing! (19,562 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner

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

By eightfour

This sample shows you how to send e-mails over SMTP using the Mail-Class in C# and ASP.NET.
C#.NET 1.0, .NET 1.1, Win2K, WinXP, Windows, .NET, ASP.NET, Visual Studio, VS.NET2003, Dev

Posted: 24 Nov 2002
Updated: 24 Nov 2002
Views: 164,542
Bookmarked: 85 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
18 votes for this Article.
Popularity: 4.30 Rating: 3.43 out of 5
4 votes, 22.2%
1
1 vote, 5.6%
2
3 votes, 16.7%
3
2 votes, 11.1%
4
8 votes, 44.4%
5

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

About the Author

eightfour


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
Occupation: Web Developer
Location: Germany Germany

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
Subject  Author Date 
Generalcan't received email although follow all the step of your guide .anybody help me ! plz .memberfantasy20080822:46 10 Jul '08  
Generalcan't able to send email [modified]memberSanjay k Jain2:19 4 Jun '08  
Generalhai again this is paulrajmemberPaul's7:46 19 Nov '07  
Generaltransport error code 0x800ccc15memberPaul's7:36 19 Nov '07  
GeneralRe: transport error code 0x800ccc15memberThe Geek3:57 25 Mar '08  
QuestionRe: transport error code 0x800ccc15memberSaurabh Tripathi0:31 30 Apr '08  
Generalcould not acess cdo.Messagememberkoorasrinivas22:42 8 Jan '07  
GeneralCould not access "CDO.Message"memberRamesh.Tsi21:19 10 Dec '06  
QuestionHow to find the rejected recipient addresses?memberanees770:15 28 Jun '06  
Generalsame errormemberlakshmi patil23:52 28 Dec '05  
GeneralCould not access 'CDO.Message' objectmemberhany mahmoud23:52 14 Sep '05  
Generalwhy can't send out my Email?susslideyong19:10 26 Apr '05  
GeneralRe: why can't send out my Email?memberSpring Boy22:58 27 Apr '05  
GeneralHow to read from a database and send to more than one email addressmemberpaulpinder4:19 3 Dec '04  
GeneralRe: How to read from a database and send to more than one email addressmembermosn8417:38 2 Jan '08  
GeneralOpen Server RelaymemberChris Porter6:57 20 Aug '04  
Generalcannot send email using SMTP????memberchoonchi7:04 26 Dec '03  
GeneralRelaying vs. Authenticationmemberyigitatli18:19 25 Aug '03  
GeneralRe: Relaying vs. AuthenticationmemberMojtaba Vali20:34 10 May '06  
GeneralAbout multi-sendingmemberNilzoneTW6:48 3 Jul '03  
GeneralRe: About multi-sendingmemberpraetorion23:16 1 Jun '05  
GeneralError :- Could not access 'CDO.Message' object.memberdeepmala1:41 25 Mar '03  
GeneralRe: Error :- Could not access 'CDO.Message' object.memberAgyklon1:09 4 Apr '03  
GeneralRe: Error :- Could not access 'CDO.Message' object.memberdeepmala18:52 6 Apr '03  
GeneralRe: Error :- Could not access 'CDO.Message' object.memberScottyWakefield20:24 9 Dec '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Nov 2002
Editor: Nick Parker
Copyright 2002 by eightfour
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project