zoukankan
html css js c++ java
ASP.NET 实现邮件发送和接受的功能(Mail)
前台代码:html
<%
@ Page language
=
"
c#
"
Codebehind
=
"
WebMail.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
MymailSys.WebMail
"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
title
>
WebForm1
</
title
>
<
meta
content
="Microsoft Visual Studio .NET 7.1"
name
="GENERATOR"
>
<
meta
content
="C#"
name
="CODE_LANGUAGE"
>
<
meta
content
="JavaScript"
name
="vs_defaultClientScript"
>
<
meta
content
="http://schemas.microsoft.com/intellisense/ie5"
name
="vs_targetSchema"
>
</
HEAD
>
<
body
MS_POSITIONING
="GridLayout"
>
<
form
id
="Form1"
method
="post"
runat
="server"
enctype
="multipart/form-data"
>
<
FONT
face
="宋体"
>
<
asp:label
id
="Label2"
style
="Z-INDEX: 101; LEFT: 224px; POSITION: absolute; TOP: 16px"
runat
="server"
Width
="292px"
Font-Names
="隶书"
Font-Size
="XX-Large"
>
邮件发送系统
</
asp:label
>
<
TABLE
id
="Table1"
style
="Z-INDEX: 102; LEFT: 80px; WIDTH: 606px; POSITION: absolute; TOP: 96px; HEIGHT: 341px"
cellSpacing
="1"
cellPadding
="1"
width
="606"
align
="left"
border
="1"
>
<
TR
>
<
TD
style
="WIDTH: 178px; HEIGHT: 21px"
colSpan
="2"
><
asp:label
id
="lblShowMsg"
runat
="server"
Width
="99px"
ForeColor
="Red"
></
asp:label
></
TD
>
<
TD
style
="WIDTH: 178px; HEIGHT: 21px"
><
FONT
face
="宋体"
></
FONT
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 122px"
><
asp:label
id
="Label1"
runat
="server"
Width
="92px"
>
发件人:
</
asp:label
></
TD
>
<
TD
><
asp:textbox
id
="tbFrom"
runat
="server"
></
asp:textbox
></
TD
>
<
TD
><
asp:requiredfieldvalidator
id
="RequiredFieldValidator1"
runat
="server"
Width
="156px"
ControlToValidate
="tbFrom"
ErrorMessage
="发件人不能为空!"
Height
="26px"
></
asp:requiredfieldvalidator
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 122px"
><
FONT
face
="宋体"
>
收件人:
</
FONT
></
TD
>
<
TD
><
asp:textbox
id
="tbTo"
runat
="server"
></
asp:textbox
></
TD
>
<
TD
><
FONT
face
="宋体"
><
asp:requiredfieldvalidator
id
="RequiredFieldValidator2"
runat
="server"
Width
="152px"
ControlToValidate
="tbTo"
ErrorMessage
="收件人不能为空!"
Height
="26px"
></
asp:requiredfieldvalidator
></
FONT
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 122px"
><
FONT
face
="宋体"
>
邮件主题:
</
FONT
></
TD
>
<
TD
><
asp:textbox
id
="tbSubject"
runat
="server"
></
asp:textbox
></
TD
>
<
TD
><
asp:requiredfieldvalidator
id
="RequiredFieldValidator3"
runat
="server"
Width
="160px"
ControlToValidate
="tbSubject"
ErrorMessage
="邮件主题不能为空!"
Height
="26px"
></
asp:requiredfieldvalidator
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 122px"
>
邮件服务器:
</
TD
>
<
TD
><
asp:textbox
id
="tbServer"
runat
="server"
></
asp:textbox
></
TD
>
<
TD
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 122px"
>
邮件用户名称:
</
TD
>
<
TD
><
asp:textbox
id
="tbUserName"
runat
="server"
></
asp:textbox
></
TD
>
<
TD
><
asp:requiredfieldvalidator
id
="RequiredFieldValidator5"
runat
="server"
ControlToValidate
="tbUserName"
ErrorMessage
="用户名不能为空!"
></
asp:requiredfieldvalidator
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 122px"
>
密码:
</
TD
>
<
TD
><
asp:textbox
id
="tbPass"
runat
="server"
TextMode
="Password"
></
asp:textbox
></
TD
>
<
TD
><
asp:requiredfieldvalidator
id
="RequiredFieldValidator6"
runat
="server"
ControlToValidate
="tbPass"
ErrorMessage
="密码不能为空!"
></
asp:requiredfieldvalidator
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 122px"
><
FONT
face
="宋体"
>
优先级:
<
asp:dropdownlist
id
="ddlp"
runat
="server"
>
<
asp:ListItem
Value
="高"
>
高
</
asp:ListItem
>
<
asp:ListItem
Value
="低"
>
低
</
asp:ListItem
>
<
asp:ListItem
Value
="普通"
>
普通
</
asp:ListItem
>
</
asp:dropdownlist
></
FONT
></
TD
>
<
TD
><
FONT
face
="宋体"
>
邮件格式:
<
asp:dropdownlist
id
="ddlB"
runat
="server"
>
<
asp:ListItem
Value
="Text"
>
Text
</
asp:ListItem
>
<
asp:ListItem
Value
="Html"
>
Html
</
asp:ListItem
>
</
asp:dropdownlist
></
FONT
></
TD
>
<
TD
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 122px"
>
邮件附件:
</
TD
>
<
TD
colSpan
="2"
><
INPUT
id
="FileSelect"
style
="WIDTH: 282px; HEIGHT: 22px"
type
="file"
size
="27"
name
="File1"
runat
="server"
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 178px"
colSpan
="3"
><
FONT
face
="宋体"
>
邮件内容:
</
FONT
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 178px"
colSpan
="3"
><
FONT
face
="宋体"
><
asp:textbox
id
="tbBody"
runat
="server"
Width
="594px"
Height
="167px"
TextMode
="MultiLine"
></
asp:textbox
></
FONT
></
TD
>
</
TR
>
<
TR
>
<
TD
style
="WIDTH: 235px"
colSpan
="3"
><
asp:button
id
="btnSubmit"
runat
="server"
Text
="发送"
></
asp:button
><
FONT
face
="宋体"
></
FONT
></
TD
>
</
TR
>
</
TABLE
>
</
form
>
</
FONT
>
</
body
>
</
HTML
>
后台代码: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
MymailSys
{
/**/
///
<summary>
///
WebForm1 的摘要说明。
///
</summary>
public
class
WebMail : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.TextBox tbBody;
protected
System.Web.UI.WebControls.DropDownList ddlB;
protected
System.Web.UI.WebControls.DropDownList ddlp;
protected
System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
protected
System.Web.UI.WebControls.TextBox tbSubject;
protected
System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected
System.Web.UI.WebControls.TextBox tbTo;
protected
System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected
System.Web.UI.WebControls.TextBox tbFrom;
protected
System.Web.UI.WebControls.Label Label1;
protected
System.Web.UI.WebControls.Label lblShowMsg;
protected
System.Web.UI.WebControls.Label Label2;
protected
System.Web.UI.HtmlControls.HtmlForm Form1;
protected
System.Web.UI.WebControls.TextBox tbServer;
protected
System.Web.UI.WebControls.TextBox tbUserName;
protected
System.Web.UI.WebControls.TextBox tbPass;
protected
System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator5;
protected
System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator6;
protected
System.Web.UI.WebControls.Button btnSubmit;
protected
System.Web.UI.HtmlControls.HtmlInputFile FileSelect;
protected
System.Web.UI.HtmlControls.HtmlForm Form2;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.btnSubmit.Click
+=
new
System.EventHandler(
this
.btnSubmit_Click);
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
private
void
btnSubmit_Click(
object
sender, System.EventArgs e)
{
//
SmtpMail.Send("shaozhd@263.net","shaozhd@263.net","Test","Hello");
MailMessage m
=
new
MailMessage();
m.From
=
tbFrom.Text;
m.To
=
tbTo.Text;
m.Subject
=
tbSubject.Text;
m.Body
=
tbBody.Text;
//
优先级
switch
(ddlp.SelectedIndex)
{
case
0
:
m.Priority
=
MailPriority.High;
break
;
case
1
:
m.Priority
=
MailPriority.Low;
break
;
default
:
m.Priority
=
MailPriority.Normal;
break
;
}
//
格式
if
(ddlp.SelectedIndex
==
0
)
m.BodyFormat
=
MailFormat.Text;
else
m.BodyFormat
=
MailFormat.Html;
//
以下设置服务器
if
(tbServer.Text
!=
""
)
{
SmtpMail.SmtpServer
=
tbServer.Text;
//
以下代码适用于Framework1.1以上版本。
//
m.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate
",
//
"1");
//
basic authentication
//
m.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendusername
",
//
tbUserName.Text);
//
set your username here
//
m.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendpassword
",
//
tbPass.Text);
//
set your password here
}
//
以下处理附件
string
strFileName
=
FileSelect.PostedFile.FileName;
if
(strFileName
!=
""
)
m.Attachments.Add(
new
MailAttachment(strFileName));
SmtpMail.Send(m);
}
}
}
查看全文
相关阅读:
【Unity】A星寻路(UGUI)
MD5加密处理
Unity连接Sql数据库(PC端)
连接数据库后的一般操作
连接数据库后的简单查询
连接数据库的几种方式
关于Unity实现三维物体裁剪功能
关于Unity实现AR功能(五)摄像头转换与闪光灯开关控制
关于Unity实现AR功能(四)设置相机的对焦
关于Unity实现AR功能(二)进阶触发及手势
原文地址:https://www.cnblogs.com/ghd258/p/259692.html
最新文章
openoffice osl模块学习1
【Unity】利用C#反射打印类的字段信息
【Unity】protobuf故意踩坑记
当我读《软技能》时我在想什么
【Unity】Timeline探索记(4)第二个例子——动作特写/子弹时间
【Unity】Timeline探索记(3)关于Playable
【Unity】Timeline探索记(2)第一个例子——字幕轨
【Unity】Timeline探索记(1)Timeline怎么用——面板功能探索
【Unity】Timeline探索记(0)我要用Timeline做什么
当我读《程序员修炼之道(第2版)》时我在想什么
热门文章
记一道“芯片升级消耗”数学题
当我读《自己动手实现Lua-虚拟机、编译器和标准库》时我在想什么
当我读《游戏引擎架构》时我在想什么
【Unity】bundleID相关介绍
Windows用VSCode搭建Go环境
《自己动手实现Lua》一书Windows命令行代码
Windows快捷安装Lua
当我读《程序员修炼之道》时我在想什么
【Unity】为什么拼UI时会用到图集
当我读《游戏编程模式》时我在想什么
Copyright © 2011-2022 走看看