how to send email in ASP.NET

No.of Views1715
Bookmarked0 times
Downloads 
Votes0
By  NikhilJohari   On  16 Feb 2010 03:02:18
Tag : ASP.NET , Miscellaneous
how to send email in ASP.NET
emailbookmarkadd comments print

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

 In this article we will create a tool by which we can send the email to one person, we will not use BCc and Cc.So get ready to create a simple e-mail sending tool in asp.net.

First type this code in source code window to create the user interface of email-sending tool by using html code .

Default.aspx

When you are look designed GUI lokk like followings,

Image Loading

Now in design window double click the button (Send) to give the code,Before writing this code you have to specify a namespace which is

using System.Net.Mail;

Default.aspx.cs

protected void btnSend_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
client.Port = 587;
client.Host = “smtp.gmail.com”;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential(txtemail.Text ,txtpass.Text );
client.EnableSsl = true;
try
{
MailAddress SendTo = new MailAddress(txtTo.Text);
MailAddress SendFrom = new MailAddress(txtFrom.Text);
message = new MailMessage(SendFrom, SendTo);
message.Body = txtMessage.Text;
message.Subject = txtSub.Text;
client.Send(message);
txtStatus.Text = “Your email has been sent”;
Page.RegisterClientScriptBlock(”Thanks!”, “alert(’Thanks! for using this tool.’)”);
}
catch (Exception ex)
{
txtStatus.Text = ex.ToString();
}
}
}

 

Now we have created a tool for sending e-mail through asp.net,but by using this tool we can only send one email at a time we can’t send mails to BCc or Cc ,in my next upcoming articles i will show you how to send BCc and Cc email by doing a bit changes in this code and after that blog i will show you how to send attachments by just adding some more lines of code.
It is really very fun work and very important also.


Now run this code and fill all the Text Boxes right and then check your email id you will get there a send or inbox mail,And try to do write this code by your own in spite copy and paste this code.

Conclusion

In this article, you have learn , how to send email using .NET namespace in ASP.NET. i hope this help ASP.NET beginners.

 
Sign Up to vote for this article
 
About Author
 
NikhilJohari
Occupation-
Company-
Member Type-Senior
Location-India
Joined date-15 Aug 2009
Home Page-http://fast-get.com
Blog Page-http://dotnetask.blog.co.in
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</