Friday, June 26, 2009

Sending Email Via SMTP mail Server

To send emails your SmtpServer should be running. This can be checked using the Internet Services Manager found in Start->Programs->Administrative Tools. When you open the Internet Services Manager you can see whether the Default SMTP Virtual Server is running or not. If the ‘Play’ icon in the toolbar is disabled it means that the Default SMTP Virtual Server is running. You can also check by Right clicking the Default SMTP Virtual Server and see whether ‘Start’ is disabled. If it is so then the Default SMTP Virtual Server is running. This SMTP Service is shipped with Windows XP Professional and Windows 2000 by default.

It is better to test your SMTP Server whether it functions properly before developing any application in ASP.Net to send emails. The easiest way to test the SMTP Server is to open a text editor like Notepad and write,

To: someperson@somesite.com
From: me@mysite.com
Subject: Testing SMTP Server in my system.

Make sure that you are typing some valid email address for the To and From fields in the text file. Now save this text file in c:\inetpub\mailroot\pickup folder. Once you save this file it will disappear indicating that the mail has been sent. If the SMTP Server is not functioning properly it will not disappear indicating that the mail is not sent. The SMTP Service uses the c:\inetpub\mailroot folder for all the mail services.

Now we will see how to send mails from your web application. Create a web application using your Visual Studio.Net IDE. Now drop four text fields in the design page and make the fourth field to accept multi-line content by setting the TextMode property to MultiLine. Drop down a submit button which can be used to send mails when clicked. Now double click the button on the form to open the code window and write code in the Button1_Click() event to send emails. The steps involved in code are:

1. Import the System.Web.Mail namespace
2. Create an instance of the MailMessage class
3. Set the properties of the MailMessage class like To, From, Subject, and Body
4. Use the Send method of the SmtpMail class to send the email message by passing the instance of the MailMessage class as the parameter to the Send method.

The code for sending emails will look somewhat like this after you follow the above steps:

using System.Web.Mail;

protected MailMessage emailMesg;

emailMesg = new MailMessage();
emailMesg.From = txtFrom.Text;
emailMesg.To = txtTo.Text;
emailMesg.Subject = txtSubject.Text;
emailMesg.Body = txtBody.Text;
emailMesg.Priority = MailPriority.High;
SmtpMail.Send(emailMesg);
By default the mail messages are sent as Plain text. It is possible to send mails in the HTML format which allows you to sent mails as web pages which is rich in look. To send emails in the HTML format you need to set the BodyFormat property of the MailMessage instance to MailFormat.Html. If you need to send mail in HTML format the above code would be added one more line like,

emailMesg.BodyFormat = MailFormat.Html;

The Body property of the MailMessage instance would be in the HTML code as given in the following example.


The above HTML code will send the mail message in white color with Maroon background color for the page. You can also send attachments with an email message. For sending attachments you have to use the MailAttachment class available. The code given below will add a file as attachment to the email message.

protected MailAttachment emailAttach;
emailAttach = new MailAttachment(@"path-to-the-document");
emailMesg.Attachments.Add(emailAttach)

This code has to be given before using the Send method. The path to the document to be attached can also be given from the File Input field in the HTML code.

Thus it is easy to send emails through a web page in your web application. You may try to send emails by following the above methods in your websites

1 comment:

  1. Thanks a lot for this code.

    suppose I want to display the email body in letter format then how can I do it.

    And also wil you please give code for playing audio files which are at server.

    ReplyDelete

 
Locations of visitors to this page