CodeSnip: Sending E-Mail Using SmtpMail
 
Published: 17 Aug 2004
Unedited - Community Contributed
Abstract
See how simple, powerful, and easy-to-use the SmtpMail object is from within .NET.
by Steven Swafford
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 17617/ 22

The System.Web.Mail Namespace

The System.Web.Mail namespace contains all the necessary classes that will allow you to create and send out email messages to whom you may desire. You can use this functionality for sending a user a forgotten password, sending out newsletters, or sending updates concerning a product or website. This is an efficient way to automate a process that may otherwise be long and boring.

Face it, if you have a subscription service and users forget the password assigned to them (we all know this happens more often than not) then you could utilize a database on your backend to store relevant user information and generate a Web form that provides users a self-service option for retrieving a password.

Class Purpose and Usage
1. MailAttachment -
Provides properties and methods for constructing an e-mail attachment.
2. MailMessage - Provides properties and methods for constructing an e-mail message.
3. SmtpMail - Provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component.

The various enumerations in this namespace allow you to establish the email encoding type, the email format, and finally the priority for the email message you wish to send--all very important features!

Be sure to read the specifics of the System.Web.Mail Namespace.

Sending an E-mail from an ASP.NET Page

Import System.Web.Util namespace in the ASP.NET Page. This namespace contains all the objects necessary to send an e-mail.

  • SmtpMail represents the mail system for sending emails.
  • MailMessage represents a message, with properties such as sender, address, recipient address, and so on.
  • MailFormat represents the format of the message: HTML, Text, and so on.
  • MailAttachment represents an e-mail attachment.
  • MailEncoding enum represents any encoding such as Base64 or UUencode.
  • MailPriority enum is used to set priority for the message. Values: High, Low, Normal .

Instantiate the MailMessage object:
Dim mailObj AS new MailMessage

Prepare the mail using the MailMessage object properties:
mailObj.From = "someone
@somewhere.com"
mailObj.To = Request.Form ("to")
mailObj.Subject = "subject"
mailObj.Body = "Message"

Now here we send the email:
SmtpMail.Send(mailObj)

VB.NET Example:

<@page language="VB"> 
<@Import Namespace="System.Web.Util">
<HTML>
<BODY>


<SCRIPT LANGUAGE="VB" RUNAT="server">

Sub SendMail (Obj As Object, E As EventArgs)
Dim mailObj AS new MailMessage


mailObj.From = Request.Form("From")
mailObj.To = Request.Form("To")
mailObj.Subject = "Subject"
mailObj.Body = "Body"
mailObj.BodyFormat = MailFormat.Html
mailObj.BodyEncoding = MailFormat.Base64
mailObj.Priority = MailPriority.High


' attach a file to the email
mailObj.Attachments.Add(new MailAttachment("c:\test.doc"))


SmtpMail.Send(mailObj)


End Sub

</SCRIPT>
<asp:label ID="lblMsg" Text="Enter Your Email Address:" RUNAT="server"/>


<FORM METHOD="post" RUNAT="server">
Email Recipient: <INPUT TYPE="text" NAME="to"> <br>
Email Sender: <INPUT TYPE="text" NAME="from">
<INPUT TYPE="submit" NAME="Submit" VALUE="Send Mail" RUNAT="server" OnServerClick="SendMail">
</FORM>
</BODY>

Now, for the example in C#:

<<a href="mailto:%@page">%@page</a language="C#"> %>
<<a href="mailto:%@Import">%@Import</a Namespace="System.Web.Util"> %>
<HTML><BODY>
<SCRIPT LANGUAGE="C#" RUNAT="server">

public void SendMail (Object Obj, EventArgs E)
{
MailMessage mailObj = new MailMessage();


mailObj.From = Request.Form("From");
mailObj.To = Request.Form("To");
mailObj.Subject = "Subject";
mailObj.Body = "Body";
mailObj.BodyFormat = MailFormat.Html;
mailObj.BodyEncoding = MailFormat.Base64;
mailObj.Priority = MailPriority.High;


// attach a file to the email
mailObj.Attachments.Add(new MailAttachment("c:\\test.doc"));


SmtpMail.Send(mailObj);
}

</SCRIPT>
<asp:label ID="lblMsg" Text="Enter Your Email Address:" RUNAT="server"/>
<FORM METHOD="post" RUNAT="server">
Email Recipient: <INPUT TYPE="text" NAME="to"> <br>
Email Sender: <INPUT TYPE="text" NAME="from">
<INPUT TYPE="submit" NAME="Submit" VALUE="Send Mail" RUNAT="server" OnServerClick="SendMail">
</FORM>
</BODY>



User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 9:12:12 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search