Sending E-mail Using ASP.NET


Description and steps:

1. I have created “EmailHelper” class which is very self explanatory.

2. Include the namespace “System.Net.Mail”.

using System.Net.Mail;

Please note: We can include “System.Web.Mail” or “System.Net.Mail“. These 2 namespaces contains classes used to send an email to a Simple Mail Transfer Protocol (SMTP) server for delivery.. But since “System.Web.Mail” is deprecated, It is recommended to use “”System.NET.Mail”.

Even if you can use “System.Web.Mail”, it is not a full .NET native implementation of the SMTP protocol. Instead, it uses the pre-existing COM functionality in CDONTS. System.Net.Mail, in contrast, is a fully managed implementation of an SMTP client.

3. In your Web.config file, include following lines.


<system.net>
<mailSettings>
<smtp from="defaultEmail@yourdomain.com" deliveryMethod="SpecifiedPickupDirectory">
<network host="localhost" port="25" userName="UserName" password="Password"/>
<specifiedPickupDirectory pickupDirectoryLocation="c:\Emails\"></specifiedPickupDirectory>
</smtp>
</mailSettings>
</system.net>

4. Create “Emails” folder on your C drive. P.S. You can create folder anywhere on your system, but path of the folder should be same as like in the web.config file.

5. Code snippet to send mail using “EmailHelper” class.


EmailHelper emailHelper = new EmailHelper("test@email.com", "recepient@email.com", null, null, "This email was sent from my asp.net application", string.Format("Email was sent on {0}<br/></b>This is bold text.</b><br/><I>This is italic</i>", DateTime.Now), true, System.Net.Mail.MailPriority.High);
emailHelper.SendMailMessage();

6. If you are running it locally, you need to configure SMTP on your system. If you don’t know how to do it, please refer the related article links at the bottom of this post.

EmailHelper Class Explained:

1. It contains 2 constructors. First one is default and other one is parameterised.

2. If you use the parameterised constructor while creating instance of the class, you can send mail as shows above by very simple way with only 2 lines of code.

3. It also contains a SendMailMessage method with 1 overload. First one with no input parameters and other one is with multiple parameters.

4. If you want to send emails to multiple users (e.g 2 users in TO, 2 users in CC, 3 users in Bcc etc), then SendMailMessage with multiple input parameter method is for you. Pass the list of persons as per your requirement and call the method.

5. It has one extra method called “IsValidEmailAdress”. It validates email ID’s using regular expressions.

Downloads:

Github [Repository Link]

Box.com [Direct Link to Zip file]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s