zoukankan
html css js c++ java
实现Email传送
using
System.Web.Mail;
using
System.IO;
private
void
btnSend_Click(
object
sender, System.EventArgs e)
{
//
分别取得邮件的收信人的地址、发信人的地址、抄送、主题、内容等信息
string
strTo
=
tbTo.Text;
string
strFrom
=
tbFrom.Text;
string
strPwd
=
tbPwd.Text;
string
strCopyTo
=
tbCopyTo.Text;
string
strSubject
=
tbSubject.Text;
string
strBody
=
tbBody.Text;
try
{
MailMessage ms
=
new
MailMessage();
ms.To
=
strTo;
//
收信人的地址
ms.From
=
strFrom;
//
发信人的地址
ms.Cc
=
strCopyTo;
//
抄送
ms.Subject
=
strSubject;
//
主题
ms.BodyFormat
=
MailFormat.Html;
//
正文格式html/text
ms.Body
=
strBody;
//
正文
string
strPathOfAttachFile
=
""
;
//
初始化附件
//
如果有附件则上传
HttpPostedFile hpPFile
=
AttachFile.PostedFile;
//
获得上传文件的访问
if
(hpPFile.FileName
!=
""
)
{
//
有附件,则上传到Temp目录中
//
判断是否存在Temp目录,若无,则创建
string
FolderName
=
Server.MapPath(
"
.
"
)
+
"
\\Temp
"
;
if
(Directory.Exists(FolderName)
==
false
)
Directory.CreateDirectory(FolderName);
//
取得文件名(不含路径)
char
[] separator
=
{
'
\\
'
}
;
//
separator的值为"\"
string
[] AFileName
=
hpPFile.FileName.Split(separator);
string
strFileName
=
AFileName[AFileName.Length
-
1
];
strPathOfAttachFile
=
Server.MapPath(
"
.
"
)
+
"
\\Temp\\
"
+
strFileName;
hpPFile.SaveAs(strPathOfAttachFile);
//
添加附件
ms.Attachments.Add(
new
MailAttachment(strPathOfAttachFile));
}
//
从发信人的地址计算出邮件服务器
string
[] strTemp
=
strFrom.Split(
'
@
'
);
string
strPartOfSmtpServer
=
strTemp[strTemp.Length
-
1
];
string
strSmtpServer
=
"
smtp.
"
+
strPartOfSmtpServer;
ms.Fields.Add(
"
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate
"
,
"
1
"
);
//
value=0 No Check value=1 Basic Check Value=2 Exchage Check
ms.Fields.Add(
"
http://schemas.microsoft.com/cdo/configuration/sendusername
"
, strFrom);
//
发信人的邮箱地址
ms.Fields.Add(
"
http://schemas.microsoft.com/cdo/configuration/sendpassword
"
, strPwd);
//
验证信息
SmtpMail.SmtpServer
=
strSmtpServer;
//
邮件服务器ip或域名
SmtpMail.Send(ms);
//
发送
//
清除控件中的内容
tbTo.Text
=
""
;
tbCopyTo.Text
=
""
;
tbSubject.Text
=
""
;
tbBody.Text
=
""
;
//
删除Temp目录中的附件
if
(File.Exists(strPathOfAttachFile)
==
true
)
File.Delete(strPathOfAttachFile);
//
确认邮件发送成功
string
strScript
=
"
<script>alert('邮件发送成功!')</script>
"
;
if
(
!
Page.IsStartupScriptRegistered(
"
Alert
"
))
{
Page.RegisterStartupScript(
"
Alert
"
, strScript);
}
}
catch
{
string
strScript
=
"
<script>alert('邮件发送失败!')</script>
"
;
if
(
!
Page.IsStartupScriptRegistered(
"
Alert
"
))
{
Page.RegisterStartupScript(
"
Alert
"
, strScript);
}
}
}
查看全文
相关阅读:
128-django的注册和登录【2】:注册和登录的初步实现
127-django的注册和登录【1】:尝试使用预设的User类
126-对已添加文章的编辑,编辑完成后呈现此文章
125-django的标签,条件过滤
124-django的翻页/分页功能,使用Paginator
123-在前端添加评论,显式地指定绑定关系
122-django不依赖后台,在前端添加文章(提交后跳转到其他页面)
二叉树及遍历方式详解
由一个算法引发的hash讲解
Java基础知识总结
原文地址:https://www.cnblogs.com/xiaodi/p/121621.html
最新文章
Spark 自定义累加变量(Accmulator)AccumulatorParam
Spark RDD 多文件输入
踩坑事件:不能对基于文本的临时表使用sql insert语句
踩坑事件:windows操作系统下的eclipse中编写SparkSQL不能从本地读取或者保存parquet文件
ZooKeeper个人笔记Session管理
ZooKeeper学习总结 第二篇:ZooKeeper深入探讨(转载)
ZooKeeper个人笔记之节点的监听
概率论公理
排序--选择排序
排序--冒泡排序
热门文章
排序算法--介绍
递归--解决迷宫问题
关于栈实现综合计算器的代码实现和讲解
双链表代码实现和讲解
数组方式实现队列操作
win下的mongodb安装和基础操作
关于ArrayList源码
131-使用login_required(),以及将当前的操作内容绑定到登陆者
130-注册和登录的相关反馈,比较拙劣的做法,请大牛给予建议
129-django工程中,在views函数里,让页面跳转到其他app的模板页面(跨app跳转)
Copyright © 2011-2022 走看看