直播中
namespace Blood.Com.ClassLib
{
/// <summary>
/// TcpClient派生類,用來進行SMTP服務(wù)器的連接工作
/// </summary>
public class SMTPClient : TcpClient
{
public bool isConnected()
{
return Active;
}
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 ;
}
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 s,string SMTPCode)
{
return(s.IndexOf(SMTPCode,0,10)==-1)?false:true;
}
} //結(jié)束類
/// <summary>
/// 發(fā)送郵件類
/// </summary>
public class SMTPMail
{
/// <summary>
/// 錯誤反饋信息
/// </summary>
private string strErrMessage = null;
/// <summary>
/// SMTP服務(wù)器反饋的信息
/// </summary>
private string strResponse;
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public SMTPMail()
{
strErrMessage = "";
strResponse = "";
}
/// <summary>
/// 取得錯誤反饋信息
/// </summary>
public string ErrorMessage
{
get
{
return strErrMessage ;
}
}
/// <summary>
/// 取得SMTP服務(wù)器反饋的信息
/// </summary>
public string ServerResponse
{
get
{
return strResponse;
}
}
/// <summary>
/// 郵件發(fā)送優(yōu)先級
/// </summary>
public enum Prioritys
{
/// <summary>
/// 最高級別
/// </summary>
HIGH = 1,
/// <summary>
/// 默認級別
/// </summary>
NORMAL = 3,
/// <summary>
/// 最低級別
/// </summary>
LOW = 5
}
public void SendMail(string SmtpHost,int Port,string From,string DisplayFromName,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服務(wù)器連接失敗...";
return;
}
//讀取反饋信息
strResponseNumber = smtpcMail.GetServerResponse();
if (smtpcMail.DoesStringContainSMTPCode(strResponseNumber,"220"))
{
this.strResponse += strResponseNumber;
}
else
{
this.strErrMessage = "連接失敗" + strResponseNumber;
return ;
}
string[] strSendBuffer = new string[6];
string[] strResponseCode = {"220","250","251","354","221"}; // success codes from SMTP server
string strData = "";
strData = string.Concat("HELO ",SmtpHost);
strData = string.Concat(strData,"\r\n");
strSendBuffer[0] = strData ;
strData = "";
strData = string.Concat("MAIL FROM: ","<" + From + ">");
strData = string.Concat(strData,"\r\n");
strSendBuffer[1] = strData;
strData = "";
strData = string.Concat("RCPT TO: ","<" + To + ">");
strData = string.Concat(strData,"\r\n");
strSendBuffer[2] = strData;
strData = "" ;
strData = string.Concat("DATA","\r\n");
strSendBuffer[3] = strData ;
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[4] = strData;
strData = "" ;
strData = string.Concat(strData,"QUIT\r\n");
strSendBuffer[5] = strData;
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)
{
this.strErrMessage += strResponseNumber;
this.strErrMessage += strSendBuffer[i];
return;
}
}
}
i++ ;
} // 結(jié)束循環(huán)
}
catch(SocketException err)
{
this.strErrMessage += err.Message + " " + err.StackTrace;
}
catch(Exception e)
{
this.strErrMessage += e.Message + " " + e.StackTrace;
}
} //結(jié)束郵件發(fā)送方法
} // 結(jié)束類
} // 結(jié)束Namespace