smtp主要是为了自己使用,大家可以自己去扩充。
全部代码:
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;
}
}
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 == true)
{
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)
{
this.strErrMessage += strResponseNumber;
this.strErrMessage += strSendBuffer[i];
return;
}
}
}
i++ ;
} // 结束循环
}
catch(SocketException err)
{
this.strErrMessage += err.Message + " " + err.StackTrace;
}
catch(Exception e)
{
this.strErrMessage += e.Message + " " + e.StackTrace;
}
} //结束邮件发送方法
} // 结束类
}
支持SMTP认证功能的邮件发送类。
80酷酷网 80kuku.com