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);
}
}
}
查看全文
相关阅读:
CF809D Hitchhiking in the Baltic States
CF1188D Make Equal
CF1137 Train Car Selection
LOJ3215「PA 2019」Muzyka pop
洛谷4455 [CQOI2018]社交网络 (有向图矩阵树定理)(学习笔记)
洛谷3571 POI2014 SUP-Supercomputer (斜率优化)
洛谷2805 [NOI2009]植物大战僵尸 (拓扑排序+最小割)
洛谷2120 [ZJOI2007]仓库建设(斜率优化dp)
洛谷2494 [SDOI2011]保密 (分数规划+最小割)
洛谷3648 [APIO2014]序列分割(斜率优化+dp)
原文地址:https://www.cnblogs.com/xiaodi/p/121621.html
最新文章
【转】springmvc源码分析链接
【转】netty-mina对比二
【转】netty-mina对比一
【转】线程状态
【转】ArrayList源码分析
【转】ConcurrenthashMap
【转】netty线程模型
【转】Netty源码的阅读学习
【转】Mina工作原理分析
【转】java 的HashMap的源码分析
热门文章
[Poj #1853] Cat
[Poj #1717] Dominoes
[Poj #1794] Georgia and Bob
[NOIP2018TGd1t3] 赛道修建
[乱搞模板] 闲来无事写的分数类
ARC092D Two Sequences
CF403E Two Rooted Trees
CF908G New Year and Original Order
CF1221F Choose a Square
CF796F Sequence Recovery
Copyright © 2011-2022 走看看