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 += "
";
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;
}
} //结束邮件发送方法
} // 结束类
}
邮件发送类
80酷酷网 80kuku.com