Implementation of the email sending function on the web page

Source: Internet
Author: User
Tags mailmessage

【Abstract】 using web pages to send emails from Web servers is not only private, but also intuitive, convenient, and fast. This article uses ASP. NET launched by Microsoft to implement the above functions.
【Key words】 webpage email
Although Web websites already provide many interactive methods, such as chat rooms, message boards, and forums, such interactive methods are not private and cannot be used in all cases; although traditional Client email sending and receiving software solves the privacy problem, it lacks the intuitive, convenient, and fast features, which is especially inconvenient for Internet access in public, using web pages to send emails from the Web server can solve the preceding problems.
The implementation of the above functions is related to the operating system used by the Web server. If you use Unix, you only need to call the Unix sendmail command in Perl, but the UNIX system does not use much on the campus network; if ASP is used for implementation, the specific operating platform must be considered. In Windows 2000 and NT, you can use cdnots. newmail is used to send e-mail, but Windows 95/98 does not have this object. Therefore, you can only use the mailto hyperlink to call outlook, thus losing the meaning of sending emails online.
Since Microsoft launched ASP. NET, the transfer feature of E-MAIL has been designed as Web Program To solve the problem.
I. Development Platform:
Currently, Asp.. Net supports the following development platforms: Windows NT4.0 (Service Pack 5 or later is required), Windows 2000 (server or professional), and Windows 9X. We recommend that you use Windows 2000 Server for development, this document uses the development platform as an example.
Necessary components for installing ASP. NET include:
* IIS 5.0, which is installed by default on Windows 2000 Server;
* For Internet Explorer 5.5 or later, the default version of Windows 2000 Server is 5.0. XML support is still insufficient in this version. It is recommended to upgrade to Internet Explorer 5.5 or later.
*. NET Framework and. NET Framework SDK. They are the most critical component for executing ASP. NET. The download URL is as follows:
Http://msdn.microsoft.com/cownloads/default.asp
In addition, because ASP. NET's default text encoding is a ISO8859-1 and does not display Chinese correctly, you can use the UTF-8 encoding method in the <configuration> module in the web. config file to display Chinese.
<System. Web>
<Globalization requestencoding = UTF-8"
Responseencoding = "UTF-8"/>
</System. Web>
II. Implementation Method:
1) directly use the smtpmail class to send simple Emails:
The smtpmail class is system. web. the most basic class in the mail namespace, which is the core class for implementing the email sending function. No matter how complicated the email is to be sent, it is finally sent through the send method in the smtpmail class.
The statement for declaring a namespace is:
<% @ Import namespace = "system. Web. Mail" %>
The send method has two basic usage methods, but when sending a simple email, you only need to call the smtpmail class:
Smtpmail. Send (from, to, subject, message)
The four parameters in the brackets indicate the sender's email address, recipient's email address, subject, and content.
Generally, an email contains four parts. Therefore, if the program needs to send such a simple email frequently, it is best to call the class to complete the purpose of reducing program code.
The method is to read the mailto, mailfrom, mailsubject, and mailbody attributes of the form from the webpage, specify the to, from, subject, and body attributes of the mail object respectively, and finally call smtpmail. send an email.
2) use the mailmessage object to send complex Emails:
A Practical email system should also contain many other contents, such as sending to multiple recipients, using different mail formats, and sending attachments. smtpmail alone cannot be used, system. web. the mailmessage class in the mail namespace.
To use the mailmessage class, you must first create a mailmessage class instance.
Mailmessage mymsg = new mailmessage ();
Then set the attributes of mailmessage to implement various functions of email.
Among the common attributes of the mailmessage class, in addition to the basic from, to, subject, and body, it is worth noting that the mailformat attribute uses mailformat. text can be set to plain text, using mailformat. HTML can be set to HTML format.
You can add two single-choice buttons on the webpage, and modify the Property setting program:
If format. selecteditem. Text = "plain text format" then
Mail. bodyformat = mailformat. Text
Else
Mail. bodyformat = mailformat. html
End if
Mail. Body = mailbody. Value
Sometimes we see the sender's name rather than the email address when receiving the email. To achieve this, you only need to change the email address to the "name <email address>" format.
If the same Email needs to be sent to multiple recipients, you can separate all email addresses with commas (,), for example:
Mail. To = "xhyjl <xhyjl@pub.tz.jsinfo.net>", "Arcadian <arcadian@sohu.com>"
For others, you can set the CC, BCC, bodyencoding, and priority attributes of mailmessage as long as you set the recipient of the copy, the recipient of the copy, the encoding method of the mail content, and the priority of the mail, this article will not be detailed because it is rarely used.
After setting the attributes of mailmessage, the next step is to convert the mailmessage object into an email. The send method of the smtpmail class is used to complete this task, but the send method has only one parameter:
SMTP. Send (mailmessage)
For this example: smtpmail. Send (mymsg );
So far, a more extensive email sending program has been completed.
3) use the attachments attribute of the mailmessage object to send an email with an attachment:
In the past, it was relatively difficult to send email attachments on web pages, because no matter what kind of web dynamic technology can only operate files on the local server, but not files on the client, however, to send an email attachment, you must first upload the attachment to the server, Asp. NET provides the built-in file upload function, so it is no longer difficult to send email attachments.
The steps for ASP. NET to send email attachments are as follows:
* Upload the file to the server;
* Create a mailattachment class instance and reference the uploaded file;
* Add the created mailattachment class instance to the mailmessage class attachments attribute by using the add method, and send it together with mailmessage.
The specific method is as follows:
First, add an input box to the webpage:
<Input type = "file" id = "Attach" runat = "server">
Before sending an email, use the file name obtained by <input type = "file"> to generate a mailattachment object and add it to the attachments attribute of the mailmessage object:
Mail. attachments. Add (New mailattachment (attach. postedfile. strfilename ))
At this point, the attachment combination of the mail has been completed, and the program finally uses the SMTP. Send method to send the mailmessage class with attachments in the form of mail.
Iii. Run the test:
The following is a complete example of sending mail:
<% @ Page Language = "vb debug =" ture "%>
<% @ Import namespace = "system. Web. Mail" %>
<HTML>
<Body>
<H2> welcome to use Asp.net to send an email <HR> </H2>
<Form runat = "server">
<Table border = 1>
<Tr> <TD> recipient </TD>
<TD> <input type = "text" id = "mailto" size = 30 runat = "server"/> </TD> </tr>
<Tr> <TD> sender </TD>
<TD> <input type = "text" id = "mailfrom" size = 30 runat = "server"/> </TD> </tr>
<Tr> <TD> topic </TD>
<TD> <input type = "text" id = "mailsubject" size = 30 runat = "server"/> </TD> </tr>
<Tr> <TD> content </TD>
<TD> <textarea id = "mailbody" rows = 10 Cols = 40 runat = "server"/> </textarea> <br>
Add attachment: <input type = "file" id = "Attach" runat = "server">
</TD> </tr>
</Table>
<Input type = "button" value = "send" id = "sendbutton" onserverclick = "sendbutton_click runat =" server "/>
</Form>
<HR>
<Asp: Label id = "label1" runat = "server"/>
</Body>
</Html>
<Script language = "VB" runat = "server">.
Sub sendbutton_click (sender as object, e as eventargs)
Dim mail as mailmessage = new mailmessage ()
'The following describes how to set attributes of the mailmessage object.
Mail. To = mailto. Value
Mail. From = mailfrom. Value
Mail. Subject = mailsubject. Value
Mail. attachments. Add (New mailattachment (attach. postedfile. filename ))
Mail. Body = mailbody. Value
'Specify the default SMTP Server
Smtpmail. smtpserver = ""
'Start sending emails
Smtpmail. Send (Mail)
Label1.text = "the email is sent successfully !"
End sub
</SCRIPT>
This program is installed on the Web server of the Windows 2000 server operating system, and creates a virtual directory pointing to this aspx file to connect to the Internet. The client operating system is Windows 98. You can easily send emails by running this program in your browser.

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.