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>