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

Send mail through .aspx C#

By , 15 Nov 2005
 

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      = "cgodinez@technomex.net";
string From    = "mymail@mail.com";
string Subject = "The Famous";
string Body    = "Hello World";

SmtpMail.Send(From,To,Subject,Body);

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("mymail@mail.com","cgodinez@technomex.net","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    = "acarboncopy@cc.com";
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

SmtpMail.Send(myMail);
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.

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

benjarras
Web Developer Softtek GDC Ensenada
Mexico Mexico
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generale_mail questionmembersholar14 May '09 - 4:00 
pls sir,this has been delaying my final year project ,my question is dat do i have to be connected or if i have installed the smtp it will send the message.thanks
Generalsmtp local servermemberAco Antevski17 Mar '09 - 10:39 
how can i configure my personal computer to send emails.
I have IIS 7 on Windows Vista and SMTP E-mail is turned on.
Questionsend mailmemberrakeshgiri124 Oct '08 - 20:41 
please tell me how to send mail in asp.net using html form with c# as language
Generali have a big problemmemberkhaled Elbadri15 Sep '08 - 9:00 
i used the code,and it was working fine
but now it didn't send any more
what is the problem
QuestionHow to send Email in Out look Express Using ASP .NET 2.0memberitee2 May '08 - 19:16 
Sir,
Kindly sugest me how to send email in out look express using ASp.NET 2.0 .
 
Itu
Questionhow to send newsletter or email in groupsmemberMalakool12 Nov '07 - 19:53 
hi
 
i have four tables one table Newsletter (that contain newsletterID, newsletter title and body)
second table NewsletterSubscriber table (that contain SubscriberID and Email ID)
third table MemberGroup (that contain groupId and Group name)
 
fouth table subscribergroup(that contain subgrId , SubscriberID,groupId )
 
in datagrid as we submit email address to group its SubscriberID,groupId goes to fouth table ..
 
now i want to send newsletter in groups so plz help me.......
its really urgent
thanks in advance;););)
 
malakool@gmail.com
Generalsendind email problemmemberruchiverma31 May '07 - 20:04 
using this code,i am able to send email thru c# code.but it worked only once, and now it is not sending emails anymore.plz help..
AnswerRe: sendind email problemmembericantfindanunsedname2 Jul '07 - 11:19 
There is a free email plug-in for the server at http://www.neokernel.com that lets you specify the smtp server to send with, it might help you out.
 
marcus

QuestionHow 2 slove thismembermani_softeng20 Feb '06 - 9:49 
when i add attachment to html mail message i got this error
can u please guide me 2 slove this
 

System.Web.HttpException: Invalid mail attachment 'C:\Documents and Settings\pradeep\Desktop\Today's Work.txt'. at System.Web.Mail.MailAttachment.VerifyFile() at System.Web.Mail.MailAttachment..ctor(String filename) at ODanielHonda.requestform.Submit_Click(Object sender, EventArgs e)
 
hi
AnswerRe: How 2 slove thismemberbenjarras20 Feb '06 - 12:32 
I guess you have to check the string file name, eg: you can't use this simbol ( ' ), remeber that it's a string name, so you need to change the file name or remove the ' simbol.
 
MyText.txt - correct
MyText's - wrong
 
.Net Developer ** VS2005

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

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