Asp. NET send email full instance (turn)

Source: Internet
Author: User
Tags format base64 include mail mailmessage net send
asp.net ASP. NET send email Complete instance

This article illustrates the many possibilities of sending emails in asp.net, covering such aspects as email format, priority, attachment and email encoding.

Asp. NET is given a new object to send email, named SmtpMail. When you use the SmtpMail object to send e-mail from a asp.net page, you can follow these simple steps:

▲ contains the namespaces required for the class of the message;
▲ example an information object, set the attribute;
▲ send a message using the Send method of the SmtpMail object instance.

Now we're going to take a step-by-step look at the process of sending emails from a asp.net page. We use VB to illustrate this example, the final will include VB and C # complete code.

First step: Include namespaces

Introduce the System.Web.Util namespace in the ASP.net page, which includes all the objects necessary to send an email. These objects are:

SmtpMail: Represents the mail system for sending emails.
MailMessage: Represents a message whose properties include the sender address, the recipient address, and so on.
MailFormat: Represents the format of information: HTML, text, and so on.
MailAttachment: Represents an email attachment.
mailencoding enum: Any encoding that represents Base64 or uuencode. Value range: Base64, UUencode
mailpriority enum: Used to set precedence for information. Values are: High, low, general.
<% @Import Namespace = "System.Web.Util"%>

Step Two: Example MailMessage object

Use the following statement to sample the MailMessage object:

Dim Mailobj as New MailMessage

Prepares the message with the properties of the MailMessage object. The MailMessage object has the following properties:

From: Email Address of sender
To: The email address of the recipient
The theme of Subject:email
The main body of the Body:email
Cc:email CC's recipient list
Bcc:email List of recipients to send secretly
Priority: Priority of information: high, low, or general
Bodyencoding: The encoding of an information body, if any, is Base64 or uuencode
BodyFormat: Format of information: Html or text
Attachments: A list of MailAttachment objects attached to an email, mainly a reference to the collection of objects

The following code demonstrates the use of MailMessage object properties, which represent a message that will be created in this example, to be sent using the SmtpMail object. In the example, mailobj references an example of an information object:

Mailobj.from = "Abc@mydomain.com"
Mailobj.to = Request.Form ("to")
Mailobj.subject = "Subject of the Mail"
Mailobj.body = "Message of the Mail"

Step three: Send email

At this point, we can use the Send method of the SmtpMail object to send the message:

Smtpmail.send (Mailobj)

Full instance

Finally, we combine the attributes explained above in a complete example. To illustrate the full possibility of sending an email with asp.net, we also include some "tips". Here is a complete example of using vb.net:

<% @page language= "VB"%>
<% @Import namespace= "System.Web.Util"%>
<HTML><BODY>
<script language= "VB" runat= "Server" >
' This is called on the ' server ' when the submit
' button is clicked on the client and ' 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 ' and ' to address ' email
Mailobj.from = Request.Form ("from")
Mailobj.to = Request.Form ("to")
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 '
Mailobj.priority = Mailpriority.high
' Optional:attach a file to the email.
' Here we are have created a MailAttachment object to
' 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 example above, the email address of the from (sender) and to (recipient) is collected from the corresponding text box, and the message is sent when the "Send Mail" button is clicked. When the Send Mail button is clicked, the form is passed back to itself, the "SendMail" (send mail) program is triggered on the server, and the message is sent. The following are examples of using C #:

<% @page language= "C #"%>
<% @Import namespace= "System.Web.Util"%>
<HTML><BODY>
<script language= "C #" runat= "Server" >
This are called on the server when the submit
The button is clicked on the client and 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" to "address" on the email
Mailobj.from = Request.Form ("from");
Mailobj.to = Request.Form ("to");
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
mailobj.priority = Mailpriority.high;
Optional:attach a file to the email.
Here we are have created a MailAttachment object to
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.