class SMTPMail
{
public SMTPMail()
{
//添加构造函数逻辑
}
public string server = "";
private int port = 25;
private string userName = "";
private string passWord = "";
private string from = "";
private string to = "";
private string fromName = "";
private string toName = "";
private string subject = "";
private string body = "";
private string htmlBody = "";
private bool isHtml = false;
private string languageEncoding = "GB2312";
private string encoding = "8bit";
private int priority = 3;
private ArrayList attachments = new ArrayList();
public string Server
{
get { return server; }
set { if (value != server)server = value; }
}
public int Port
{
get { return port; }
set { if (value != port)port = value; }
}
public string UserName
{
get { return userName; }
set { if (value != userName)userName = value; }
}
public string PassWord
{
get { return passWord; }
set { if (value != passWord)passWord = value; }
}
public string From
{
get { return from; }
set { if (value != from)from = value; }
}
public string To
{
get { return to; }
set { if (value != to)to = value; }
}
public string FromName
{
get { return fromName; }
set { if (value != fromName)fromName = value; }
}
public string ToName
{
get { return toName; }
set { if (value != toName)toName = value; }
}
public string Subject
{
get { return subject; }
set { if (value != subject)subject = value; }
}
public string Body
{
get { return body; }
set { if (value != body)body = value; }
}
public string HtmlBody
{
get { return htmlBody; }
set { if (value != htmlBody)htmlBody = value; }
}
public bool IsHtml
{
get { return isHtml; }
set { if (value != isHtml)isHtml = value; }
}
public string LanguageEncoding
{
get { return languageEncoding; }
set { if (value != languageEncoding)languageEncoding = value; }
}
public string MailEncoding
{
get { return encoding; }
set { if (value != encoding)encoding = value; }
}
public int Priority
{
get { return priority; }
set { if (value != priority)priority = value; }
}
public IList Attachments
{
get { return attachments; }
//set { if (value != attachments)attachments = value; }
}
protected void WriteString(NetworkStream netStream, string str)
{
str = str + "\r\n";
byte[] bWrite = Encoding.GetEncoding(languageEncoding).GetBytes(str.ToCharArray());
int start = 0;
int length = bWrite.Length;
int page = 0;
int size = 75;
int count = size;
try
{
if (length > 75)
{
if ((length / size) * size < length)
page = length / size + 1;
else
page = length / size;
for (int i = 0; i < page; i++)
{
start = i * size;
if (i == page - 1)
count = length - (i * size);
netStream.Write(bWrite, start, count);
}
}
else
netStream.Write(bWrite, 0, bWrite.Length);
}
catch (Exception)
{
//忽略错误
}
}
protected string ReadString(NetworkStream netStream)
{
string sp = null;
byte[] by = new byte[1024];
int size = netStream.Read(by,0,by.Length);
if (size > 0)
{
sp = Encoding.Default.GetString(by);
}
return sp;
}
protected bool Command(NetworkStream netStream, string command, string state)
{
string sp = null;
bool success = false;
try
{
WriteString(netStream, command);
sp = ReadString(netStream);
if (sp.IndexOf(state) != -1)
success = true;
}
catch (Exception)
{
//忽略错误
}
return success;
}
protected string ToBase64(string str)
{
try
{
byte[] by = Encoding.Default.GetBytes(str.ToCharArray());
str = Convert.ToBase64String(by);
}
catch
{
//忽略错误
}
return str;
}
public void SendMail()
{
TcpClient tcp = null;
try
{
tcp = new TcpClient(server, port);
}
catch (Exception)
{
throw new Exception("无法连接服务器");
}
ReadString(tcp.GetStream());
if(!Command(tcp.GetStream(),"HELO","250"))
throw new Exception("登录阶段失败");
if (userName != "")
{
if (!Command(tcp.GetStream(), "AUTO LOGIN", "334"))
throw new Exception("身份验证失败");
string nameB64 = ToBase64(userName);
if(!Command(tcp.GetStream(),nameB64,"334"))
throw new Exception("身份验证失败");
string passB64 = ToBase64(passWord);
if(!Command(tcp.GetStream(),passB64,"235"))
throw new Exception("身份验证失败");
}
if (!Command(tcp.GetStream(), "mail From:" + "<" + from + ">", "250"))
throw new Exception("发送地址错误");
if (!Command(tcp.GetStream(), "rcpt to:" + "<" + to + ">", "250"))
throw new Exception("接收地址错误");
if (!Command(tcp.GetStream(), "data", "354"))
throw new Exception("发送数据错误");
WriteString(tcp.GetStream(), "Date:" + DateTime.Now);
WriteString(tcp.GetStream(), "From:" + fromName + "<" + from + ">");
WriteString(tcp.GetStream(), "Subject:" + subject);
WriteString(tcp.GetStream(), "To:" + toName + "<" + to + ">");
WriteString(tcp.GetStream(), "Content-Type:multipart/mixed;boundary=\"unique-boundary-1\"");
WriteString(tcp.GetStream(),"Reply-To"+from);
WriteString(tcp.GetStream(), "X-Priority" + priority);
WriteString(tcp.GetStream(), "MIME-Version;1.0" );
WriteString(tcp.GetStream(), "Message-Id:"+DateTime.Now.ToFileTime()+"@security.com");
WriteString(tcp.GetStream(), "Content-Transfer-Encoding:"+encoding);
WriteString(tcp.GetStream(), "X-Mailer:JcPersonal.Utility.MailSender");
WriteString(tcp.GetStream(), "");
WriteString(tcp.GetStream(), ToBase64("This is a multi-part message in MIME formet."));
WriteString(tcp.GetStream(), "");
WriteString(tcp.GetStream(), "--unique-boundary-1");
WriteString(tcp.GetStream(),"Content-Type:multipart/alternative;Boundary=\"unique-bounderary-2\"");
WriteString(tcp.GetStream(), "");
if (!isHtml)
{
WriteString(tcp.GetStream(), "--unique-boundary-2");
WriteString(tcp.GetStream(), "Content-Type:text/plain;charset=" + languageEncoding);
WriteString(tcp.GetStream(), "Content-Transfer-Encoding:" + encoding);
WriteString(tcp.GetStream(), "");
WriteString(tcp.GetStream(), "");
WriteString(tcp.GetStream(), body);
WriteString(tcp.GetStream(), "");
WriteString(tcp.GetStream(), "--unique-boundary-2--");
WriteString(tcp.GetStream(), "");
}
else
{
WriteString(tcp.GetStream(), "--unique-boundary-2");
WriteString(tcp.GetStream(), "Content-Type:text/html;charset=" + languageEncoding);
WriteString(tcp.GetStream(), "Content-Transfer-Encoding:" + encoding);
WriteString(tcp.GetStream(), "");
WriteString(tcp.GetStream(), htmlBody);
WriteString(tcp.GetStream(), "");
WriteString(tcp.GetStream(), "--unique-boundary-2--");
WriteString(tcp.GetStream(), "");
}
for (int i = 0; i < attachments.Count; i++)
{
WriteString(tcp.GetStream(), "--unique-boundary-1");
WriteString(tcp.GetStream(), "Content-Type:application/octet-stream;name=\""+((AttachmentInfo)attachments[i]).FileName+"\"");
WriteString(tcp.GetStream(), "Content-Transfer-Encoding:base64" );
WriteString(tcp.GetStream(), "Content-Disposition:attachment;filename=\"" + ((AttachmentInfo)attachments[i]).FileName + "\"");
WriteString(tcp.GetStream(), "");
WriteString(tcp.GetStream(), ((AttachmentInfo)attachments[i]).Bytes);
WriteString(tcp.GetStream(), "");
}
WriteString(tcp.GetStream(), "");
WriteString(tcp.GetStream(), "");
if (!Command(tcp.GetStream(), ".", "250"))
throw new Exception("发送数据错误");
tcp.Close();
}
public struct AttachmentInfo
{
public string FileName
{
get { return fileName; }
set { fileName = Path.GetFileName(value); }
}private string fileName;
public string Bytes
{
get { return bytes; }
set { if (value != bytes)bytes = value; }
}private string bytes;
public AttachmentInfo(string ifileName, Stream stream)
{
fileName = Path.GetFileName(ifileName);
byte[] by = new byte[stream.Length];
stream.Read(by, 0, (int)stream.Length);
bytes = Convert.ToBase64String(by);
}
public AttachmentInfo(string ifileName, byte[] ibytes)
{
fileName = Path.GetFileName(ifileName);
bytes = Convert.ToBase64String(ibytes);
}
public AttachmentInfo(string path)
{
fileName = Path.GetFileName(path);
FileStream file = new FileStream(path,FileMode.Open);
byte[] by = new byte[file.Length];
bytes = Convert.ToBase64String(by);
file.Close();
}
}
}