主要是为了自己使用,大家可以自己去扩充。
全部代码:
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 += "<br>";
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;
}
} //结束邮件发送方法
} // 结束类
}