分享
 
 
 

发布源码:高效的Esmtp,带验证,用Socket编写

王朝other·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

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; }

}}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有