Griffiths web design based in Royston, Hertfordshire, United Kingdom


Sending E-Mails in the code behind with ASP.NET using C#

Author:   Michael Griffiths

Date created:   02 February 2016

[l]I was recently asked, several times by varying different people, how to send emails in ASP.NET. When I explained to them how to do so they were amazed at how simple it was. I thought I would share with you this little nugget of wisdom as it is a very common feature of nearly all websites out in the wild. In this tutorial we will be creating a new website but if you are just looking to add the email feature to an existing site then please skip the first step. [b]The steps:[/b] Create a new empty website. Name it and save it. I have called mine ‘sendemail’ but you can name yours whatever you like. Next we need to add a web form. So go over to the solution explorer and right click on the website, a menu will appear, click ‘Add’ then ‘Add New Item’ (or ctrl + Shift + A). Select webform from the list, name it, then click ‘Add’ Now we have our webform we need to add some mark up to the page. Nothing complicated just some text inputs, labels and a button, like so: [code]

[/code] Add the following using statements in your code behind: [code] using System.Net; using System.Net.Mail; using System.Text.RegularExpressions; [/code] Next add the following code to the click event of the button: [code] protected void btnSend_Click(object sender, EventArgs e) { // create variables and assign values string Name = txtFirstName.Text; string CompanyName = txtCompanyName.Text; string PhoneNumber = txtPhone.Text; string EmailAddress = txtEmail.Text; string SendAddress = @""; string Subject = txtSubject.Text; // preserve mew lines in the body of the message string Message = Regex.Replace(txtMessage.Text, @"\r\n?|\n", "
"); // create new message MailMessage msg = new MailMessage(); msg.To.Add(new MailAddress(@"")); msg.Subject = Subject; msg.From = new MailAddress(SendAddress); msg.IsBodyHtml = true; msg.Body = "

Contact Name: " + Name + "
"; msg.Body += "Company Name: " + CompanyName + "
"; msg.Body += "Phone Number: " + PhoneNumber + "
"; msg.Body += "Email Address: " + EmailAddress + "
"; msg.Body += "Message:
" + Message + "

"; // set the reply to address to the address entered in the form msg.ReplyToList.Add(new MailAddress(EmailAddress)); // set up smtp client and credentials SmtpClient smtpClnt = new SmtpClient( smtpClnt.Credentials = new NetworkCredential(EmailAddress and password goes here); // send the message smtpClnt.Send(msg); // inform user that message has sent lblError.Text = "Message sent!"; } [/code] Now run your application and test it out. For the sake of security I have removed my details from the example . If you have any comments or question then please leave me a comment below, or if preferred you can head over to the contact page and fill in the form. I would like to add that the contact form on this very site uses the same script to send the results directly to my email. I hope this helps somebody and thank you for taking the time to read. Please share it around as much as you can. Any and all shares/tweets/etc. are very much appreciated. [/l]

About the author

My name is Michael Griffiths, I am a website designer and developer and the owner of Griffiths Web Design. I am based in Royston, Hertfordshire. Most of my work is local, Hertfordshire, Cambridgeshire, Bedfordshire, Essex etc. but that’s not to say I won’t work elsewhere. One of the wonders of modern technology is remote working. So no matter where the client is located it is always possible to connect.

I specialise in creating ASP.NET webforms applications using C#. I use Microsoft SQL Server for database development and on the front end I use Html, CSS and JavaScript. I am also proficient in the use of jQuery and several CSS frameworks.

One of my greatest passions in life is learning. Which is why I chose to pursue a career in web design and development, technology is in a state of constant and rapid evolution which means there is always something new to learn. I also enjoy writing, both fiction and non-fiction.


Please feel free to leave a comment below:

Sandy cady

Thank you for your effort, Could you please ttell me why you use the "@" symbol. I also want to know why you use those <>. thanks

Michael Griffiths

Hello Sandy. The @ symbol is used so you don't have to escape special characters. As for the angle brackets Ive used many so you'll have to be a little more specific Hope this helps

Griffiths Web Design Logo by Michael Griffiths
Griffiths Web Design Logo by Michael Griffiths
Click to go to the top of the page