Use ASP. NET to send e-mail

Source: Internet
Author: User
In this article, we will demonstrate how to write asp.net to send emails. This will discuss such as email format, priority level, attachment and email encryption.
Asp.net uses SmtpMail objects to send emails. The SmtpMail object sends emails in the following steps.
# Import mail-related namespaces.
# Create a message object and set its attributes.
# Send an email using the 'send' method of the SmtpMail object instance.

Now let's take a step-by-step look at the process of sending emails from the asp.net page.
1. Import the namespace
On the asp.net page, import the System. Web. Util namespace, which contains all the objects required for sending emails. These objects are:

Object Introduction
Smtpmail Declare the mail system to send the mail.
Mailmessage Declares a message, including the sending Address, receiving address, and other attributes.
Mailformat Declare the Message format-HTML, text, and so on.
Mailattachment Declare the attachment of the email.
Mailencoding Enum Declared encryption method: base64 or uuencode.
Mailpriority Enum Set mail priority: Value: High, bottom, General

<% @ Import Namespace = "System. Web. Util" %>

II. Initialize MailMessage object
Use the following statement to initialize the MailMessage object.
Dim mailObj AS new MailMessage
The MailMessage object has the following attributes.

Attribute Description
From Sender's email address
To Recipient's email address
Subject Email Subject
Body Email content
CC List of recipients copied on the email
BCC List of recipients blind-copied on the email
Priority Email priority
High, bottom, average
Bodyencoding Encrypted email content
Base64 or uuencode
Bodyformat Content format
HTML or text
Attachments Attachment List

The following lines of code demonstrate how to use attributes of the MailMessage object. He described the Message to be created, and the message is sent using the SmtpMail object. In our example, mailObj is an instance of the MailMeessage object.

MailObj. From = "abc@mydomain.com"
MailObj. To = Request. Form ("")
MailObj. Subject = "subject of the mail"
MailObj. Body = "Message of the mail"

3. Send email
Finally, send the mail through the 'send' method of the SmtpMail object. The following code is used to send emails.
SmtpMail. Send (mailObj)
Finally, we will apply the above applications to a complete example.
The following is written in Asp.net + 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)
{

MailMessage mailObj = new MailMessage ();

// Set the email address 'from' and 'to'
MailObj. From = Request. Form ("From ");
MailObj. To = Request. Form ("");

MailObj. Subject = "Subject Of the Mail ";
MailObj. Body = "Body of the Mail ";

// Optional: html-format Email
MailObj. BodyFormat = MailFormat. Html;

// Optional: encrypt the email.
MailObj. BodyEncoding = MailFormat. Base64;

// Optional: set the priority of the email to high.
MailObj. Priority = MailPriority. High;

// Optional: attachment
// Note that a MailAttachment object is created to append a file to the email.
MailObj. Attachments. Add (new MailAttachment ("c: \ test.doc "));

// Use the smtpmail object to send the mail.
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>

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.