Pin Me

Learn to Send Emails with ASP.NET (C#)

written by: Mohammad•edited by: Michele McDonough•updated: 2/12/2010

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.

  • slide 1 of 2

    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.

  • slide 2 of 2

    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(“info@YourWebSiteDomain.com"); 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("Bob@yahoo.com","Hello","Hello Bob, how are you?",True | False);

    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.






© Copyright 2016 brighthub.com.