Click here to Skip to main content
13,406,131 members (49,945 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 15 Nov 2005

Send mail through .aspx C#

, 15 Nov 2005
Rate this:
Please Sign up or sign in to vote.
How to send an email in Dot Net

All the Contact Us forms needs an e-mail system

In this article you will learn how to send emails from you .aspx site, using plain format, html format also add attachments by two ways, the quick (easy way) and the profesional ( i like this way ) The first step is be sure that the SMTP Server is locally installed at SMTP Service of the IIS

Easy Way

This is an easy and quick way to do that, we will call this form ContactMe.aspx First, we have to import the System.Web.Mail Then in the code behind add the namespace

using System.Web.Mail;
Now build your message
string To      = "";
string From    = "";
string Subject = "The Famous";
string Body    = "Hello World";


What is the SmtpMail?

this class has a method called Send that allows the user to send an email, in this way we set four parameters:

SmtpMail.Send("","","The Famous","Hello World");

As you can see this is the easy and quick way to do that. Now you will lear how to do the professional way (interesting). Here you will send an email including an attachment and CC, we can use the same parameters above

Professional Way (I like this Way)

We have to create an object (MailMessage) called myMail
MailMessage myMail = new MailMessage();

myMail.To       = To;
myMail.Cc    = "";
myMail.From    = From;
myMail.Subject    = Subject;

myMail.BodyFormat  = MailFormat.Html; // here you can choose: Plain or HTML

string body = "<html><body> Bolded message <br> <p>The Message</p> </body></html>";

myMail.Body    = body; //set the body message

myMail.Attachments.Add(new MailAttachment("c:\\filename.jpg")); //add the attachment

Now go and check your inbox and see what happend.

So what I did in this code?

As you can see I used an enum from MailMessage class (MailFormat) this enum has 2 options: Text and HTML, I set the HTML and build the Body Message.

You will not receive a value about the sucess of the email message. The reason is that the email is writted into the Inetput folder, also if the mail fails you can go and check the Badmail folder into inetpub, this way if you set an invalid To or From email, the message will be set in this folder.


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 Softtek GDC Ensenada
Mexico Mexico
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
MohdWaseem1-Jun-13 21:12
memberMohdWaseem1-Jun-13 21:12 
Generale_mail question Pin
sholar14-May-09 5:00
membersholar14-May-09 5:00 
Generalsmtp local server Pin
Aco Antevski17-Mar-09 11:39
memberAco Antevski17-Mar-09 11:39 
Questionsend mail Pin
rakeshgiri124-Oct-08 21:41
memberrakeshgiri124-Oct-08 21:41 
Generali have a big problem Pin
khaled Elbadri15-Sep-08 10:00
memberkhaled Elbadri15-Sep-08 10:00 
QuestionHow to send Email in Out look Express Using ASP .NET 2.0 Pin
itee2-May-08 20:16
memberitee2-May-08 20:16 
Questionhow to send newsletter or email in groups Pin
Malakool12-Nov-07 20:53
memberMalakool12-Nov-07 20:53 
Generalsendind email problem Pin
ruchiverma31-May-07 21:04
memberruchiverma31-May-07 21:04 
AnswerRe: sendind email problem Pin
icantfindanunsedname2-Jul-07 12:19
membericantfindanunsedname2-Jul-07 12:19 
QuestionHow 2 slove this Pin
mani_softeng20-Feb-06 10:49
membermani_softeng20-Feb-06 10:49 
AnswerRe: How 2 slove this Pin
benjarras20-Feb-06 13:32
memberbenjarras20-Feb-06 13:32 
QuestionNo mention about SMTP Server Pin
daniero15-Nov-05 8:53
memberdaniero15-Nov-05 8:53 
AnswerRe: No mention about SMTP Server Pin
Sigurd Johansen4-Dec-05 10:16
memberSigurd Johansen4-Dec-05 10:16 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180221.1 | Last Updated 15 Nov 2005
Article Copyright 2005 by benjarras
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid