Introduction
Sending email is becoming a minimum requirement for any web application in these days. Microsoft Visual Studio .Net 2005 aimed to reduce the code we write. Which results the birth of numerous powerful packages.Here I will show three approaches for this,
- System.web.mail ASP.NET 1.1
- System.net.mail ASP.NET 2.0 and
- Using Microsoft Outlook
Note That the above three methods can only send email. To read email you either need a Mime parsing component such as aspNetMime or a POP3 component such as aspNetPOP3.
Before You Start
If you are not a using ASP.NET 2.0 then you can do it using DotNetOpenMail.dll API, which I had discussed in my previous article Sending Emails using .NET Part I please go through it if you are not using ASP.NET 2.0. Check if the Microsoft SMTP Server is turned on. To do this either open the Internet Services Manager directly or open Computer Management and Navigate to Internet Information Services -> Default SMTP Virtual Server and checkout if the button with the 'play' icon on it is disabled, that means it is already started.
The SmtpServer property This mail will by default be sent through the local SMTP server. You can specify a different SMTP server, by setting the SmtpServer string property on the SmtpMail class. Since SmtpMail is a static class and SmtpServer is a shared static property, once this property has been set, it will be used for all other calls to the SmtpMail.Send method, even in different web applications.
1) System.Web.Mail
The System.Web.Mail namespace has three classes.
- SmtpMail
- MailMessage
- MailAttachment
The SmtpMail is a static class. That is, we do not need to instantiate an object from this class. That is, this class is Ready Made; you can directly invoke the methods as it is static
System.Web.Mail.SmtpMail.Send ("senderID@domainName.com",
"receiverID@domainName.com","Subject of the Mail",
" Message Body");
Using System.Web.Mail;
<% @Page Language="C#" %>
<% @Import Namespace="System.Web.Mail" %>
<%
MailMessage message = new MailMessage ();
message. To = "receiverID@domainName.Com";
message. From = "sender@domainName.com";
message. Subject = "Email Subject";
message.BodyFormat = MailFormat.Html;
msgMail.Body = "<html><body><h2" +
" align = center>Hello World!" +
" </h2></body></html>";
SmtpMail.Send (message);
Response. Write ("Email Sent");
%>
VB.NET Code
<%@Page Language="VB" %>
<% @Import Namespace= "System.Web.Mail" %>
<%
Dim message As MailMessage
message = New MailMessage ()
message. To = "receiverID@domainName.Com"
message. From = "sender@domainName.com"
message. Subject = "Email Subject"
message.BodyFormat = MailFormat.Html
msgMail.Body = "<html><body><h2 " +
"align = center>Hello World! " +
"</h2></body></html>";
SmtpMail.Send (message)
response. Write ("<BR><font color=red" +
" face=verdana size=2> " +
"Sent the mails. </font>")
%>
Attaching The Files to your Message
Here is the sample code to attach a sample text file
MailAttachment attachment1 =
new MailAttachment (@"c:\My Documents\OfficeFile1.doc");
MailAttachment attachment2 =
new MailAttachment ("d:\\Documents\\asp.netTurorial.doc");
message.Attachments.Add (attachment1);
message.Attachments.Add (attachment2);
SmtpMail.Send (message);
2) Using System.Net.Mail [ASP.NET 2.0]
For the users who are using ASP.NET 1.1 System.Web.Mail will work but it is deprecated in current version. We can say that System.Net.Mail is the replacement of System.Web.Mail as it comes with more features such as authentication, that is from what domain name and with what user id the use is sending the emils. This is done using System.Net.NetworkCredential as you will see in below code. The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and specify the user credentials necessary to send authenticated email.
C#.NET CODE
The Using Statement
using System.Net.Mail;
<span style="font-size: 9pt;"> </span>
MailMessage message = new MailMessage("senderID@domainName.com",
"receiverID@domainName.com","subject of the Message ",
"body fo the message ");
Attachment attachment = new Attachment("sample.doc",
MediaTypeNames.Application.Octet);
message.Attachments.Add(attachment);
SmtpClient smtpClient = new SmtpClient("Your SMTP Server");
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("userID",
"password", "domainName");
smtpClient.Send(message);
VB.NET CODE
The Using Statement
imports System.Net.Mail;
Dim message As New MailMessage("senderID@domainName.com",_
"receiverID@domainName.com","subject of the Message ",_
"body fo the message ")
/* create and add the attachment(s) */
Attachment attachment = new Attachment("sample.doc",
MediaTypeNames.Application.Octet)
message.Attachments.Add(attachment)
/* create SMTP Client and add credentials */
Dim emailClient As New new SmtpClient("Your SMTP Server")
/* Email with Authentication */
Dim SMTPUserInfo As New new NetworkCredential("userID", "password", "domainName")
emailClient.UseDefaultCredentials = False
emailClient.Credentials = SMTPUserInfo
/*Send the message */
emailClient.Send(message)
The Web.config file
<?xml version="1.0"?>
<CONFIGURATION>
<SYSTEM.NET>
<MAILSETTINGS>
<SMTP from="authicationEmailID@yourdomain.com">
<NETWORK password="password"
userName="UserID" port="25"
host="smtp.yourdomain.com"/>
</SMTP>
</MAILSETTINGS>
</SYSTEM.NET>
</CONFIGURATION>
There are other properties that you can set, such as the message priority, whether it should be text or HTML, and the encoding type. More information about these additional properties should be available in the ASP.Net framework documentation. Note That the Send method do not have return values about the success of dispatching the email message. The reason for this is that the emails simply are written into the Pickup folder of the Inetpub directory from where they are read and then sent by the SMTP Service. Failed emails (dispatching errors) also are written into files, and moved to Badmail folder.
3) Using Microsoft Outlook
Using Microsoft Outlook is ingeneral not preferable but just discussing the possible ways of sending emails. Take a look at it.
Step 1: Add a reference to the Outlook library
Step 2: import the Outlook namespace:
Imports Outlook
Step 3: Then, add code to create a new MailItem and set its properties to the information which is already known. Finally, display the MailItem. Here’s the complete code
'Create Outlook application.
OutlookApplication = New Outlook. Application
'Create Outlook MailItem
Dim message As MailMessage message = New MailMessage ()
message. To = "receiverID@domainName.Com"
message. From = "sender@domainName.com"
message. Subject = "Email Subject"
OutlookMailItem = CType (OutlookApplication.CreateItem (
OlItemType.olMailItem), message)
'Display MailItem.
OutlookMailItem.Display ()
Using Outlook is easy but the programmer is the ultimate judge to decide what approach is to be used in his application.
Conclusion
Microsoft .NET is extremely powerful and yet simple to work with. I Hope that you will get a basic understanding of how to create and send email's with ASP.NET 2.0 simple and compact code.
History
Version 1.0 Release 2006