How to send emails using Asp.net

Source: Internet
Author: User
Tags mailmessage
This article explains how to use ASP. NET to send emails, including choosing the email format, priority, attachment, and email internal code.
Now let's gradually explore using ASP. NET to send emails. We will use VB to illustrate the example, but our final code includes VB and C #.
Step 1: Include the namespace
The System. Web. Util namespace is in ASP. NET. This namespace contains all the necessary objects for sending emails.
The object is:
Object Summary
SmtpMail indicates the mail system. Send an email.
MailMessage, such as the sender address and recipient address.
MailFormat, email format-HTML, text, and so on
MailAttachment indicates an email attachment.
MailEncoding enum represents any internal code, such as Base64 or uencode.
MailPriority enum sets mail priority. Value: High, low, normal
<% @ Import Namespace = "System. Web. Util" %>
OK, the first part is complete.

Step 2: MailMessage object example
Declare the MailMessage object.
Dim mailObj AS new MailMessage
MailMessage object attributes:

Attribute description
From sender's address
Recipient's address
Subject
Body
Cc email copy recipient address
BCC email copy recipient address ratio is displayed in the email (BCC)
Priority of a priority email
Bodyencoding email inner code
Bodyformat, in HTML or text format
Attachments email attachment
The following code snippet shows the mailmessage object.
Mailobj. From = "abc@mydomain.com"
Mailobj. To = request. Form ("")
Mailobj. Subject = "Subject of the mail"
Mailobj. Body = "message of the mail"

Use the smtpmail object send method to send emails. The following code snippet is used to send emails
Smtpmail. Send (mailobj)

VB.net complete source code
<% @ Page Language = "VB" %>
<% @ Import namespace = "system. Web. util" %>
<HTML>
<Head>
<Title> welcome to the chenyangasp sample program
</Title>
</Head>
<Body>
<Script language = "VB" runat = "server">
'Access the server when submitting this method.
Sub SendMail (Obj As Object, E As EventArgs)
Dim mailObj AS new MailMessage
'Set the mail sending and receiving address
MailObj. From = Request. Form ("From ")
MailObj. To = Request. Form ("")

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

'Html-format emails
MailObj. BodyFormat = MailFormat. Html

'Set to advanced priority
MailObj. Priority = MailPriority. High

'Add attachments to emails
'Note: here we create a mailattachment object to add an attachment to the email.
MailObj. Attachments. Add (new MailAttachment ("c: \ test.doc "))
'Use the SmtpMail object to send emails
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>

C # complete source code
<% @ Page language = "C #" %>
<% @ Import Namespace = "System. Web. Util" %>
<HTML>
<Head>
<Title> welcome to the chenyangasp sample program
</Title>
</Head>
<BODY>

<Script language = "C #" RUNAT = "server">
// This method accesses the server when it is submitted
Public void SendMail (Object Obj, EventArgs E)
{

MailMessage mailObj = new MailMessage ();

// Set the mail sending and receiving addresses
MailObj. From = Request. Form ("From ");
MailObj. To = Request. Form ("");

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

// Emails in html Format
MailObj. BodyFormat = MailFormat. Html;

// Set it to advanced priority
MailObj. Priority = MailPriority. High;

// Add attachments to emails
// Note: here we create a mailattachment object to add an attachment to the email.
MailObj. Attachments. Add (new MailAttachment ("c: \ test.doc "));

// Use the SmtpMail object to send an email
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.