Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > PHP > asp.net邮件发送
【标  题】:asp.net邮件发送
【关键字】:asp.net
【来  源】:http://blog.csdn.net/lqscoke/archive/2007/04/15/1565874.aspx

asp.net邮件发送

Your Ad Here
<%@ Page language="c#" Codebehind="SendEmail.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.SendEmail" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>SendEmail</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 MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<FONT face="宋体">
                
<DIV>
                    
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="0"
                        cellPadding
="0" width="776" align="center" border="0">
                        
<TR>
                            
<TD>
                                
<TABLE id="Table2" cellSpacing="1" cellPadding="4" width="600" align="center" bgColor="#cccccc"
                                    border
="0">
                                    
<TR>
                                        
<TD align="center" bgColor="#f0f0f0" colSpan="2">电子邮件发送程 序</TD>
                                    
</TR>
                                    
<TR>
                                        
<TD align="right" width="150" bgColor="#f0f0f0">发送人:</TD>
                                        
<TD align="left" bgColor="#ffffff">
                                            
<asp:TextBox id="fromMail" runat="server" Width="300"></asp:TextBox></TD>
                                    
</TR>
                                    
<TR>
                                        
<TD align="right" bgColor="#f0f0f0">收件人:</TD>
                                        
<TD align="left" bgColor="#ffffff">
                                            
<asp:TextBox id="toMail" runat="server" Width="300"></asp:TextBox></TD>
                                    
</TR>
                                    
<TR>
                                        
<TD align="right" bgColor="#f0f0f0">抄送人:</TD>
                                        
<TD align="left" bgColor="#ffffff">
                                            
<asp:TextBox id="ccMail" runat="server" Width="300"></asp:TextBox></TD>
                                    
</TR>
                                    
<TR>
                                        
<TD align="right" bgColor="#f0f0f0">暗送人:</TD>
                                        
<TD align="left" bgColor="#ffffff">
                                            
<asp:TextBox id="bccMail" runat="server" Width="300"></asp:TextBox></TD>
                                    
</TR>
                                    
<TR>
                                        
<TD align="right" bgColor="#f0f0f0">&nbsp;&nbsp;&nbsp;&nbsp;题 :</TD>
                                        
<TD align="left" bgColor="#ffffff">
                                            
<asp:TextBox id="subject" runat="server" Width="300"></asp:TextBox></TD>
                                    
</TR>
                                    
<TR>
                                        
<TD align="right" bgColor="#f0f0f0">&nbsp;&nbsp;&nbsp;&nbsp;件 :</TD>
                                        
<TD align="left" bgColor="#ffffff"><INPUT id="upfile" type="file" name="upfile" runat="server"></TD>
                                    
</TR>
                                    
<TR>
                                        
<TD align="right" bgColor="#f0f0f0">&nbsp;&nbsp;&nbsp;&nbsp;容 :</TD>
                                        
<TD align="left" bgColor="#ffffff">
                                            
<asp:TextBox id="body" runat="server" Width="300" TextMode="multiLine" Height="200"></asp:TextBox></TD>
                                    
</TR>
                                    
<TR>
                                        
<TD align="right" bgColor="#f0f0f0">&nbsp;&nbsp;&nbsp;&nbsp;式 :</TD>
                                        
<TD align="left" bgColor="#ffffff">
                                            
<asp:RadioButtonList id="format" runat="server"></asp:RadioButtonList></TD>
                                    
</TR>
                                    
<TR>
                                        
<TD align="center" bgColor="#f0f0f0" colSpan="2">
                                            
<asp:Button id="send" runat="server" Text="发送"></asp:Button>&nbsp;&nbsp;
                                            
<asp:Button id="reset" runat="server" Text="重置"></asp:Button></TD>
                                    
</TR>
                                
</TABLE>
                            
</TD>
                        
</TR>
                    
</TABLE>
                
</DIV>
            
</FONT>
        
</form>
    
</body>
</HTML>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Util;
using System.Web.Mail;


