1、Default.aspx代码如下:
复制代码 代码如下:
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"ValidateRequest="false"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<tableid="TABLE1"runat="server"border="0"cellpadding="0"cellspacing="0">
<tr>
<td>
收信:<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox><br/>
主题:<asp:TextBoxID="TextBox2"runat="server"></asp:TextBox><br/>
内容:<asp:TextBoxID="TextBox3"runat="server"Height="154px"TextMode="MultiLine"
Width="336px"></asp:TextBox><br/>
<asp:ButtonID="Button1"runat="server"Text="发送"OnClick="Button1_Click"/></td>
</tr>
</table>
</div>
<tableid="Table2"runat="server"border="0"cellpadding="0"cellspacing="0"visible="false">
<tr>
<tdalign="center">
<asp:LabelID="Label1"runat="server"ForeColor="Red"Text="恭喜您,发表成功!"></asp:Label><br/>
<asp:ButtonID="Button2"runat="server"Text="返回"OnClick="Button2_Click"/></td>
</tr>
</table>
</form>
</body>
</html>
2、Default.aspx.cs代码如下:
复制代码 代码如下:
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
//倒入命名空间
usingSystem.Net;
usingSystem.Net.Mail;
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
////设置发件人信箱,及显示名字
MailAddressfrom=newMailAddress("zgdx0503@cpp114.com","延边职大信息中心");
//设置收件人信箱,及显示名字
MailAddressto=newMailAddress(TextBox1.Text,"0503班");
//创建一个MailMessage对象
MailMessageoMail=newMailMessage(from,to);
oMail.Subject=TextBox2.Text;//邮件标题
oMail.Body=TextBox3.Text;//邮件内容
oMail.IsBodyHtml=true;//指定邮件格式,支持HTML格式
oMail.BodyEncoding=System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
oMail.Priority=MailPriority.High;//设置邮件的优先级为高
//发送邮件服务器
SmtpClientclient=newSmtpClient();
client.Host="mail.cpp114.com";//指定邮件服务器
client.Credentials=newNetworkCredential("zgdx0503@cpp114.com","123456");//指定服务器邮件,及密码
//发送
try
{
client.Send(oMail);//发送邮件
Label1.Text="恭喜你!邮件发送成功。";
}
catch
{
Label1.Text="邮件发送失败,检查网络及信箱是否可用。";
}
oMail.Dispose();//释放资源
TABLE1.Visible=false;
Table2.Visible=true;
}
protectedvoidButton2_Click(objectsender,EventArgse)
{
//返回,继续发送
Response.Redirect(Request.Url.ToString());
TABLE1.Visible=true;
Table2.Visible=false;
}
}
3、运行并输入测试信箱zgdx0503@tom.com,(密码:123456)。如下所示:
4、打开信箱查看