Constructing complete e-mail sending system with ASP.net

Source: Internet
Author: User
Tags mail mailmessage
asp.net if you want to use ASP to do an e-mail delivery system, you must first think of the use of third-party development of components, but this will cost you a lot of silver. Of course, you can also use Windows with the object--cdonts.newmail object to send e-mail, although it is free, but very dependent on the operating platform, CDONTS. The NewMail object exists only under Windows 200 and NT and is not available under Windows 95/98.
Since Microsoft launched Asp.net,e-mail, it has become the basic object of Web programming. This article is about how to make a fully functional E_mai delivery system.

A Development and Operation Platform
Windows Professional,. Net FrameWork Beta 2

Two What kind of e-mail delivery system is a very complete system only when a mail delivery system has: The recipient address, sender address, CC address, secret address, mail subject, mail subject, and attachment functions, this mail delivery system is a relatively complete system. The specific features are shown in the following illustration:





Here's a detailed description of the steps to implement these features.

Three Concrete implementation Steps
The first step is to introduce a class library (class libraries)--system.web.mail in the. Net FrameWork SDK about e-mail delivery. This class library provides all the objects, properties, and methods sent by e-mail. The most important objects are MailMessage objects and SmtpMail objects. MailMessage objects are mainly packaged e-mail of the various structures. For example: Recipient address, sender address, etc. The SmtpMail object is primarily to send the MailMessage object that has already been defined. Here are some important steps to specify:
1. To define a MailMessage object, you first create a MailMessage object. You can create a MailMessage object by using the following statement.
<% @Import namespace= "System.Web.Mail"
Dim Mailobj as New mailmessage ()%>

2. With a MailMessage object, the structure of the message is defined below. We define one by one according to the order of the message structure above.
1>. send address of message:
mailobj.form= "Send Address"
2> The destination address of the message:
mailobj.to= "Destination Address"
If you want to send to more than one person, separate each email address with a comma.
3&gt the CC address of the message:
mailobj.cc= "CC Address"
Similarly, if you want to send to more than one person, you can write multiple email addresses on the CC address, but separate them with commas.
4&gt The secret address of the message:
Mailobj.bcc= "Secret Send Address"
The secret address and CC address are similar
5&GT; Message Subject:
mailobj.subject= "Mail Subject"
6>. Message content:
mailobj.body= "Mail Content"
7> priority of messages:
mailobj.priority = Mailpriority.normal ' This is to define the priority of the message as normal, and you can also define it as higher (high), lower (low)
8&GT; Define message Format:
Mailobj.bodyformat = Mailformat.text ' This is to define the message as a text format, you can also define the message as hypertext format, only to change the Mailformat.text to mailformat.html.
9> add an attachment to a message:
People who have used the ASP know how difficult it is to look at the file. But after ASP.net appeared, all this became simpler. You can select any file for the drive using only the following line of code.
<input id= "Emailfile" type= "file" runat= "Server" size= ""/>

3. Send e-mail
Smtpmail.smtpserver = ' "' Specifies that the default SMTP server be used
Smtpmail.send (mailobj) ' Send e-mail


Four The 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 are the properties of the set MailMessage object, and some routine judgments of the program.
' Set the sending address of the message
If emailfrom.text<> "" Then Mailobj.from = Emailfrom.text
' Set the destination address of the message
If emailto.text<> "" Then mailobj.to = Emailto.text
' Set the CC address of the message
If emailcc.text<> "" Then mailobj.cc = Emailcc.text
' Set the secret address of the message
If emailbcc.text<> "" Then MAILOBJ.BCC = Emailbcc.text
' Set the message format is a text format, if you want to set the hypertext, the Mailformat.text to mailformat.html
Mailobj.bodyformat = Mailformat.text
' Set message priority, high (height), low (lower), normal (normal)
Mailobj.priority = Mailpriority.normal
' Set Email theme
Mailobj.subject = Emailsubject.text
' Set the content of the message
Mailobj.body = Emailbody.text
' Add an attachment to the message
Dim strFileName As String
Strfilename=emailfile.postedfile.filename
If strfilename<> "" Then MailObj.Attachments.Add (new MailAttachment (strFileName))
' Specifies that the default SMTP server is used
Smtpmail.smtpserver = ""
' Now start sending mail
Smtpmail.send (Mailobj)
Panelsendemail.visible = False
Panelmailsent.visible = True
End Sub
</script>
<body>
<asp:panel id= "Panelsendemail" runat= "Server" >
<form method= "Post" enctype= "Multipart/form-data" runat= "Server" >
<b> Please enter email address:</b>
<asp:textbox id= "Emailfrom" size= runat= "Server"/>
<p>
<b> Please enter the Mail destination address:</b>
<asp:textbox id= "Emailto" size= runat= "Server"/>
<p>
<b> Please enter the message cc address:</b>
<asp:textbox id= "EMAILCC" size= runat= "Server"/>
<p>
<b> Please enter the email address:</b>
<asp:textbox id= "EMAILBCC" size= runat= "Server"/>
<p>
<b> Please enter the message subject:</b>
<asp:textbox id= "EmailSubject" size= runat= "Server"/>
<p>
<b> Please enter the message body:</b>
<asp:textbox id= "Emailbody" textmode= "MultiLine"
columns= "rows=" runat= "Server"/>
<p>
<b> Please add the attachment name:</b>
<input id= "Emailfile" type= "file" runat= "Server" size= ""/>
<asp:button runat= "Server" id= "Sendbutton" text= "Send"
onclick= "Sendbutton_click"/>
</form>
</asp:panel>
<asp:panel id= "panelmailsent" runat= "Server" visible= "False" >
Your email has been successfully sent and you are welcome to use it again!
</asp:panel>
</body>



Five Thus a complete e-mail delivery system is complete. If your machine reaches the operating environment mentioned earlier in this article, create a virtual directory that points to the ASPX file, connect to the Internet, and run the program in the browser, so you can easily complete the email.


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.