Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   Sending Emails in ASP.NET (http://www.go4expert.com/articles/sending-emails-aspnet-t2364/)

decodec 28Dec2006 06:07

Sending Emails in ASP.NET


A very common and extremely useful functionality in Web Developement is sending emails from a Web Application. Some of the very common purposes of sending emails from a web application are, to send feedback to the webmaster of a site or informing a forum member of new post in a thread they subscribed - like Go4Expert, the list is never ending.

In the .NET Framework the task of sending an email is made very easy. In order to send an email message we need to use the SmtpMail class found in the System.Web.Mail namespace, which contains a static method Send.The simplest way to send an email message is to call the Send method passing an instance of the MailMessage class, the MailMessage class is also found in the System.Web.Mail namespace and it represents an email message.

Creating a MailMesaage

The MailMessage class contains properties very similar to CDONTS (the frequently used component used in ASP to send mails).Such properties include: To, From, Cc, Bcc, BodyFormat, Subject, Priority, Body, etc. So, to send an email, we should create an instance of the MailMessage class and set its properties.

'Create an instance of the MailMessage class

Dim oMM as New MailMessage()


'Set the properties
oMM.To = "deepz@go4expert.com"
oMM.From = "sam@go4expert.com"
'If you want to CC this email to someone
oMM.Cc = "naveen@codingforums.com"

'If you want to BCC this email to someone
oMM.Bcc = "pallav@globaldevelopers.net"

'Send the email in text format
oMM.BodyFormat = MailFormat.Text
 '(to send HTML format, you need to change MailFormat.Text to MailFormat.Html)
 'Set the priority - options are High, Low, and Normal
 oMM.Priority = MailPriority.Normal
'Set the subject
oMM.Subject = "Hello there!"

'Set the body - use VbCrLf to insert a carriage return
oMM.Body = "Hi!" & vbCrLf & vbCrLf & "Sending an Email from ASP.NET"

Note: You will need to import the System.Web.Mail namespace in your ASP.NET Web page in order to be able to utilize the above code, like this: <% @Import Namespace="System.Web.Mail" %>

Sending the MailMessage

Once we have created the MailMessage class instance with all the required properties, sending the email message is a piece of cake. We just need to call the static method Send in the SmtpMail class, passing the MailMessage class instance.


' Send the message, using the Send method of the SmtpMail class

The SmtpMail class uses the SMTP Service built into Windows 2000 to do the work of actually sending the email message. By using the Send method above, the local SMTP server is used to send the email message. In order to specify a different SMTP mail server to use to send the message, set the SmtpServer property:


SmtpMail.SmtpServer = emailServerNameorIP
Be careful, though. Since this is a static property, changing this value in one ASP.NET Web page will affect all ASP.NET Web pages using sending emails via the SmtpMail object. That is, if in one ASP.NET Web page you set the SmtpServer property to myNewMailServer, and then, in another ASP.NET Web page, do not specify the SmtpServer property, intending to use the default SMTP server, myNewMailServer will be used instead. For that reason, you should probably always specify the SmtpServer property - if you want to use the default SMTP server, simply do:


SmtpMail.SmtpServer = ""

pradeep 28Dec2006 17:13

Sending Mails with Attachments
With ASP.Net, it is easy to use the class MailAttachment to send mails with attachments. The following code demonstrates how an attachment can be added to the MailMessage.


<% @Page Language="C#" %>
 <% @Import Namespace="System.Web.Mail" %>
 MailMessage msgMail = new MailMessage();
 msgMail.To = "postmaster@go4expert.com";
 msgMail.From = "aspMan@go4expert.com";
 msgMail.Subject = "Mail With Attachment";
 msgMail.BodyFormat = MailFormat.Text;
 msgMail.Body = "Sent a mail with an attachment!";
 msgMail.Attachments.Add(new MailAttachment("F:\\file\\totalPosts.xls"));

haileyjonathan 16Mar2009 12:33

Re: Sending Emails in ASP.NET
I have a little bit of knowledge about ASP.NET... But I read this thread I gathered some information about coding...thank you verymuch....

All times are GMT +5.5. The time now is 12:31.