Send E-mail From Your Web Page

E-mail without exposing your address to spammers

asp.netNOW Q&A

 

Send E-mail From Your Web Page

E-mail without exposing your address to spammers

 

By Josef Finsel

 

Editor's Note: For more detailed info on e-mailing from ASP.NET, check out the ClassAct column in the September 2002 issue of asp.netPRO.

 

Q. How do I set up a Web page to send e-mail? I want a way for people to contact me without exposing my e-mail address to spammers.

- SK from Willmar, Minn.

 

A. Setting up a Web page to send e-mail actually requires two pieces. The first is a Simple Mail Transfer Protocol (SMTP) server. SMTP is the part of Internet e-mail that sends e-mail across the Web to your mailbox. Fortunately, there is an SMTP server included with Microsoft Internet Information Server (IIS). The first step to sending e-mail from a Web page is to make sure your SMTP server is set up. From the Microsoft Management Console (MMC), you should find an SMTP virtual server. As long as this service is started, you can connect to the local server. Or, if you have an SMTP server on your network, you can use that instead. You can find everything you need for e-mail in System.Web.Mail.

 

You can send e-mail in two ways. First, you can use SmtpMail.Send(from, to, subject, message). This sends a basic text e-mail. If you want something more involved, you'll need to look at the MailMessage object.

Like SmtpMail.Send, the MailMessage object has To, From, Subject, and Body properties. But it also has some other options that enable more advanced e-mails. Let's take a look at a couple of these.

 

BodyFormat enables you to send HTML-formatted e-mails. BodyEncoding allows you to use Unicode for international text. Priority is an enumeration that let's you specify Normal, Low, or High priority.

To simplify sending more complex e-mails that include attachments and a priority, I've provided a function with parameters that allows you to send e-mails with one simple call (see Figure 1). You can download this function (and a sample Web page); see end of article for details.

 

private void SendEmail(string strFromEmailAddress,string strToEmailAddress,

         string strSubject , string strEmailText , string strAttachments,

MailPriority Priority )

{

  //SendEmail is used privately within the Web form to send e-mails

  MailMessage objMessage = new MailMessage();

  objMessage.BodyFormat = MailFormat.Html;

  objMessage.To = strToEmailAddress;

  objMessage.From = strFromEmailAddress;

  objMessage.Priority = Priority;

  objMessage.Subject = strSubject;

  objMessage.BodyFormat = MailFormat.Html;

  objMessage.Body = strEmailText;

  // Check to see if attachments need to be sent

  // Attachments would be the path/file name of the attachment

  if (strAttachments != "")

  {

   objMessage.Attachments.Add(strAttachments);

  }

  //Send the message

  SmtpMail.Send(objMessage);

}

Figure 1. This basic function creates and sends an e-mail.

 

The Attachments property is actually a collection of filenames and paths. If you are attaching e-mails, it's important to remember that the Web server needs to have read rights to the file in order to be able to send it as an attachment.

 

To send the message, use the SmtpMail object. By default this object uses the local server address to find an SMTP server. If you want to use another SMTP server, you simply need to specify the name or IP address of the server using the SmtpMail.SmtpServer property.

 

The file referenced in this article is available for download.

 

Have a question? Send it to [email protected].

 

Josef Finsel is a software consultant with G.A. Sullivan, a global software development company. He has published a number of VB and SQL Server-related articles and is working on the syntax for FizzBin.NET, a programming language that works the way programmers have always suspected. He's also author of The Handbook for Reluctant Database Administrators (ISBN 1893115909).

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish