Advertisement
Tech

How to Send email with ASP.NET Using C#

If you are creating a .net web site or application, it might be important to learn how to send emails from within your web site. There are several different reasons this could come in handy, so keep reading to find out more.

By Mohammad
Desk Tech
Reading time 3 min read
Word count 523
Web development Internet Coding tutorials
How to Send email with ASP.NET Using C#
Advertisement
Quick Take

If you are creating a .net web site or application, it might be important to learn how to send emails from within your web site. There are several different reasons this could come in handy, so keep reading to find out more.

On this page

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…

Advertisement

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.

Advertisement

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.

Advertisement

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.

Advertisement

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

Notice we are going to be passing, for arguments.

Advertisement

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.

Advertisement

MailMessage msg = new MailMessage();

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

Advertisement

msg.Subject = strSubject; //from argument

msg.IsBodyHtml = isHTML; //from argument

Advertisement

msg.Body = strMessage; //from argument

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

Advertisement

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

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

Advertisement

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.

Advertisement

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

clsEmail.SendEmail(“Bob@yahoo.com ”,“Hello”,“Hello Bob, how are you?",True | False);

Advertisement

Thats all you have to do to send the email. Email will go to Bob@yahoo.com 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.

Keep Exploring

More from Tech

Filed under
Web development Internet
More topics
Coding tutorials
Advertisement