namespace WebApplication1
{
    
/// <summary>
    
/// SendEmail 的摘要说明。
    
/// </summary>
    public class SendEmail : System.Web.UI.Page
    {
        
protected System.Web.UI.WebControls.Button reset;
        
protected System.Web.UI.WebControls.Button send;
        
protected System.Web.UI.WebControls.RadioButtonList format;
        
protected System.Web.UI.WebControls.TextBox body;
        
protected System.Web.UI.WebControls.TextBox subject;
        
protected System.Web.UI.WebControls.TextBox bccMail;
        
protected System.Web.UI.WebControls.TextBox ccMail;
        
protected System.Web.UI.WebControls.TextBox toMail;
        
protected System.Web.UI.WebControls.TextBox fromMail;
        
protected System.Web.UI.HtmlControls.HtmlInputFile upfile;
    
        
private void Page_Load(object sender, System.EventArgs e)
        {
            
if(!IsPostBack)
            {
                format.Items.Add(
new ListItem("文本","0"));//类型
                format.Items.Add(new ListItem("HTML""1"));
                format.Items[
0].Selected = true;

                fromMail.Text 
= "lqscoke08@126.com"//发送方邮件
                fromMail.Enabled = false;
            }
        }

        
#region Web 窗体设计器生成的代码
        
override protected void OnInit(EventArgs e)
        {
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }
        
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {    
            
this.send.Click += new System.EventHandler(this.send_Click);
            
this.Load += new System.EventHandler(this.Page_Load);

        }
        
#endregion

        
private void send_Click(object sender, System.EventArgs e)
        {
            
bool flag=Sendmail(fromMail.Text,toMail.Text,ccMail.Text,bccMail.Text,subject.Text,body.Text,format.SelectedValue);
            
if (flag == true)
            {
                Response.Write(
"<script>alert('发送成功!');</script>");
            }
            
else 
            {
                Response.Write(
"<script>alert('发送失败!');</script>");
            }
        }

        
private bool Sendmail(string fromMail,string toMail,string ccMail,string bccMail,string subject,string body,string sendMode)
        {
            
try
            {
                MailMessage myMail 
= new MailMessage();
                myMail.From 
= fromMail;        //发件人邮箱
                myMail.To = toMail;            //收件人Email
                myMail.Cc = ccMail;            //邮件列表
                myMail.Bcc = bccMail;        
                myMail.Subject 
= subject;    //主题
                myMail.Body = body;            //正文
                myMail.BodyFormat = sendMode == "0" ? MailFormat.Text : MailFormat.Html; //邮件类型

                
//附件
                string ServerFileName = "";
                
if (this.upfile.PostedFile.ContentLength != 0)
                {
                    
string upFileName = this.upfile.PostedFile.FileName;
                    
string[] strTemp = upFileName.Split('.');
                    
string upFileExp = strTemp[strTemp.Length - 1].ToString();
                    ServerFileName 
= Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss"+ "." + upFileExp);
                    
this.upfile.PostedFile.SaveAs(ServerFileName);
                    myMail.Attachments.Add(
new MailAttachment(ServerFileName));
                }

                
// 通过SMTP服务器验证
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"1);
                myMail.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/sendusername""lqscoke08"); //发送方邮件帐户
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword""3626053705881"); //发送方邮件密码

                SmtpMail.SmtpServer 
= "smtp." + fromMail.Substring(fromMail.IndexOf("@"+ 1);
                SmtpMail.Send(myMail);

                
return true;
            }
            
catch 
            {
                
return false;
            }
        }


    }
}
CEdit & CRichEdit 使用技巧:【上一篇】
新人求助关于PHP的转换成ASP:【下一篇】
【相关文章】
  • Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
  • 《ASP.NET AJAX程序设计 第I卷 服务器端ASP.NET AJAX Extensions与ASP.NET AJAX Control Toolkit》连载&...
  • 使用Visual Studio2005入门asp.Net2.0系列视频教程
  • Visual Studio 2005入门 之 Asp.Net中的事件(页面事件)[视频]
  • Visual Studio 2005入门 之 Asp.Net中的事件(控件事件) [视频]
  • asp.net2.0 ajax
  • Asp.net 2.0自定义控件(点击HyperLink后执行事件)[网友问题: DataList里HyperLink控件激发事件,在哪定义?]
  • 利用Asp.net Ajax异步获取xml文档内容
  • 本周ASP.NET英文技术文章推荐[04/08 - 04/14]
  • ASP.NET 2.0 技巧:用MasterPage 代替 PageBase
  • 【随机文章】
  • 我现在不用NHibernate了,换一种简单的方法表示数据
  • 配置示例
  • 自己写的一个用AJAX实现的留言系统
  • Intel多核培训感想
  • 什么是系统集成
  • kvm的移植
  • 快速重装MSDN帮助库
  • 朱镕基等中央领导在大学的表现
  • 山东移动BOSS 系统备份解决方案
  • 双线策略路由pf.conf
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.