载入中
自定义HTML载入中... loading
用asp.net发送Email(一):简单地发送 [原创 2006-04-24 16:37:20]  删除... 
字体变小 字体变大

    在项目里新建一个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">

              <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)

              {

                  

 

              }

         }

 

         #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
所属版块: 科技
票数:
什么是“我顶”?
点击数:    评论数:
本文章引用通告地址(TrackBack Ping URL)为:
本文章尚未被引用。
发表评论
大 名:
(不填写则显示为匿名者)
网 址:
(您的网址,可以不填)
标 题:
内 容:
请根据下图中的字符输入验证码:
(您的评论将有可能审核后才能发表)
和讯个人门户 v1.0 | 和讯部落 | 客服中心