使用系統;
使用System.Text;
使用System.io;
使用system.net;
使用system.net.sockets;
namespace blood.com.classlib
{
/// <summary>
/// tcpclient派生類,用來進行smtp服務器的連接工作
/// </summary>
公共類SMTPCLIENT:TCPCLIENT
{
公共佈局()
{
返回活動;
}
public void sendcommandtoserver(字符串命令)
{
NetworkStream ns = this.getStream();
byte [] writebuffer;
writebuffer = new Byte [1024];
writeBuffer = encoding.default.getBytes(命令);
ns.Write(WriteBuffer,0,writebuffer.length);
返回;
}
公共字符串getserverresponse()
{
int流;
字符串returnValue =;
byte []讀取器;
NetworkStream ns = this.getStream();
readbuffer =新字節[1024];
streamSize = ns.Read(readbuffer,0,deadbuffer.length);
if(streamsize == 0)
{
返回returnValue;
}
別的
{
returnValue = encoding.default.getString(readbuffer);
returnReturnvalue;
}
}
公共bool doesstringContainsmtpcode(字符串S,字符串SMTPCODE)
{
返回(s.indexof(smtpcode,0,10)== -1)? false:true;
}
} //結束類
/// <summary>
///發送郵件類
/// </summary>
公共類SMTPMail
{
/// <summary>
///錯誤反饋信息
/// </summary>
私有字符串strerrmessage = null;
/// <summary>
/// SMTP服務器反饋的信息
/// </summary>
私人字符串strresponse;
/// <summary>
///構造函數
/// </summary>
public smtpmail()
{
Strerrmessage =;
strresponse =;
}
/// <summary>
///取得錯誤反饋信息
/// </summary>
公共字符串錯誤
{
得到
{
返回Strerrmessage;
}
}
/// <summary>
///取得smtp服務器反饋的信息
/// </summary>
公共字符串ServerResponse
{
得到
{
返回strresponse;
}
}
/// <summary>
///郵件發送優先級
/// </summary>
公共枚舉優先級
{
/// <summary>
///最高級別
/// </summary>
高= 1,
/// <summary>
///默認級別
/// </summary>
正常= 3,
/// <summary>
///最低級別
/// </summary>
低= 5
}
public void sendmail(字符串smtphost,int端口,字符串,字符串displing fromname,字符串到,字符串displistoname,優先級優先級,bool html,字符串基礎,字符串base,字符串主題,字符串消息)
{
嘗試
{
字符串strresponsenumber;
smtpclient smtpcmail = new smtpclient();
smtpcmail.connect(smtphost,端口);
bool bolconnect = smtpcmail.isconnected();
//判斷是否進行了連接
如果(!bolconnect)
{
strerrmessage = smtp服務器連接失敗...;
返回;
}
//讀取反饋信息
strresponsenumber = smtpcmail.getServerResponse();
如果(smtpcmail.doesstringcontainsmtpcode(strresponsenumber,220))
{
this.stresponse += strresponsenumber;
}
別的
{
this.strermessage =連接失敗 + strresponsenumber;
返回 ;
}
字符串[] strsendBuffer = new String [6];
string [] strresponsecode = {220,250,251,354,221}; //從SMTP服務器獲得成功代碼
字符串strdata =;
strdata = string.concat(helo,smtphost);
strdata = string.concat(strdata,/r/n);
strsendBuffer [0] = strdata;
strdata =;
strdata = string.concat(郵件來自:,< +來自 +>);
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(來自:,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,objuct:);
strdata = string.concat(strdata,主題);
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-prority: + Priority);
strdata = string.concat(strdata,/r/n);
strdata = string.concat(strdata,x-msmail-prority: + Priority);
strdata = string.concat(strdata,/r/n);
如果(html == true)
{
strdata = string.concat(strdata,content-type:text/html;);
}
別的
{
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);
如果(html == true)
{
strdata = string.concat(strdata,content-transfer-insoding:text/html;);
}
別的
{
strdata = string.concat(strdata,content-transfer-ongoding:text/plain;);
}
strdata = string.concat(strdata,/r/n);
strdata = string.concat(strdata,content-base: / + base + /);
strdata = string.concat(strdata,/r/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,退出/r/n);
strsendBuffer [5] = strdata;
int i = 0;
而(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.stresponse += strresponsenumber;
this.strresponse += <br>;
休息;
}
別的
{
if(j == strresponsecode.length-1)
{
this.strerrmessage += strresponsenumber;
this.strermessage += strsendBuffer [i];
返回;
}
}
}
i ++;
} //結束循環
}
捕獲(socketException err)
{
this.strerrmessage + = err.message + + err.stacktrace;
}
捕獲(例外E)
{
this.strerrmessage + = e.message + + e.stacktrace;
}
} //結束郵件發送方法
} //結束類
} //結束名稱空間