双击按钮,在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"
>

<
P
>

<
FONT
face
="宋体"
>
收件人:
<
asp:TextBox
id
="tbTo"
runat
="server"
Width
="232px"
></
asp:TextBox
></
FONT
></
P
>

<
P
><
FONT
face
="宋体"
>
主题:
<
asp:TextBox
id
="tbSubject"
runat
="server"
Width
="248px"
></
asp:TextBox
></
FONT
></
P
>

<
P
><
FONT
face
="宋体"
>
内容:
<
asp:TextBox
id
="tbBody"
runat
="server"
TextMode
="MultiLine"
Width
="256px"
Height
="128px"
></
asp:TextBox
></
FONT
></
P
>

<
P
><
FONT
face
="宋体"
>

<
asp:Button
id
="Button1"
runat
="server"
Text
="发送"
></
asp:Button
></
P
>

</
FONT
>

</
form
>

</
body
>

</
HTML
>

email.aspx.cs:
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
System.Web.Mail;

namespace
test

...
{

/**//// <summary>
/// email 的摘要说明。
/// </summary>
public class email : System.Web.UI.Page

...{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox tbSubject;
protected System.Web.UI.WebControls.TextBox tbBody;
protected System.Web.UI.WebControls.TextBox tbTo;

private void Page_Load(object sender, System.EventArgs e)

...{
if (!this.IsPostBack)

...{


}
}


Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)

...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}


/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

...{ 
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)

...{
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("OK");
}
}
}

这篇博客介绍了如何使用ASP.NET发送电子邮件。通过在email.aspx.cs文件中添加`System.Web.Mail`引用,并在Page_Load事件中设置初始化代码,在Button1_Click事件中配置邮件内容和SMTP服务器信息,即可实现邮件发送。用户只需将SMTP服务器地址替换为自己的实际服务器地址,即可完成邮件发送功能。
8683

被折叠的 条评论
为什么被折叠?



