Author: tigerwen01
In daily development, we often encounter the development of an application module that automatically sends emails to customers. For example, when a customer performs a specific operation or registers or purchases online, the server will send a message to the customer when the customer operation is completed. Automatically send a confirmation email, etc. .NET Framework There are many ways to use sockets to establish a connection with the mail server, and then use the SMTP (Simple Mail Transfer Protocol) service to transmit the mail, but this is quite complicated. Let's use the classes provided by the System.Web.Mail namespace of the .NET framework to complete sending emails, because it is very simple.
The System.Web.Mail namespace provides a simple managed interface for SMTP (Simple Mail Transfer Protocol) and contains many classes, including important classes such as MailMessageMailAttachmentSmtpMail. The MailAttachment class provides properties for constructing email attachments. and methods, the MailMessage class provides properties and methods for constructing e-mail messages, and the SmtpMail class provides properties and methods for sending messages using the Collaboration Data Objects (CDOSYS) messaging component of the host system.
In order to ensure that the program can run correctly, the "relay" method for setting the default SMTP virtual server in IIS is as follows:
Enter the "Internet Information Services" interface, right-click "Default SMTP Virtual Server Properties" and select "Properties" in the pop-up menu. Click the "Relay" button in the "Access" tab, and then add "127.0.0.1" to "IP Address (Mask)/Domain Name" in the "Relay Restrictions" window.
Below is a SendEmail.aspx file, which was developed using the beta version 2 of Visual Web Developer 2005 Express Edition and passed running under Windows2000Server+sp4. For reference:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" " http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd ">
<script runat="server">
void OnSend(Object Sender,EventArgs e) {
MailMessage messge = new MailMessage();
messge.From = Sed.Text;
messge.To = Receiver.Text;
messge.Subject = Subj.Text;
messge.Body = Body.Text;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(messge);
lab.Text="Mail Sent successfully! ";
}
</script>
<html xmlns=" http://www.w3.org/1999/xhtml " >
<head runat="server">
<title>Send Email User Interface</title>
</head>
<body>
<h1> Send Email UI</h1>
<form id="form1" runat="server">
<div>
<hr>
<table cellspacing="8">
<tr>
<td align=right valign=bottom>Sender :</td>
<td><asp:TextBox ID=Sed runat=server/></td>
</tr>
<tr>
<td align=right valign=bottom>Receiver:</td>
<td> <asp:TextBox ID=Receiver runat=server /></td>
</tr>
<tr>
<td align=right valign=bottom>Theme:</td>
<td><asp:TextBox ID=Subj runat= server /></td>
</tr>
<tr>
<td align=right valign=bottom>Content:</td>
<td><asp:TextBox ID=Body TextMode=MultiLine Rows=5 Columns=40 runat= server /></td>
</tr>
</table>
<hr />
<asp:Button Text="Send" OnClick="OnSend" runat=server />
</div>
</form>
<asp:Label ID=lab runat=server/>
</body>
</html>
This article only introduces how to send emails in .NET framework applications, and does not involve a detailed introduction to the classes in the System.Web.Mail namespace. About System.Web.Mail can refer to relevant information.