Use ASP. NET to build a complete email Sending System

Source: Internet
Author: User
Tags mailmessage
Use ASP. NET to build a complete email Sending System
If you want to use ASP for an e-mail sending system, you must first think of components developed by a third party, but this will consume a lot of money. Of course, you can also use the built-in Windows Object-cdonts. newmail object to send e-mail, which is free of charge, but is very dependent on the operating platform, cdonts. the newmail object only exists under Windows 200 and NT, and does not exist under Windows 95/98.
Since Microsoft launched ASP. NET, sending e-mail has become the basic object of WEB programming. This article will discuss how to build a fully functional e_mai sending system.

I. Development and operation platform
Windows 2000 Professional,. NET Framework beta 2

II. what kind of email sending system is a very complete function system only when one email sending system has: the email sending system is a complete system for recipient addresses, sender addresses, cc addresses, bcc addresses, email topics, email subjects, and attachments. Specific functions are shown in:



The following describes how to implement these functions.

Iii. Specific implementation steps
First, we need to introduce a class library (Class Library) -- system. Web. Mail sent by e-mail in the. NET Framework SDK. This class library provides all the objects, attributes, and Methods sent by e-mail. The most important objects are mailmessage objects and smtpmail objects. The mailmessage object is mainly used to encapsulate the structure of e-mail. Such as the recipient address and sender address. The smtpmail object mainly sends the defined mailmessage object. The following describes some important steps:
1. To define the mailmessage object, you must first create a mailmessage object. The following statement creates a mailmessage object.
<% @ Import namespace = "system. Web. Mail"
Dim mailobj as new mailmessage () %>

2. With a mailmessage object, we will define the mail structure below. We define the order of the mail structure one by one.
1>. Mail sending Address:
Mailobj. form = "Sending address"
2>. Target email address:
Mailobj. To = "Destination Address"
If you want to send it to multiple people, separate each email address with a comma.
3>. CC address of the email:
Mailobj. Cc = "CC address"
Similarly, if you want to send it to multiple people, you can write multiple email addresses on the CC address, but separate them with commas.
4>. email Bcc address:
Mailobj. bcc = "BCC address"
The BCC address is similar to the CC address.
5>. Subject:
Mailobj. Subject = "Email Subject"
6>. Email content:
Mailobj. Body = "email content"
7>. Mail priority:
Mailobj. Priority = mailpriority. Normal. This defines the mail priority as normal. You can also define it as high or low)
8>. Define the mail format:
Mailobj. bodyformat = mailformat. Text '. This defines the email as a text format. You can also define the email as a hypertext format. You only need to change mailformat. Text to mailformat. html.
9>. Add an attachment to the email:
Anyone who has used ASP knows how difficult it is to browse files. However, after ASP. NET appears, all of this becomes simple. With only one line of code below, you can select any file on the drive.
<Input id = "emailfile" type = "file" runat = "server" size = "40"/>

3. Send email
Smtpmail. smtpserver = "" 'specifies the default SMTP Server
Smtpmail. Send (mailobj) 'send e-mail

4. complete source code of the program
<% @ Import namespace = "system. Web. Mail" %>
<Script language = "VB" runat = "server">
Sub sendbutton_click (sender as object, e as eventargs)
'Create a mailmessage object
Dim mailobj as new mailmessage ()
'The following describes how to set attributes of the mailmessage object and some general judgments of the program.
'Set the mail sending Address
If emailfrom. Text <> "" Then mailobj. From = emailfrom. Text
'Set the destination email address
If emailto. Text <> "" Then mailobj. To = emailto. Text
'Set the CC address of the email.
If emailcc. Text <> "" Then mailobj. Cc = emailcc. Text
'Set the email Bcc address
If emailbcc. Text <> "" Then mailobj. bcc = emailbcc. Text
'Set the mail format to text format. To set it to Ultra text, change mailformat. Text to mailformat. html.
Mailobj. bodyformat = mailformat. Text
'Set the mail priority, which can be high (high), low (low), normal (normal)
Mailobj. Priority = mailpriority. Normal
'Set the Email Subject
Mailobj. Subject = emailsubject. Text
'Set the email content
Mailobj. Body = emailbody. Text
'Add an attachment to the email
Dim strfilename as string
Strfilename = emailfile. postedfile. filename
If strfilename <> "" Then mailobj. attachments. Add (New mailattachment (strfilename ))
'Specify the default SMTP Server
Smtpmail. smtpserver = ""
'Send emails now
Smtpmail. Send (mailobj)
Panelsendemail. Visible = false
Panelmailsent. Visible = true
End sub
<Asp: Panel id = "panelsendemail" runat = "server">
<Form method = "Post" enctype = "multipart/form-Data" runat = "server">
<H2> welcome to ASP. NET for sending e-mail </H2>
<B> enter the email address: </B>
<Asp: textbox id = "emailfrom" size = "30" runat = "server"/>
<B> enter the email destination address: </B>
<Asp: textbox id = "emailto" size = "30" runat = "server"/>
<B> enter the CC address: </B>
<Asp: textbox id = "emailcc" size = "30" runat = "server"/>
<B> enter the email address: </B>
<Asp: textbox id = "emailbcc" size = "30" runat = "server"/>
<B> enter the subject: </B>
<Asp: textbox id = "emailsubject" size = "30" runat = "server"/>
<B> enter the Email Subject: </B>
<Asp: textbox id = "emailbody" textmode = "multiline"
Columns = "40" rows = "10" runat = "server"/>
<B> Add the attachment name: </B>
<Input id = "emailfile" type = "file" runat = "server" size = "40"/>
<Asp: button runat = "server" id = "sendbutton" text = "send"
Onclick = "sendbutton_click"/>
</ASP: Panel>
<Asp: Panel id = "panelmailsent" runat = "server" visible = "false">
Your email has been sent successfully. Thank you for using it again!
</ASP: Panel>


5. Now a complete email sending system is complete. If your machine reaches the running environment mentioned above, create a virtual directory pointing to this aspx file, connect to the Internet, and run this program in the browser, you can easily complete the email sending.

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: 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.