1, Default.aspx code is as follows:
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default" validaterequest= " False "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<table id= "TABLE1" runat= "server" border= "0" cellpadding= "0" cellspacing= "0" >
<tr>
<TD style= "width:393px" >
Receiving letter: <asp:textbox id= "TextBox1" runat= "Server" ></asp:textbox><br/>
Topic: <asp:textbox id= "TextBox2" runat= "Server" ></asp:textbox><br/>
Content: <asp:textbox id= "TextBox3" runat= "Server" height= "154px" textmode= "MultiLine"
Width= "336px" ></asp:textbox><br/>
<asp:button id= "Button1" runat= "Server" text= "send" onclick= "Button1_Click"/></td>
</tr>
</table>
</div>
<table id= "Table2" runat= "server" border= "0" cellpadding= "0" cellspacing= "0" visible= "false" >
<tr>
<TD align= "center" style= "width:400px" >
<asp:label id= "Label1" runat= "Server" forecolor= "Red" text= "Congratulations, publish success!" ></asp:label><br/>
<asp:button id= "Button2" runat= "Server" text= "return" onclick= "button2_click"/></td>
</tr>
</table>
</form>
</body>
2, Default.aspx.cs code is as follows:
Copy Code code as follows:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Pour into namespaces
Using System.Net;
Using System.Net.Mail;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Button1_Click (object sender, EventArgs e)
{
Set up sender's mailbox and display name
MailAddress from = new MailAddress ("zgdx0503@cpp114.com", "Yanbian Adult College Information Center");
Set the recipient's mailbox and display the name
MailAddress to = new MailAddress (TextBox1.Text, "Class 0503");
Create a MailMessage object
MailMessage omail = new MailMessage (from, to);
Omail.subject = TextBox2.Text; Message headers
Omail.body = TextBox3.Text; Message content
Omail.isbodyhtml = true; Specify message format, support HTML format
omail.bodyencoding = System.Text.Encoding.GetEncoding ("GB2312");/the encoding used by the message
Omail.priority = mailpriority.high;//The priority of the message is high
Send mail server
SmtpClient client = new SmtpClient ();
Client. Host = "mail.cpp114.com"; Specify mail server
Client. Credentials = new NetworkCredential ("zgdx0503@cpp114.com", "123456");//specify server mail, password
Send
Try
{
Client. Send (Omail); Send mail
Label1.Text = "Congratulations!" The message was sent successfully. ";
}
Catch
{
Label1.Text = "Mail failed to check if the network and mailbox are available." ";
}
Omail.dispose (); Releasing resources
TABLE1. Visible = false;
Table2.visible = true;
}
protected void button2_click (object sender, EventArgs e)
{
Go back, continue sending
Response.Redirect (Request.Url.ToString ());
TABLE1. Visible = true;
Table2.visible = false;
}
}
3, run and enter the test box zgdx0503@tom.com, (Password: 123456). As shown below:
4, open the mailbox view