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
{
///
1<summary>
2/// Class1 的摘要说明。
3/// </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
1<sendbuffer.length;i++) (!isright())="" (!sendcommand(sendbuffer[i]))="" (ix="" ;="" body)="" char="" false;="" from,="" fromname,string="" if="" int="" issuccess="0;" ix='To.LastIndexOf("@");' ix;="" iy='To.LastIndexOf(".");' iy;="" iz='To.LastIndexOf(",");' iz;="" mailaddress;="" networkstm.close();="" public="" return="" s1="To;" send_email(string="" split="," split;="" string="" string[]="" subject,string="" tcpclt.close();="" to,string="" toname,string="" true;="" {="" }="">0 && iy>0 && iy>ix)
2{
3if (iz>0)
4{
5MailAddress=s1.Split(split);
6for(int i=0;i<mailaddress.length;i++) !="null)" (int="" (mailaddress[i].substring(ix+1)="sina.com" (mysendmail.sendmail()="true)" (s1.substring(ix+1)="sina.com" )="" ;}="" catch="" else="" else{issuccess="2;}" else{send_method="0" finally="" for="" from,="" fromname,string="" i="0;" i++)="" i<="(Message.Length/35);" if="" if(system.configuration.configurationsettings.appsettings["messagepath"]="" int="" issuccess="0;" issuccess;="" ix='MailAddress[i].LastIndexOf("@");' message)="" messagepath='@"D:\abc.htm";' messagepath;="" msendmail="" msendmail(toname,mailaddress[i],fromname,from,subject,body);="" msendmail(toname,to,fromname,from,subject,body);="" mysendmail="new" newsaddr,string="" public="" return="" send_tuijian(string="" stream="new" stream.basestream.seek(0,seekorigin.begin);="" stream.close();="" streamreader="" streamreader(messagepath,system.text.encoding.getencoding("gb2312"));="" string="" strtemplate="stream.ReadToEnd();" strtemplate.replace("\"","'");="" strtemplate;="" title,string="" tmpmessage='tmpMessage.Insert((i+1)*35,"<br' to,string="" toname,string="" try="" {="" {send_method="1" {}="" }="" 替换="" 读取邮件内容="">");
7}
8}
9catch
10{
11}
12Message = tmpMessage;
13Message = Message + "<br/>";
14strTemplate = strTemplate.Insert(strTemplate.LastIndexOf("此致,礼"),Message);
15strTemplate = strTemplate.Replace("aa",ToName);
16strTemplate = strTemplate.Replace("bb",FromName);
17strTemplate = strTemplate.Replace("cc",Title);
18strTemplate = strTemplate.Replace(@"dd ",NewsAddr );
19strTemplate = strTemplate.Replace("1980年",DateTime.Now.ToShortDateString());
20
21//发送邮件
22int IsSuccess = 0;
23string Subject = "想请你去看看";
24
25//邮件地址判断
26string s1=To;
27int ix;
28int iy;
29int iz;
30char split;
31split=',';
32string[] MailAddress;
33
34ix=To.LastIndexOf("@");
35iy=To.LastIndexOf(".");
36iz=To.LastIndexOf(",");
37
38if (ix>0 && iy>0 && iy>ix)
39{
40if (iz>0)
41{
42MailAddress=s1.Split(split);
43for(int i=0;i<MailAddress.Length;i++)
44{
45ix=MailAddress[i].LastIndexOf("@");
46if (MailAddress[i].Substring(ix+1)=="sina.com")
47{Send_Method="1";}
48else{Send_Method="0";}
49
50mSendMail mySendMail = new mSendMail(ToName,MailAddress[i],FromName,From,Subject,strTemplate);
51try
52{
53if (mySendMail.sendMail()== true)
54IsSuccess = 0;
55}
56catch
57{
58
59}
60
61}
62}
63else
64{
65if (s1.Substring(ix+1)=="sina.com")
66{Send_Method="1";}
67else{Send_Method="0";}
68
69mSendMail mySendMail = new mSendMail(ToName,To,FromName,From,Subject,strTemplate);
70try
71{
72if (mySendMail.sendMail()== true)
73IsSuccess = 0;
74}
75catch
76{}
77
78}
79}
80else{IsSuccess=2;}
81return IsSuccess;
82
83}
84}
85
86}</mailaddress.length;i++)></sendbuffer.length;i++)>