zoukankan
html css js c++ java
ASP.Net邮件发送类
using
System;
using
System.Text;
using
System.IO;
using
System.Net;
using
System.Net.Sockets;
namespace
OSLeagueForumXP.Components
{
/**/
///
<summary>
///
TcpClient派生类,用来进行SMTP服务器的连接工作
///
</summary>
public
class
SMTPClient : TcpClient
{
/**/
///
<summary>
///
进行SMTP服务器的连接
///
</summary>
public
SMTPClient()
{
}
/**/
///
<summary>
///
是否以连接
///
</summary>
///
<returns>
连接为True,不连接为False
</returns>
public
bool
isConnected()
{
return
Active;
}
/**/
///
<summary>
///
向服务器发送命令
///
</summary>
///
<param name="Command">
命令
</param>
public
void
SendCommandToServer(
string
Command)
{
NetworkStream ns
=
this
.GetStream() ;
byte
[] WriteBuffer ;
WriteBuffer
=
new
byte
[
1024
] ;
WriteBuffer
=
Encoding.Default.GetBytes(Command);
ns.Write(WriteBuffer,
0
,WriteBuffer.Length);
return
;
}
/**/
///
<summary>
///
取得服务器反馈信息
///
</summary>
///
<returns>
字符串
</returns>
public
string
GetServerResponse()
{
int
StreamSize ;
string
ReturnValue
=
""
;
byte
[] ReadBuffer ;
NetworkStream ns
=
this
.GetStream() ;
ReadBuffer
=
new
byte
[
1024
] ;
StreamSize
=
ns.Read(ReadBuffer,
0
,ReadBuffer.Length);
if
(StreamSize
==
0
)
{
return
ReturnValue ;
}
else
{
ReturnValue
=
Encoding.Default.GetString(ReadBuffer);
return
ReturnValue;
}
}
/**/
///
<summary>
///
判断返回的信息中是否有指定的SMTP代码出现
///
</summary>
///
<param name="Message">
信息
</param>
///
<param name="SMTPCode">
SMTP代码
</param>
///
<returns>
存在返回False,不存在返回True
</returns>
public
bool
DoesStringContainSMTPCode(
string
Message,
string
SMTPCode)
{
return
(Message.IndexOf(SMTPCode,
0
,
10
)
==-
1
)
?
false
:
true
;
}
}
//
结束类
/**/
///
<summary>
///
发送邮件类
///
</summary>
public
class
SMTPMail
{
/**/
///
<summary>
///
错误反馈信息
///
</summary>
private
string
strErrMessage
=
null
;
/**/
///
<summary>
///
SMTP服务器反馈的信息
///
</summary>
private
string
strResponse;
/**/
///
<summary>
///
构造函数
///
</summary>
public
SMTPMail()
{
strErrMessage
=
""
;
strResponse
=
""
;
}
/**/
///
<summary>
///
取得错误反馈信息
///
</summary>
public
string
ErrorMessage
{
get
{
return
strErrMessage ;
}
}
/**/
///
<summary>
///
取得SMTP服务器反馈的信息
///
</summary>
public
string
ServerResponse
{
get
{
return
strResponse;
}
}
/**/
///
<summary>
///
邮件发送优先级
///
</summary>
public
enum
Prioritys
{
/**/
///
<summary>
///
最高级别
///
</summary>
HIGH
=
1
,
/**/
///
<summary>
///
默认级别
///
</summary>
NORMAL
=
3
,
/**/
///
<summary>
///
最低级别
///
</summary>
LOW
=
5
}
/**/
///
<summary>
///
进行BASE64编码
///
</summary>
///
<param name="Data">
数据
</param>
///
<returns>
字符串
</returns>
private
string
Encode(
string
Data)
{
byte
[] bteData;
bteData
=
Encoding.Default.GetBytes(Data);
return
Convert.ToBase64String(bteData);
}
/**/
///
<summary>
///
进行BASE64解码
///
</summary>
///
<param name="Data">
数据
</param>
///
<returns>
字符串
</returns>
private
string
Decode(
string
Data)
{
byte
[] bteData;
bteData
=
Convert.FromBase64String(Data);
return
Encoding.Default.GetString(bteData);
}
/**/
///
<summary>
///
发送邮件
///
</summary>
///
<param name="SmtpHost">
SMTP服务器
</param>
///
<param name="Port">
SMTP服务器端口
</param>
///
<param name="From">
邮件发送者
</param>
///
<param name="DisplayFromName">
显示的发送者名称
</param>
///
<param name="Authentication">
是否进行认证
</param>
///
<param name="UserName">
认证用户名
</param>
///
<param name="Password">
认证密码
</param>
///
<param name="To">
邮件接收者
</param>
///
<param name="DisplayToName">
显示的接收者名称
</param>
///
<param name="Priority">
优先级
</param>
///
<param name="Html">
是否为HTML
</param>
///
<param name="Base">
URL
</param>
///
<param name="Subject">
邮件主题
</param>
///
<param name="Message">
邮件内容
</param>
public
void
SendMail(
string
SmtpHost,
int
Port,
string
From,
string
DisplayFromName,
bool
Authentication,
string
UserName,
string
Password,
string
To,
string
DisplayToName,Prioritys Priority,
bool
Html,
string
Base,
string
Subject,
string
Message)
{
try
{
string
strResponseNumber;
SMTPClient smtpcMail
=
new
SMTPClient();
smtpcMail.Connect(SmtpHost,Port);
bool
bolConnect
=
smtpcMail.isConnected();
//
判断是否进行了连接
if
(
!
bolConnect)
{
strErrMessage
=
"
Smtp服务器连接失败
"
;
return
;
}
//
读取反馈信息
strResponseNumber
=
smtpcMail.GetServerResponse();
if
(smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
220
"
))
{
this
.strResponse
+=
strResponseNumber;
}
else
{
this
.strErrMessage
=
"
连接失败
"
+
strResponseNumber;
return
;
}
int
intBuffer
=
0
;
int
intArray
=
0
;
if
(Authentication)
{
intBuffer
=
9
;
}
else
{
intBuffer
=
6
;
}
string
[] strSendBuffer
=
new
string
[intBuffer];
string
[] strResponseCode
=
{
"
220
"
,
"
250
"
,
"
251
"
,
"
354
"
,
"
221
"
,
"
334
"
,
"
235
"
}
;
//
success codes from SMTP server
string
strData
=
""
;
strData
=
string
.Concat(
"
HELO
"
,SmtpHost);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strSendBuffer[intArray]
=
strData;
intArray
=
intArray
+
1
;
if
(Authentication)
{
strData
=
""
;
strData
=
string
.Concat(
"
AUTH LOGIN
"
,
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strSendBuffer[intArray]
=
strData;
intArray
=
intArray
+
1
;
strData
=
""
;
strData
=
string
.Concat(Encode(UserName),
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strSendBuffer[intArray]
=
strData;
intArray
=
intArray
+
1
;
strData
=
""
;
strData
=
string
.Concat(Encode(Password),
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strSendBuffer[intArray]
=
strData;
intArray
=
intArray
+
1
;
}
strData
=
""
;
strData
=
string
.Concat(
"
MAIL FROM:
"
,
"
<
"
+
From
+
"
>
"
+
"
AUTH=
"
+
From);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strSendBuffer[intArray]
=
strData;
intArray
=
intArray
+
1
;
strData
=
""
;
strData
=
string
.Concat(
"
RCPT TO:
"
,
"
<
"
+
To
+
"
>
"
);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strSendBuffer[intArray]
=
strData;
intArray
=
intArray
+
1
;
strData
=
""
;
strData
=
string
.Concat(
"
DATA
"
,
"
\r\n
"
);
strSendBuffer[intArray]
=
strData ;
intArray
=
intArray
+
1
;
strData
=
""
;
strData
=
string
.Concat(
"
From:
"
,DisplayFromName
+
"
<
"
+
From
+
"
>
"
);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
To:
"
);
strData
=
string
.Concat(strData,DisplayToName
+
"
<
"
+
To
+
"
>
"
);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
Subject:
"
);
strData
=
string
.Concat(strData,Subject);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
MIME-Version: 1.0
"
);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
X-Priority:
"
+
Priority);
strData
=
string
.Concat(strData,
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
X-MSMail-Priority:
"
+
Priority);
strData
=
string
.Concat(strData,
"
\r\n
"
);
if
(Html)
{
strData
=
string
.Concat(strData,
"
Content-Type: text/html;
"
);
}
else
{
strData
=
string
.Concat(strData,
"
Content-Type: text/plain;
"
);
}
strData
=
string
.Concat(strData,
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
charset=\
"
iso
-
8859
-
1
\
""
);
strData
=
string
.Concat(strData,
"
\r\n
"
);
if
(Html
==
true
)
{
strData
=
string
.Concat(strData,
"
Content-Transfer-Encoding: text/html;
"
);
}
else
{
strData
=
string
.Concat(strData,
"
Content-Transfer-Encoding: text/plain;
"
);
}
strData
=
string
.Concat(strData,
"
\r\n
"
);
strData
=
string
.Concat(strData,
"
Content-Base: \
""
+ Base +
"
\
""
);
strData
=
string
.Concat(strData,
"
\r\n
"
+
"
\r\n
"
);
strData
=
string
.Concat(strData,Message);
strData
=
string
.Concat(strData,
"
\r\n.\r\n
"
);
strSendBuffer[intArray]
=
strData;
intArray
=
intArray
+
1
;
strData
=
""
;
strData
=
string
.Concat(strData,
"
QUIT\r\n
"
);
strSendBuffer[intArray]
=
strData;
intArray
=
intArray
+
1
;
int
i
=
0
;
while
(i
<
strSendBuffer.Length)
{
smtpcMail.SendCommandToServer(strSendBuffer[i]);
strResponseNumber
=
smtpcMail.GetServerResponse();
for
(
int
j
=
0
;j
<
strResponseCode.Length;j
++
)
{
if
(smtpcMail.DoesStringContainSMTPCode(strResponseNumber,strResponseCode[j]))
{
this
.strResponse
+=
strResponseNumber;
this
.strResponse
+=
"
<br>
"
;
break
;
}
else
{
if
(j
==
strResponseCode.Length
-
1
)
{
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
500
"
))
{
this
.strErrMessage
=
"
邮箱地址错误
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
501
"
))
{
this
.strErrMessage
=
"
参数格式错误
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
502
"
))
{
this
.strErrMessage
=
"
命令不可实现
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
503
"
))
{
this
.strErrMessage
=
"
服务器需要SMTP验证
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
504
"
))
{
this
.strErrMessage
=
"
命令参数不可实现
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
421
"
))
{
this
.strErrMessage
=
"
服务未就绪,关闭传输信道
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
450
"
))
{
this
.strErrMessage
=
"
要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
550
"
))
{
this
.strErrMessage
=
"
要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
451
"
))
{
this
.strErrMessage
=
"
放弃要求的操作;处理过程中出错
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
551
"
))
{
this
.strErrMessage
=
"
用户非本地,请尝试<forward-path>
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
452
"
))
{
this
.strErrMessage
=
"
系统存储不足,要求的操作未执行
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
552
"
))
{
this
.strErrMessage
=
"
过量的存储分配,要求的操作未执行
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
553
"
))
{
this
.strErrMessage
=
"
邮箱名不可用,要求的操作未执行(例如邮箱格式错误)
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
553
"
))
{
this
.strErrMessage
=
"
邮箱名不可用,要求的操作未执行(例如邮箱格式错误)
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
432
"
))
{
this
.strErrMessage
=
"
需要一个密码转换
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
534
"
))
{
this
.strErrMessage
=
"
认证机制过于简单
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
538
"
))
{
this
.strErrMessage
=
"
当前请求的认证机制需要加密
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
454
"
))
{
this
.strErrMessage
=
"
临时认证失败
"
;
}
else
if
(
!
smtpcMail.DoesStringContainSMTPCode(strResponseNumber,
"
530
"
))
{
this
.strErrMessage
=
"
需要认证
"
;
}
return
;
}
}
}
i
++
;
}
//
结束循环
}
catch
(SocketException err)
{
this
.strErrMessage
+=
err.Message
+
"
"
+
err.StackTrace;
}
catch
(Exception e)
{
this
.strErrMessage
+=
e.Message
+
"
"
+
e.StackTrace;
}
}
//
结束邮件发送方法
}
//
结束类
}
查看全文
相关阅读:
python按行读取并替换
python 爬取网页内容
file.write(str),file.writelines(sequence)
04Spring_bean 后处理器(后处理Bean),BeanPostProcessor ,bean创建时序,动态代理
03Spring_bean的创建和作用域以及生命周期
02Spring_Ioc和DI介绍
01Spring_基本jia包的导入andSpring的整体架构and怎么加入日志功能
错题724-java
05传智_jbpm与OA项目_部门模块中增加部门的jsp页面增加一个在线编辑器功能
04传智_jbpm与OA项目_部门模块改进_直接在BaseAction中实现ModelDriven<T>
原文地址:https://www.cnblogs.com/ziyan22/p/1244356.html
最新文章
Java中什么是匿名对象,空参构造方法输出创建了几个匿名对象,属性声明成static
什么是静态代码块?java中如何使用空参构造方法自动生成不同名字的对象,使用非静态的属性和静态属性有什么区别,原因是什么?如何理解static关键字
Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?
Java 吃金币游戏设计与制作,下载版后补,代码没问题
Java开发小游戏 用键盘控制精灵在游戏中上下左右跑动 窗体小游戏可打包下载,解压后双击start运行
shell 中cut
How can I check the last time stats was run on Oracle without using OEM
唐诗
二手房不满2年率
适合代码的字体
热门文章
shell input value from console
夏令时
社保
2018年上海各区重点小学排名
trcd_extract_EDCD_new
python网页爬虫 spiders_97A-04B
changetoutf-8
b4和tncl_extract_UNCL_new
b3_trcd_EDCD_new
b2_trsd_EDSD_new
Copyright © 2011-2022 走看看