Complete example of sending email in ASP. NET

Source: Internet
Author: User
Tags mailmessage
This article provides examples to illustrate the many possibilities of sending emails in ASP. NET, covering the e-mail format, priority, attachment and email encoding.

ASP. NET is assigned a new email sending object named smtpmail. When using an smtpmail object to send an email from an ASP. NET page, follow these simple steps:

▲Contains The namespace required for mail-related classes;
▲Shows an information object and sets attributes;
▲Use the send method of the smtpmail object instance to send an email.

Now let's take a step-by-step look at the process of sending emails from an ASP. NET page. We use VB to illustrate this example. The complete code of VB and C # will be included.

Step 1: Include namespace

Introduce the system. Web. util namespace in the ASP. NET page. This namespace includes all the objects required to send an email. These objects are:

Smtpmail: indicates the mail system, which is used to send emails.
Mailmessage: indicates a message. Its attributes include the sender address and recipient address.
Mailformat: indicates the format of the information, such as HTML and text.
Mailattachment: indicates an email attachment.
Mailencoding Enum: represents any encoding of base64 or uencode. Optional values: base64 and uuencode.
Mailpriority Enum: Used to set priority for information. Value: High, low, and general.
<% @ Import namespace = "system. Web. util" %>

Step 2: demonstrate the mailmessage object

Use the following statement to demonstrate mailmessage objects:

Dim mailobj as new mailmessage

Use the attributes of the mailmessage object to prepare the email. The mailmessage object has the following attributes:

From: The sender's email address.
To: Recipient's email address
Subject: Email Subject
Body: Email Subject
Cc: list of recipients copied by email
BCC: list of recipients of the email message.
Priority: Priority of information: High, low, or general
Bodyencoding: the encoding of the information body. If yes, It is base64 or uuencode.
Bodyformat: Information Format: HTML or text
Attachments: List of mailattachment objects appended to an email. It is mainly a reference to this object set.

The following code demonstrates how to use the attributes of the mailmessage object. They represent the information that will be created in this example. This information must be sent using the smtpmail object. In this example, mailobj references the example of an information object:

Mailobj. From = "abc@mydomain.com"
Mailobj. To = request. Form ("")
Mailobj. Subject = "Subject of the mail"
Mailobj. Body = "message of the mail"

Step 3: Send email

Then, we can use the send method of the smtpmail object to send the mail:

Smtpmail. Send (mailobj)

Complete instance

Finally, we combine the attributes described above into a complete example. To illustrate all the possibilities of sending an email using ASP. NET, we also include some tips ". The following is a complete example of using VB. NET:

<% @ Page Language = "VB" %>
<% @ Import namespace = "system. Web. util" %>
<HTML> <body>
<Script language = "VB" runat = "server">
'This method is called on the server when the submit
'Button is clicked on the client and when the page
'Posts back to itself
Sub Sendmail (OBJ as object, e as eventargs)
'Instantiate A mailmessage object. This serves as a message object
'On which we can set properties.
Dim mailobj as new mailmessage
'Set the from and to address on the email
Mailobj. From = request. Form ("from ")
Mailobj. To = request. Form ("")
Mailobj. Subject = "Subject of the mail"
Mailobj. Body = "Body of the mail"
'Optional: HTML format for the email
Mailobj. bodyformat = mailformat. html
'Optional: encoding for the message
Mailobj. bodyencoding = mailformat. base64
'Optional: set the priority of the message to high
Mailobj. Priority = mailpriority. High
'Optional: attach a file to the email.
'Note here that we have created a mailattachment object
'Attach a file to the email
Mailobj. attachments. Add (New mailattachment ("C:/test.doc "))
'Send the email using the smtpmail object
Smtpmail. Send (mailobj)
End sub
</SCRIPT>
<Asp: Label id = "headingmsg" 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>

In the preceding example, the email addresses of from and to are collected from the corresponding text box. When you click "send mail, the email is sent. When the "send mail" button is clicked, the form is handed back to itself. The "Sendmail" program on the server is triggered and the mail is sent. The following example uses C:

<% @ Page Language = "C #" %>
<% @ Import namespace = "system. Web. util" %>
<HTML> <body>
<Script language = "C #" runat = "server">
// This method is called on the server when the submit
// Button is clicked on the client and when the page
// Posts back to itself
Public void Sendmail (Object OBJ, eventargs E)
{
// Instantiate a mailmessage object. This serves as a message object
// On which we can set properties.
Mailmessage mailobj = new mailmessage ();
// Set the from and to address on the email
Mailobj. From = request. Form ("from ");
Mailobj. To = request. Form ("");
Mailobj. Subject = "Subject of the mail ";
Mailobj. Body = "Body of the mail ";
// Optional: HTML format for the email
Mailobj. bodyformat = mailformat. html;
// Optional: encoding for the message
Mailobj. bodyencoding = mailformat. base64;
// Optional: set the priority of the message to high
Mailobj. Priority = mailpriority. High;
// Optional: attach a file to the email.
// Note here that we have created a mailattachment object
// Attach a file to the email
Mailobj. attachments. Add (New mailattachment ("C: // test.doc "));
// Send the email using the smtpmail object
Smtpmail. Send (mailobj );
}
</SCRIPT>
<Asp: Label id = "headingmsg" 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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.