By Faisal Khan.
Overview
In this Tutorial We Will Learn How To Send E-mails of Both text and html formats using classes of system.web.mail namespace.
BEFORE DELVING in To The Source Code, I'll Briefly Explain The MailMessage and SMTPMAIL CLASESS Which We'll BE Using to Send E-MAILS.
System.Web.mail.mailMessage
An Object of this class represents a single e-mail message. Properties of this Class Are Listed Below:
Properties of MailMessage
To Use this class, we create a new object, set some of it's profib. ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
MailMessage Mail = New MailMessage ();
Mail.to = "Receivers@emil.address";
Mail.from = "senders@emil.address";
Mail.BodyFormat = mailformat.text;
Mail.subject = "Subject of this email";
Mail.Body = "Body of this message."
SMTPMAIL.SEND (MAIL);
All the code required to create and send a MailMessage object has been shown above. If you've understood this code then you do not need to go any further. Just change the values to suite your needs. In the rest of this article we Will Create a Sendmail.aspx Page Which Will Make Use of the Above Code.
High-End Commercial Email Component: i. Aspemail: MOST Feature-Rich Mailing Component. Supports message Queueing, Embedded Images, Secure Mail, Authentication. Read more.
System.Web.mail.smtpmail
This class is used to * send * e-mails (MailMessages) using SMTP protocol. All we are interested as far as this tutorial is concerned are the two static methods (Send ()) it provides. You just use the static method, provide A Valid MailMessage Object As the Argument, And send it! .properties and methods of smtpmail
Sendmail.aspx
IT IS TIME NOW THAT Page Which Provides A Form To You, You Enter The VALUES AND HIT The "Send" Button to send the message. A confirmation message is kiln.
Create a new .aspx page and save it as sendmail.aspx in a folder accessible by your server. Now copy and paste the folload code in it:
<% @ Page language = "c #"%>
<% @ Import namespace = "system.web.mail"%>
Protected Void Page_Load (Object Sender, Eventargs E) {
IF (! page.ispostback)
Message.text = "Sending E-mail Using ASP.NET";
}
Protected void send_email (Object sender, eventargs e) {
MailMessage Mail = New MailMessage ();
Mail.to = Request.form ["to"];
Mail.From = Request.form ["from"];
IF (Request.form ["Format"]. Equals ("text")))
Mail.BodyFormat = mailformat.text;
Else
Mail.BodyFormat = MailFormat.html;
Mail.subject = Request.form ["Subject"];
Mail.Body = Request.form ["Body"];
SMTPMAIL.SEND (MAIL);
Response.flush ();
Message.Text = "Message Sent ...
"
" Go back ";
}
script>
A {font-size: 8pt; font-family: tahoma; font-weight: Normal; Color: maroon;
style>
hEAD>
p>
<% IF (! page.ispostback) {%>
all fields are required. p>