邮件发送类,带连接测试、错误处理类

2008-04-09 04:14:23来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:远离黑客十条永恒的安全法则

下一篇:使用W3 JMail实现邮件发送的解决方案