using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Configuration;
using System.Text;
using System.XML;
using System.IO;
using System.Web;
using System.Web.Mail;namespace mail
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class mSendMail
{
private TcpClient tcpClt;
private NetworkStream networkStm;
private Hashtable rightCodeHT = new Hashtable();
private string smtpServerName;
private int smtpServerPort;
private string userName;
private string passWord;
private string to;
private string from;
private string fromName;
private string charset;
private string recipientName;
private string subject;
private string body;
private string priority;
static string Send_Method;
public mSendMail()
{
} public mSendMail(string strToName,string strTo,string strBody)
{
to = strTo;
recipientName = strToName;
body = strBody;
smtpCodeAdd();
}
public mSendMail(string strToName,string strTo, string strSubject, string strBody)
{
to = strTo;
recipientName = strToName;
subject = strSubject;
body = strBody;
smtpCodeAdd();
}
public mSendMail(string strToName,string strTo,string strFromName,string strFrom, string strSubject, string strBody)
{
to = strTo;
recipientName = strToName;
from = strFrom;
fromName = strFromName;
subject = strSubject;
body = strBody;
smtpCodeAdd();
}
private bool initialize()
{
try
{
if(Send_Method=="1")
{
smtpServerName = ConfigurationSettings.AppSettings["smtpServerName"];
smtpServerPort = Convert.ToInt32(ConfigurationSettings.AppSettings["smtpServerPort"]);
userName = ConfigurationSettings.AppSettings["userName"];
password = ConfigurationSettings.AppSettings["password"];
//from = ConfigurationSettings.AppSettings["from"];
//fromName = ConfigurationSettings.AppSettings["fromName"];
charset = ConfigurationSettings.AppSettings["charset"];
}
else
{
smtpServerName ="";//your smtp server
smtpServerPort =25;
userName ="";//your name
password ="";//your pass
charset ="GB2312";
//from = ConfigurationSettings.AppSettings["from"];
//fromName = ConfigurationSettings.AppSettings["fromName"];
}
}
catch
{
return false;
}
priority = "Normal";
//subject = "//";
//smtpCodeAdd();
return true;
} private string Base64Encode(string str)
{
byte[] barray;
barray=Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
} private void smtpCodeAdd()
{
rightCodeHT.Add("220","");
rightCodeHT.Add("250","");
rightCodeHT.Add("251","");
rightCodeHT.Add("354","");
rightCodeHT.Add("221","");
rightCodeHT.Add("334","");
rightCodeHT.Add("235","");
} private bool sendCommand(string str)
{
byte[] writeBuffer;
writeBuffer = Encoding.Default.GetBytes(str);
try
{
networkStm.Write(writeBuffer, 0, writeBuffer.Length);
}
catch
{
return false;
}
return true;
} private bool isRight()
{
int streamSize;
byte[] readBuffer = new byte[1024];
string returnValue = "";
try
{
streamSize = networkStm.Read(readBuffer, 0, readBuffer.Length);
}
catch
{
return false;
}
if (streamSize != 0)
returnValue = Encoding.Default.GetString(readBuffer, 0, streamSize);
if(rightCodeHT[returnValue.Substring(0,3)] == null)
return false;
return true;
} public bool sendMail()
{
if (!initialize())
return false;
try
{
tcpClt = new TcpClient(smtpServerName, smtpServerPort);
}
catch
{
return false;
}
networkStm = tcpClt.GetStream();
if (!isRight())
return false;
string[] sendBuffer;
string enter = "\r\n"; sendBuffer = new String[9];
sendBuffer[0] = "EHLO " + smtpServerName + enter;
sendBuffer[1] = "AUTH LOGIN" + enter;
sendBuffer[2] = Base64Encode(userName) + enter;
sendBuffer[3] = Base64Encode(password) + enter;
sendBuffer[4] = "MAIL FROM:<" + from + ">" + enter;
sendBuffer[5] = "RCPT TO:<" + to +">" + enter;
sendBuffer[6] = "DATA" + enter;
sendBuffer[7] = "From:" + fromName + "<" + from +">" + enter;
sendBuffer[7] += "To:=?" + charset.ToUpper() + "?B?"
+ Base64Encode(recipientName) + "?=" + "<" + to + ">" + enter;
sendBuffer[7] += "Subject:" + "=?" + charset.ToUpper() + "?B?"
+ Base64Encode(subject) + "?=" + enter;
sendBuffer[7] += "X-Priority:" + priority + enter;
sendBuffer[7] += "X-MSMail-Priority:" + priority + enter;
sendBuffer[7] += "Importance:" + priority + enter;
sendBuffer[7] += "X-Mailer: Huolx.Pubclass" + enter;
sendBuffer[7] += "MIME-Version: 1.0" + enter;
sendBuffer[7] += "Content-Type: multipart/mixed;" + enter;
sendBuffer[7] += " boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\"" + enter;
sendBuffer[7] += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter;
sendBuffer[7] += "Content-Type: text/Html;" + enter;
sendBuffer[7] += " charset=\"" + charset.ToLower() + "\"" + enter;
sendBuffer[7] += "Content-Transfer-Encoding: base64" + enter + enter;
sendBuffer[7] += Base64Encode(body) + enter;
sendBuffer[7] += "------=_NextPart_000_00D6_01C29593.AAB31770--" + enter + enter;
sendBuffer[7] += enter + "." + enter;
sendBuffer[8] = "QUIT" + enter; int i; for (i=0;i<sendBuffer.Length;i++)
{
if (!sendCommand(sendBuffer[i]))
return false;
if (!isRight())
return false;
}
tcpClt.Close();
networkStm.Close(); return true;
}
public int Send_Email(string From, string To,string FromName,string ToName,string Subject,string Body)
{
int IsSUCcess = 0;
string s1=To;
int ix;
int iy;
int iz;
char split;
split=',';
string[] MailAddress;
ix=To.LastIndexOf("@");
iy=To.LastIndexOf(".");
iz=To.LastIndexOf(","); if (ix>0 && iy>0 && iy>ix)
{
if (iz>0)
{
MailAddress=s1.Split(split);
for(int i=0;i<MailAddress.Length;i++)
{
ix=MailAddress[i].LastIndexOf("@");
if (MailAddress[i].Substring(ix+1)=="sina.com")
{Send_Method="1";}
else{Send_Method="0";}
mSendMail mySendMail = new mSendMail(ToName,MailAddress[i],FromName,From,Subject,Body);
try
{
if (mySendMail.sendMail()== true)
IsSuccess = 0;
}
catch
{
}
}
}
else
{
if (s1.Substring(ix+1)=="sina.com")
{Send_Method="1";}
else{Send_Method="0";}
mSendMail mySendMail = new mSendMail(ToName,To,FromName,From,Subject,Body);
try
{
if (mySendMail.sendMail()== true)
IsSuccess = 0;
}
catch
{}
}
}
else{IsSuccess=2;}
return IsSuccess;
}
public int Send_TuiJian(string From, string To,string FromName,string ToName,string Title,string NewsAddr,string Message)
{
//读取邮件内容
string MessagePath;
if(System.Configuration.ConfigurationSettings.AppSettings["MessagePath"] != null)
MessagePath = System.Configuration.ConfigurationSettings.AppSettings["MessagePath"].ToString();
else
MessagePath = @"D:\abc.htm";
string strTemplate;
StreamReader stream = new StreamReader(MessagePath,System.Text.Encoding.GetEncoding("GB2312"));
try
{
stream.BaseStream.Seek(0,SeekOrigin.Begin);
strTemplate = stream.ReadToEnd();
strTemplate.Replace("\"","'");
}
finally
{
stream.Close();
} //替换
string tmpMessage = Message;
try
{
for (int i=0; i<=(Message.Length/35); i++)
{
tmpMessage = tmpMessage.Insert((i+1)*35,"<br>");
}
}
catch
{
}
Message = tmpMessage;
Message = Message + "<br>";
strTemplate = strTemplate.Insert(strTemplate.LastIndexOf("此致,礼"),Message);
strTemplate = strTemplate.Replace("aa",ToName);
strTemplate = strTemplate.Replace("bb",FromName);
strTemplate = strTemplate.Replace("cc",Title);
strTemplate = strTemplate.Replace(@"dd",NewsAddr);
strTemplate = strTemplate.Replace("1980年",DateTime.Now.ToShortDateString()); //发送邮件
int IsSuccess = 0;
string Subject = "想请你去看看";
//邮件地址判定
string s1=To;
int ix;
int iy;
int iz;
char split;
split=',';
string[] MailAddress;
ix=To.LastIndexOf("@");
iy=To.LastIndexOf(".");
iz=To.LastIndexOf(",");
if (ix>0 && iy>0 && iy>ix)
{
if (iz>0)
{
MailAddress=s1.Split(split);
for(int i=0;i<MailAddress.Length;i++)
{
ix=MailAddress[i].LastIndexOf("@");
if (MailAddress[i].Substring(ix+1)=="sina.com")
{Send_Method="1";}
else{Send_Method="0";}
mSendMail mySendMail = new mSendMail(ToName,MailAddress[i],FromName,From,Subject,strTemplate);
try
{
if (mySendMail.sendMail()== true)
IsSuccess = 0;
}
catch
{
}
}
}
else
{
if (s1.Substring(ix+1)=="sina.com")
{Send_Method="1";}
else{Send_Method="0";}
mSendMail mySendMail = new mSendMail(ToName,To,FromName,From,Subject,strTemplate);
try
{
if (mySendMail.sendMail()== true)
IsSuccess = 0;
}
catch
{}
}
}
else{IsSuccess=2;}
return IsSuccess; }
}}