Everything about sending Emails in ASP.NET 2.0- Part 4
Introduction
In
Part 1,
Part 2
and Part
3
of this series we learnt to send text, HTML and Multipart emails from ASP.NET 2.0.
Up till now we always used the virtual SMTP server of IIS to send our emails. In
many real world cases you may need to send the messages from your own SMTP
server. This article will tell you how to use your own SMTP server while sending
emails. Further, we will see how to send emails asynchronously.
Configuring SMTP Host
The SmtpClient class that we used to send emails has several properties that
allow us to configure the SMTP Host. SMTP host is a server that you use for
sending email messages. The following table lists some of the important
properties and methods of the SmtpClient class.
Property/Method |
Description |
Host |
This property indicates the name or IP address of the server that is
acting as the SMTP host |
Port |
This property indicates the post (default is 25) of the host that
will be used while sending emails |
Credentials |
This property represents the user id and password that is to be used
for authenticating the sender. The property can accept an instance of
System.Net.NetworkCredential class. |
DeliveryMethod |
This property indicates the mode of delivery of the email. The
property is an enumeration of type SmtpDeliveryMethod. The possible
values are: Network - Email is sent directly over a network
PickupDirectoryFromIis - Emails are picked up from the Pickup folder of
IIS Virtual SMTP Server. The default location is C:\inetput\mailroot\pickup
SpecifiedPickupDirectory - Emails are picked up from a specified folder
as indicated by PickupDirectoryLocation property. |
PickupDirectoryLocation |
When DeliveryMethod is set to SpecifiedPickupDirectory; this
property indicates the Pickup folder from where the email messages are
picked up. |
EnableSsl |
Indicates whether to use Secure Socket Layer (SSL) while sending the
email messages |
Send |
Sends the email message in synchronous manner. |
SendAsync |
Sends the email message in asynchronous manner. |
SendCompleted |
This event is raised when the asynchronous send operation is over. |
SendAsyncCancel |
This method cancels the asynchronous operation initiated by
SendAsync |
Timeout |
Indicates the timeout value in milliseconds when you send emails
asynchronously. |
Example - Sending emails asynchronously via remote SMTP host
Let's develop a web form that allows us to specify HOST information in
addition to normal From, To, Subject and Message. You will find the complete
source code for download along with this article. In the code behind of the web
form you will see the following code:
...
SmtpClient client = new SmtpClient();
client.Host=TextBox7.Text;
NetworkCredential credentials =
new NetworkCredential(TextBox8.Text, TextBox9.Text);
client.Credentials = credentials;
client.SendCompleted+=new
SendCompletedEventHandler(client_SendCompleted);
client.SendAsync(msg, null);
...
Here, we first create an instance of SmtpClient class. We set its Host
property to the remote SMTP server's name or IP address e.g. mail.somedomain.com.
We then create an instance of NetworkCredential class and pass the host user
name and password to it. These details are required by the SMTP host for
authenticating the sender. We then set the Credentials property of SmtpClient
class to this instance.
In this example we want to send the email message asynchronously and hence we
will be using SendAsync() method. We also want to get a notification when the
send operation is complete. We achieve this by handing the SendCompleted event
of SmtpClient class. The event handler of this event can be any function (client_SendCompleted
in our example) that accepts two parameters:
- First parameter of type object indicating the object that triggered this
event
- Second parameter of type AsyncCompletedEventArgs. This class gives you
access of the state information passed during send operation (see below).
We finally call SendAsync() method by passing two parameters:
- The email message to be sent
- An object indicating the state information being passed to the
SendCompleted event. In our example we pass null but it could have been any
valid object
Our SendCompleted event handler function looks like this:
private void client_SendCompleted
(object sender, AsyncCompletedEventArgs e)
{
lblMsg.Text = "Mail sent successfully!";
}
In the event handler we simply display a success message in the event
handler.
Summary
Email sending has become more advanced and flexible in ASP.NET 2.0. In this
series we discovered how to create messages, embed images and configure SMTP
host to send emails from ASP.NET applications. Though we used ASP.NET for
building our client applications you can very well use Windows Forms or console
applications instead.