在项目里新建一个email.aspx页面。在email里拖入三个文本框,分别命名为:tbTo、tbSubject、tbBody,它们分别是用做:收件人、Email的主题、Email的内容。然后再加入一个按钮。
双击按钮,在email.aspx.cs页面里,添加“using System.Web.Mail;”的引用。
再在Page_Load事件里加入以下代码:
if (!this.IsPostBack)
{}
然后在Button1_Click事件里加入以下代码:
MailMessage myMail = new MailMessage();
myMail.Subject = this.tbSubject.Text.Trim();
myMail.From = smallfools@hotmail.com;
myMail.To = this.tbTo.Text.Trim();
myMail.Body = this.tbBody.Text;
SmtpMail.SmtpServer = "smtp服务器的地址";
SmtpMail.Send(myMail);
Response.Write("发送成功");
只要把SmtpMail.SmtpServer换成您的smtp服务器名或IP就可以了。编译看看运行结果吧。
完整的代码如下:
email.aspx:
<%
...
@ Page language="c#" Codebehind="email.aspx.cs" AutoEventWireup="false" Inherits="test.email"
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > email </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body >
< form id ="Form1" method ="post" runat ="server"
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > email </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body >
< form id ="Form1" method ="post" runat ="server"