How to Send email with ASP.NET Using C#

Coding Your Website with Email Functionality

Sending emails from a website is very common nowadays. For example one web site can have “Send to Friend” form. Once a visitor fills out this form and hit the “Send” button, there is a lot that goes on behind the scene and that is what we are going to be learning today.

But first…

Let’s talk about what will be the routine if none of the web sites can send out emails. Let’s assume visitor one, (John) comes to a web site and he likes what this web site offers, and he would love to pass this URL to his buddy Bob. What if the web site does not offer a “Send to Friend” form? John would have to copy the URL, then open up his email client, log in, create a new email, write email to Bob, paste the URL of the site and send an email. This is not a friendly option, and keep in mind many visitors might not do this. So, to help visitors and to help the web site owner, ASP.Net allows webmasters to write code that can send out emails easily without any effort on the visitor’s part.

Let’s See the Code…

Now we are going to talk about how emails are sent when a visitor clicks on “Send” button. This example uses ASP.NET 2.0 (C#) coding standard.

Following are some steps you need to follow.

Step 1. Create a class clsEmail by right clicking on the project in solution explorer and add a new class object.

Step 2. Make use your class is using the Net.Mail liabrary. using System.Net.Mail;

Step 3. We now need to create a public method SendEmail within clsEmail class.

public static void SendEmail(string strTo, string strSubject, string strMessage, bool isHTML)

Notice we are going to be passing, for arguments.

strTo = recipient’s email address, strSubject = subject of the email, strMessage = the message body we want to email and isHTML = true / false to indicate if we want this specific email to go out as HTML content type or not.

Step 4. Now write logic within SendEmail method.

MailMessage msg = new MailMessage();

msg.From = new MailAddress(“”); msg.To.Add(strTo); //from argument

msg.Subject = strSubject; //from argument

msg.IsBodyHtml = isHTML; //from argument

msg.Body = strMessage; //from argument

SmtpClient smtp = new SmtpClient(“YourSMTPServer”); smtp.Credentials = new System.Net.NetworkCredential(“YourSMTPServerUserName”, “YourSMTPServerPassword”);

smtp.Send(msg); //send email out

msg.Dispose(); //get rid of the object

In the code above, you would need to change FROM email address to match your web site contact address; you also need to change your SMTP server address and username / password associated with your account / domain.

You can find your SMTP information from your hosting control panel.

Once the code is in clsEmail class, now here is how you can call the method.

clsEmail.SendEmail("","Hello","Hello Bob, how are you?",True | False);

Thats all you have to do to send the email. Email will go to with the subject "Hello" and body "Hello Bob, how are you?". If you pass "True" then this body will be HTML content or by passing "False" it will be plain-text.