使用TCP命令实现,EMAIL发送系统

 联系我们     |      2020-03-14 07:48

private bool SendEmail(string smtpServer,int port,bool ESmtp,string username,string password,MailMessage mailMessage)
{
if (Connect(smtpServer,port)==false)//测试连接服务器是否成功
return false;

///
/// 发送邮件方法
///
/// smtp服务器信息,如"username:password@www.smtpserver.com:25",也可去掉部分次要信

SendBufferstr += CRLF + "." + CRLF;//内容结束

//SendBufferstr += "Content-Transfer-Encoding: base64"+enter;

if (StreamSize==0)
{
return Returnvalue ;
}
else
{
Returnvalue = Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize);
logs+=Returnvalue+this.CRLF;
return Returnvalue;
}
}

#region Properties

/// 或者:SmtpServerAddress

string[] SendBuffer;
string SendBufferstr;

using System;
using NUnit.Framework;

(":")-1);
maidomain=maidomain.Substring(tempint+1,maidomain.Length-tempint-1);
}

//发件人姓名
SendBufferstr="From:" + mailMessage.FromName + "" +CRLF;

///
/// 最多收件人数量
///
private int recipientmaxnum=5;

return true;
}

///
/// 邮件附件列表
///
private IList Attachments;

500 Syntax error, command unrecognized
[This may include errors such as command line too long]
501 Syntax error in parameters or arguments
502 Command not implemented
503 Bad sequence of commands
504 Command parameter not implemented
550 Requested action not taken: mailbox unavailable
[E.g., mailbox not found, no access]
551 User not local; please try
552 Requested mail action aborted: exceeded storage allocation
553 Requested action not taken: mailbox name not allowed
[E.g., mailbox syntax incorrect]
554 Transaction failed

///
/// 将字符串编码为Base64字符串
///
/// 要编码的字符串
private string Base64Encode(string str)
{
byte[] barray;
barray=Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}

public void AddRecipients(params string[] recipient)
{
if (recipient==null)
{
throw (new ArgumentException("收件人不能为空."));
}
else
{
for (int i=0;i {
AddRecipients(recipient[i]);
}
}
}

前段时间有网友反馈了一些问题,这次主要做了一些修正

///
/// SMTP错误代码哈希表
///
private Hashtable ErrCodeHT = new Hashtable();

tempint=maidomain.IndexOf(":");
if(tempint!=-1)
{
mailserver=maidomain.Substring(0,tempint);
mailserverport=System.Convert.ToInt32(maidomain.Substring

#region Class mailAttachments
public class MailAttachments
{
private const int MaxAttachmentNum=10;
private IList _Attachments;

///
/// 发送邮件方法,所有参数均通过属性设置。
///
public bool Send()
{
if(mailserver.Trim()=="")
{
throw(new ArgumentNullException("Recipient","必须指定SMTP服务器"));
}

211 System status, or system help reply
214 Help message
[Information on how to use the receiver or the meaning of a
particular non-standard command; this reply is useful only
to the human user]
220 Service ready
221 Service closing transmission channel
250 Requested mail action okay, completed
251 User not local; will forward to

case "normal":
priority="Normal";
break;

SendBufferstr="DATA" + CRLF;
if(!Dialog(SendBufferstr,""))
return false;

///
/// 邮件主题
///
private string _subject;

}
#endregion//end Class mailAttachments

//验证网络连接是否正确
if(RightCodeHT[RecvResponse().Substring(0,3)]==null)
{
errmsg="网络连接失败";
return false;
}

public class SmtpMail
{
private static string _SmtpServer;

/*
SendBuffer=new string[10];
for(int i=0;i {

///
/// 发送SMTP命令
///
private bool SendCommand(string str)
{
byte[]WriteBuffer;
if(str==null||str.Trim()==String.Empty)
{
return true;
}
logs+=str;
WriteBuffer = Encoding.Default.GetBytes(str);
try
{
networkStream.Write(WriteBuffer,0,WriteBuffer.Length);
}
catch
{
errmsg="网络连接错误";
return false;
}
return true;
}

欢迎大家提出修改建议,[注:]最好能把修改稿Mail给我一份,我们共同学习
我的Email:lion-a@sohu.com lion.net@163.com

public string Subject
{
get { return _Subject;}
set { _Subject=value;}
}

(filepath.Substring(filepath.LastIndexOf("\")+1))+"?=""+enter+enter;
SendBufferstr += GetStream(filepath)+enter+enter;
}
SendBufferstr += "--====="+ (Html?"001_Dragon520636771063_":"001_Dragon303406132050_")

#endregion
}

#region Private Helper Functions

namespace SkyDev.Web.Mail
{
public enum MailFormat{Text,HTML};
public enum MailPriority{Low=1,Normal=3,High=5};

///
/// 邮件服务器域名和验证信息
/// 形如:"user:pass@www.server.com:25",也可省略次要信息。如"user:pass@www.server.com"或"www.server.com"
///
public string MailDomain
{
set
{
string maidomain=value.Trim();
int tempint;

string priority=GetPriorityString(mailMessage.Priority);
bool Html=(mailMessage.BodyFormat==MailFormat.HTML);

///
/// 添加邮件附件
///
/// 附件绝对路径
public void AddAttachment(params string[] FilePath)
{
if(FilePath==null)
{
throw(new ArgumentNullException("FilePath"));
}
for(int i=0;i {
Attachments.Add(FilePath[i]);
}
}

///
/// 将字符串编码为Base64字符串
///
/// 要编码的字符串
private string Base64Encode(string str)
{
byte[] barray;
barray=Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}

(Html?"001_Dragon520636771063_":"001_Dragon303406132050_") +"====="+enter;
//SendBufferstr += "Content-Type: application/octet-stream"+enter;
SendBufferstr += "Content-Type: text/plain;"+enter;
SendBufferstr += " name="=?"+Charset.ToUpper()+"?B?"+Base64Encode

///
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
///
public string Charset
{
get { return _Charset;}
set { _Charset=value;}
}

Charset.ToLower() + """)) + enter;
SendBufferstr+="Content-Transfer-Encoding: base64" + enter + enter;
SendBufferstr+= Base64Encode("邮件内容为HTML格式,请选择HTML方式查看") + enter + enter;

//连接服务器
private bool Connect(string smtpServer,int port)
{
//创建Tcp连接
try
{
tcpClient=new TcpClient(smtpServer,port);
}
catch(Exception e)
{
errmsg=e.ToString();
return false;
}
networkStream=tcpClient.GetStream();

//进行SMTP验证
if(ESmtp)
{
SendBuffer=new String[4];
SendBuffer[0]="EHLO " + mailserver + enter;
SendBuffer[1]="AUTH LOGIN" + enter;
SendBuffer[2]=Base64Encode(username) + enter;
SendBuffer[3]=Base64Encode(password) + enter;
if(!Dialog(SendBuffer,"SMTP服务器验证失败,请核对用户名和密码。"))
return false;
}
else
{
SendBufferstr="HELO " + mailserver + enter;
if(!Dialog(SendBufferstr,""))
return false;
}

}

return true;
}

  • mailMessage.Subject):("Subject:" + "=?" + mailMessage.Charset.ToUpper()
  • "?B?" +
    Base64Encode(mailMessage.Subject) +"?="))) + CRLF;
    SendBufferstr+="X-Priority:" + priority + CRLF;
    SendBufferstr+="X-MSMail-Priority:" + priority + CRLF;
    SendBufferstr+="Importance:" + priority + CRLF;
    SendBufferstr+="X-Mailer: Lion.Web.Mail.SmtpMail Pubclass [cn]" + CRLF;
    SendBufferstr+="MIME-Version: 1.0" + CRLF;
    if(mailMessage.Attachments.Count!=0)
    {
    SendBufferstr+="Content-Type: multipart/mixed;" + CRLF;
    SendBufferstr += " boundary="====="+
    (Html?"001_Dragon520636771063_":"001_Dragon303406132050_")+"=====""+CRLF+CRLF;
    }

///
/// SMTP认证时使用的密码
///
public string MailServerPassWord
{
set
{
password=value;
}
}

SendBufferstr+=
((mailMessage.Subject==String.Empty || mailMessage.Subject==null)?"Subject:":((mailMessage.Charset=="")?("Subject:"

///
/// 收件人姓名
///
public string RecipientName
{
get
{
return this._recipientName;
}
set
{
this._recipientName = value;
}
}

public void Clear()//清除所有附件
{
_Attachments.Clear();
}

///
/// 添加一组收件人(不超过recipientmaxnum个),参数为字符串数组
///
/// 保存有收件人地址的字符串数组(不超过recipientmaxnum个)
public bool AddRecipient(params string[] Recipients)
{
if(Recipient==null)
{
Dispose();
throw(new ArgumentNullException("Recipients"));
}
for(int i=0;i {
string recipient = Recipients[i].Trim();
if(recipient==String.Empty)
{
Dispose();
throw(new ArgumentNullException("Recipients["+ i +"]"));
}
if(recipient.IndexOf("@")==-1)
{
Dispose();
throw(new ArgumentException("Recipients.IndexOf("@")==-1","Recipients"));
}
if(!AddRecipient(recipient))
{
return false;
}
}
return true;
}

if(mailMessage.Attachments.Count!=0)
{
for(int i=0;i {
string filepath = (string)mailMessage.Attachments[i];
SendBufferstr += "--====="+
(Html?"001_Dragon520636771063_":"001_Dragon303406132050_") +"====="+CRLF;
//SendBufferstr += "Content-Type: application/octet-stream"+CRLF;
SendBufferstr += "Content-Type: text/plain;"+CRLF;
SendBufferstr += " name="=?"+mailMessage.Charset.ToUpper()+"?B?"+
Base64Encode(filepath.Substring(filepath.LastIndexOf("\")+1))+"?=""+CRLF;
SendBufferstr += "Content-Transfer-Encoding: base64"+CRLF;
SendBufferstr += "Content-Disposition: attachment;"+CRLF;
SendBufferstr += " filename="=?"+mailMessage.Charset.ToUpper()+"?B?"+
Base64Encode(filepath.Substring(filepath.LastIndexOf("\")+1))+"?=""+CRLF+CRLF;
SendBufferstr += GetStream(filepath)+CRLF+CRLF;
}
SendBufferstr += "--====="+
(Html?"001_Dragon520636771063_":"001_Dragon303406132050_")+"=====--"+CRLF+CRLF;
}

///
/// 邮件发送优先级,可设置为"High","Normal","Low"或"1","3","5"
///
private string priority="Normal";

[Test]
public void TestMailMessage()
{
MailMessage message=new MailMessage();
Assert.AreEqual(0,message.Attachments.Count,"初始化MailAttachments");
Assert.AreEqual(MailFormat.Text,message.BodyFormat,"邮件格式");
Assert.AreEqual("GB2312",message.Charset,"缺省的字符集");
}

if (StreamSize==0)
{
return ReturnValue ;
}
else
{
ReturnValue = Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize);
logs+=ReturnValue+this.enter;
return ReturnValue;
}
}

///
/// TcpClient对象,用于连接服务器
///
private TcpClient tcpClient;

SendBufferstr+="Content-Type: text/html;" + enter;
SendBufferstr+=((Charset=="")?(" charset="iso-8859-1""):(" charset="" +

SendBufferstr += "--=====003_Dragon520636771063_====="+CRLF;

+"=====--"+enter+enter;
}

///
/// NetworkStream对象
///
private NetworkStream networkStream;

if(maidomain!="")
{
tempint=maidomain.IndexOf("@");
if(tempint!=-1)
{
string str=maidomain.Substring(0,tempint);
MailServerUserName=str.Substring(0,str.IndexOf(":"));
MailServerPassWord=str.Substring(str.IndexOf(":")+1,str.Length-str.IndexOf

public SmtpServerHelper()
{
SMTPCodeAdd();//初始化SMTPCode
}

}
}

public IList Recipients
{
get { return _Recipients;}
}
///
/// 增加一个收件人地址
///
/// 收件人的Email地址
public void AddRecipients(string recipient)
{
//先检查邮件地址是否符合规范
if (_Recipients.Count {
_Recipients.Add(recipient);//增加到收件人列表
}
}

///
/// 收件人姓名
///
private string _recipientName="";

if(Html)
{
if(mailMessage.Attachments.Count==0)
{
SendBufferstr += "Content-Type: multipart/alternative;"+CRLF;//内容格式和分隔符
SendBufferstr += " boundary="=====003_Dragon520636771063_=====""+CRLF+CRLF;
SendBufferstr += "This is a multi-part message in MIME format."+CRLF+CRLF;
}
else
{
SendBufferstr +="This is a multi-part message in MIME format."+CRLF+CRLF;
SendBufferstr += "--=====001_Dragon520636771063_====="+CRLF;
SendBufferstr += "Content-Type: multipart/alternative;"+CRLF;//内容格式和分隔符
SendBufferstr += " boundary="=====003_Dragon520636771063_=====""+CRLF+CRLF;
}
SendBufferstr += "--=====003_Dragon520636771063_====="+CRLF;
SendBufferstr += "Content-Type: text/plain;"+ CRLF;
SendBufferstr += ((mailMessage.Charset=="")?(" charset="iso-8859-1""):(" charset="" +

///
/// SMTP认证时使用的用户名
///
private string username="";

/// /// SmtpMail.SmtpServer="user:12345678@smtp.126.com"; /// //或者: /// SmtpMail.SmtpServer="smtp.126.com"; /// 或者: /// SmtpMail.SmtpServer=SmtpServerHelper.GetSmtpServer("user","12345678","smtp.126.com"); ///
///
public static string SmtpServer
{
set { _SmtpServer=value;}
get { return _SmtpServer;}
}

///
/// 邮件可以通过 Microsoft Windows 2000 中内置的 SMTP 邮件服务或任意 SMTP 服务器来传送
///
public class SmtpMail
{

ErrCodeHT.Add("421","服务未就绪,关闭传输信道");
ErrCodeHT.Add("432","需要一个密码转换");
ErrCodeHT.Add("450","要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)");
ErrCodeHT.Add("451","放弃要求的操作;处理过程中出错");
ErrCodeHT.Add("452","系统存储不足,要求的操作未执行");
ErrCodeHT.Add("454","临时认证失败");
ErrCodeHT.Add("500","邮箱地址错误");
ErrCodeHT.Add("501","参数格式错误");
ErrCodeHT.Add("502","命令不可实现");
ErrCodeHT.Add("503","服务器需要SMTP验证");
ErrCodeHT.Add("504","命令参数不可实现");
ErrCodeHT.Add("530","需要认证");
ErrCodeHT.Add("534","认证机制过于简单");
ErrCodeHT.Add("538","当前请求的认证机制需要加密");
ErrCodeHT.Add("550","要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)");
ErrCodeHT.Add("551","用户非本地,请尝试");
ErrCodeHT.Add("552","过量的存储分配,要求的操作未执行");
ErrCodeHT.Add("553","邮箱名不可用,要求的操作未执行(例如邮箱格式错误)");
ErrCodeHT.Add("554","传输失败");

(filepath.Substring(filepath.LastIndexOf("\")+1))+"?=""+enter;
SendBufferstr += "Content-Transfer-Encoding: base64"+enter;
SendBufferstr += "Content-Disposition: attachment;"+enter;
SendBufferstr += " filename="=?"+Charset.ToUpper()+"?B?"+Base64Encode

///
/// 添加一个附件,当指定的附件不存在时,忽略该附件,不产生异常。
///
/// 附件的绝对路径
public void Add(string filePath)
{
//当附件存在时才加入,否则忽略
if (System.IO.File.Exists(filePath))
{
if (_Attachments.Count {
_Attachments.Add(filePath);
}
}
}

SendBufferstr+="CC:";
for(int i=0;i {
SendBufferstr+=Recipient[i].ToString() + ",";
}
SendBufferstr+=enter;

[Test]
public void TestSendMail()
{
SmtpMail.SmtpServer="smtp.126.com";
MailMessage mail=new MailMessage();
mail.From="qs1976@126.com";
mail.FromName="曾青松";
mail.AddRecipients("qs1976@126.com");
mail.Subject="主题:测试邮件";
mail.BodyFormat=MailFormat.Text;
mail.Body="测试的内容.";
mail.Attachments.Add("c:\test.txt");
SmtpMail.Send(mail,"","");//请填写自己的测试邮件帐号
}
}
}

源码下载:点击下载

networkStream.Close();
tcpClient.Close();
return true;
}
}

个"));
}
}

if(!Dialog(SendBufferstr,"错误信件信息"))
return false;

SendBuffer[i]="RCPT TO:" + enter;

//SendBufferstr+="To:" + ToName + "" +CRLF;
//至少要有一个收件人
if (mailMessage.Recipients.Count==0)
{
return false;
}
else
{
SendBufferstr += "To:=?"+mailMessage.Charset.ToUpper()+"?B?"+
Base64Encode((string)mailMessage.Recipients[0])+"?="+""+CRLF;
}

default:
priority="Normal";
break;
}
}
}

private string _From;//发件人地址
private string _FromName;//发件人姓名
private IList _Recipients;//收件人
private MailAttachments _Attachments;//附件
private string _Body;//内容
private string _Subject;//主题
private MailFormat _BodyFormat;//邮件格式
private string _Charset="GB2312";//字符编码格式
private MailPriority _Priority;//邮件优先级
}
#endregion

///
/// 是否Html邮件
///
public bool Html
{
get
{
return this._html;
}
set
{
this._html = value;
}
}

SendBuffer[i]="RCPT TO:" + CRLF;

息,如"www.smtpserver.com"
public bool Send(string smtpserver)
{
MailDomain=smtpserver;
return Send();
}

354 Start mail input; end with .
*/

void Dispose()
{
if(ns!=null)ns.Close();
if(tc!=null)tc.Close();
}

///
/// 接收SMTP服务器回应
///
private string RecvResponse()
{
int StreamSize;
string Returnvalue = String.Empty;
byte[] ReadBuffer = new byte[1024] ;
try
{
StreamSize=networkStream.Read(ReadBuffer,0,ReadBuffer.Length);
}
catch
{
errmsg="网络连接错误";
return "false";
}

///
/// SMTP认证时使用的密码
///
private string password="";

//验证网络连接是否正确
if(RightCodeHT[RecvResponse().Substring(0,3)]==null)
{
errmsg="网络连接失败";
return false;
}
return true;
}

///
/// 是否需要SMTP验证
///
private bool ESmtp=false;

//if(ReplyTo.Trim()!="")
//{
// SendBufferstr+="Reply-To: " + ReplyTo + CRLF;
//}

///
/// 与服务器交互,发送一条命令并接收回应。
///
/// 一个要发送的命令
/// 如果错误,要反馈的信息
private bool Dialog(string str,string errstr)
{
if(str==null||str.Trim()=="")
{
return true;
}
if(SendCommand(str))
{
string RR=RecvResponse();
if(RR=="false")
{
return false;
}
string RRCode=RR.Substring(0,3);
if(RightCodeHT[RRCode]!=null)
{
return true;
}
else
{
if(ErrCodeHT[RRCode]!=null)
{
errmsg+=(RRCode+ErrCodeHT[RRCode].ToString());
errmsg+=enter;
}
else
{
errmsg+=RR;
}
errmsg+=errstr;
return false;
}
}
else
{
return false;
}

RightCodeHT.Add("220","服务就绪");
RightCodeHT.Add("221","服务关闭传输信道");
RightCodeHT.Add("235","验证成功");
RightCodeHT.Add("250","要求的邮件操作完成");
RightCodeHT.Add("251","非本地用户,将转发向");
RightCodeHT.Add("334","服务器响应验证Base64字符串");
RightCodeHT.Add("354","开始邮件输入,以.结束");

///
/// 发件人地址
///
public string From
{
get
{
return _from;
}
set
{
this._from = value;
}
}

[Test]
public void TestMailAttachments()
{
SkyDev.Web.Mail.MailAttachments attachments=new MailAttachments();
Assert.AreEqual(0,attachments.Count,"初始化MailAttachments");
attachments.Add("c:\autoexec.bat");
Assert.AreEqual(1,attachments.Count,"增加附件(附件确实存在)");
attachments.Add("c:\autoexec.dat.txt");
Assert.AreEqual(1,attachments.Count,"增加附件(附件不存在)");
attachments.Clear();
Assert.AreEqual(0,attachments.Count,"清除附件");
}

//
SendBuffer=new string[recipientmaxnum];
for(int i=0;i {
SendBuffer[i]="RCPT TO:" + enter;

///
/// 与服务器交互,发送一条命令并接收回应。
///
/// 一个要发送的命令
/// 如果错误,要反馈的信息
private bool Dialog(string str,string errstr)
{
if(str==null||str.Trim()==string.Empty)
{
return true;
}
if(SendCommand(str))
{
string RR=RecvResponse();
if(RR=="false")
{
return false;
}
//检查返回的代码,根据[RFC 821]返回代码为3位数字代码如220
string RRCode=RR.Substring(0,3);
if(RightCodeHT[RRCode]!=null)
{
return true;
}
else
{
if(ErrCodeHT[RRCode]!=null)
{
errmsg+=(RRCode+ErrCodeHT[RRCode].ToString());
errmsg+=CRLF;
}
else
{
errmsg+=RR;
}
errmsg+=errstr;
return false;
}
}
else
{
return false;
}
}

if(!Dialog(SendBufferstr,"错误信件信息"))
return false;

public MailPriority Priority
{
get { return _Priority;}
set { _Priority=value;}
}

///
/// 邮件服务器端口号
///
public int MailDomainPort
{
set
{
mailserverport=value;
}
}

mailMessage.Charset.ToLower() + """)) + CRLF;
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
SendBufferstr+= Base64Encode("邮件内容为HTML格式,请选择HTML方式查看") + CRLF + CRLF;

///
/// 邮件主题
///
public string Subject
{
get
{
return this._subject;
}
set
{
this._subject = value;
}
}

}

private string enter="rn";

public MailFormat BodyFormat
{
set { _BodyFormat=value;}
get { return _BodyFormat;}
}

///
/// 邮件正文
///
public string Body
{
get
{
return this._body;
}
set
{
this._body = value;
}
}

private string GetPriorityString(MailPriority mailPriority)
{
string priority="Normal";
if (mailPriority==MailPriority.Low)
{
priority="Low";
}
else if (mailPriority==MailPriority.High)
{
priority="High";
}
return priority;
}

}
if(!Dialog(SendBuffer,"收件人地址有误"))
return false;

421 Service not available,
closing transmission channel
[This may be a reply to any command if the service knows it
must shut down]
450 Requested mail action not taken: mailbox unavailable
[E.g., mailbox busy]
451 Requested action aborted: local error in processing
452 Requested action not taken: insufficient system storage

case "3":
priority="Normal";
break;

public int Count//获取附件个数
{
get { return _Attachments.Count;}
}

///
/// SMTP回应代码哈希表
///
private void SMTPCodeAdd()
{
ErrCodeHT.Add("500","邮箱地址错误");
ErrCodeHT.Add("501","参数格式错误");
ErrCodeHT.Add("502","命令不可实现");
ErrCodeHT.Add("503","服务器需要SMTP验证");
ErrCodeHT.Add("504","命令参数不可实现");
ErrCodeHT.Add("421","服务未就绪,关闭传输信道");
ErrCodeHT.Add("450","要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)");
ErrCodeHT.Add("550","要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)");
ErrCodeHT.Add("451","放弃要求的操作;处理过程中出错");
ErrCodeHT.Add("551","用户非本地,请尝试");
ErrCodeHT.Add("452","系统存储不足,要求的操作未执行");
ErrCodeHT.Add("552","过量的存储分配,要求的操作未执行");
ErrCodeHT.Add("553","邮箱名不可用,要求的操作未执行(例如邮箱格式错误)");
ErrCodeHT.Add("432","需要一个密码转换");
ErrCodeHT.Add("534","认证机制过于简单");
ErrCodeHT.Add("538","当前请求的认证机制需要加密");
ErrCodeHT.Add("454","临时认证失败");
ErrCodeHT.Add("530","需要认证");

///
/// 发送电子邮件,SMTP服务器不需要身份验证
///
///
///
///
///
public bool SendEmail(string smtpServer,int port,MailMessage mailMessage)
{
return SendEmail(smtpServer,port,false,"","",mailMessage);
}

#endregion

SendBufferstr="QUIT" + CRLF;
if(!Dialog(SendBufferstr,"断开连接时错误"))
return false;

///
/// 是否Html邮件
///
private bool _html=false;

///
/// 发送电子邮件,SMTP服务器需要身份验证
///
///
///
///
///
///
///
public bool SendEmail(string smtpServer,int port,string username,string password,MailMessage mailMessage)
{
return SendEmail(smtpServer,port,false,username,password,mailMessage);
}

///
/// 服务器交互记录
///
private string logs="";

///
/// 与服务器交互,发送一组命令并接收回应。
///

///
/// 错误消息反馈
///
public string ErrorMessage
{
get
{
return errmsg;
}
}

#region Class SmtpMail
public class SmtpServerHelper
{
private string CRLF="rn";//回车换行

Charset.ToLower() + """)) + enter;
SendBufferstr+="Content-Transfer-Encoding: base64" + enter + enter;
SendBufferstr+= Base64Encode(Body) + enter;
}

public string this[int index]
{
get { return (string)_Attachments[index];}
}
///
/// 添加邮件附件
///
/// 附件的绝对路径
public void Add(params string[] filePath)
{
if(filePath==null)
{
throw(new ArgumentNullException("非法的附件"));
}
else
{
for(int i=0;i {
Add(filePath[i]);
}
}
}

///
/// 发件人姓名
///
public string FromName
{
get
{
return this._fromName;
}
set
{
this._fromName = value;
}
}

*/

///
/// 添加一组密件收件人(不超过10个),参数为字符串数组
///
/// 保存有收件人地址的字符串数组(不超过10个)
public bool AddRecipientBCC(string[] str)
{
for(int i=0;i {
if(!AddRecipientBCC(str[i]))
{
return false;
}
}
return true;
}

SendBufferstr+="Content-Type: text/html;" + CRLF;
SendBufferstr+=((mailMessage.Charset=="")?(" charset="iso-8859-1""):(" charset="" +
mailMessage.Charset.ToLower() + """)) + CRLF;
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
SendBufferstr+= Base64Encode(mailMessage.Body) + CRLF + CRLF;
SendBufferstr += "--=====003_Dragon520636771063_=====--"+CRLF;
}
else
{
if(mailMessage.Attachments.Count!=0)
{
SendBufferstr += "--=====001_Dragon303406132050_====="+CRLF;
}
SendBufferstr+="Content-Type: text/plain;" + CRLF;
SendBufferstr+=((mailMessage.Charset=="")?(" charset="iso-8859-1""):(" charset="" +
mailMessage.Charset.ToLower() + """)) + CRLF;
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
SendBufferstr+= Base64Encode(mailMessage.Body) + CRLF;
}

#endregion

///
/// SMTP正确代码哈希表
///
private Hashtable RightCodeHT = new Hashtable();

///
/// SMTP认证时使用的用户名
///
public string MailServerUserName
{
set
{
if(value.Trim()!="")
{
username=value.Trim();
ESmtp=true;
}
else
{
username="";
ESmtp=false;
}
}
}

if(!Dialog(SendBuffer,"密件收件人地址有误"))
return false;
*/

///
/// 将Base64字符串解码为普通字符串
///
/// 要解码的字符串
private string Base64Decode(string str)
{
byte[] barray;
barray=Convert.FromBase64String(str);
return Encoding.Default.GetString(barray);
}

/*
211 System status, or system help reply
214 Help message
[Information on how to use the receiver or the meaning of a
particular non-standard command; this reply is useful only
to the human user]
220 Service ready
221 Service closing transmission channel
250 Requested mail action okay, completed
251 User not local; will forward to

///
/// SMTP错误代码哈希表
///
private Hashtable ErrCodeHT = new Hashtable();

///
/// 格式:SmtpAccount:Password@SmtpServerAddress

if(!Dialog(SendBuffer,"密件收件人地址有误"))
return false;
*/
SendBufferstr="DATA" + enter;
if(!Dialog(SendBufferstr,""))
return false;

///
/// SMTP回应代码哈希表
///
private void SMTPCodeAdd()
{
//[RFC 821 4.2.1.]
/*
4.2.2. NUMERIC ORDER LIST OF REPLY CODES

这个是对于
EMAIL发送系统(C#+基于SMTP认证) 1.0

上一篇:php数组声明 下一篇:没有了