1
using System;
2
using System.Net;
3
using System.Net.Sockets;
4
using System.Collections;
5
using System.Configuration;
6
using System.Text;
7
using System.Xml;
8
using System.IO;
9
using System.Web;
10
using System.Web.Mail;
11
12
namespace mail
13
{
14
/// <summary>
15
/// Class1 的摘要说明。
16
/// </summary>
17
public class mSendMail
18
{
19
private TcpClient tcpClt;
20
private.NetworkStream.NetworkStm;
21
private Hashtable rightCodeHT = new Hashtable();
22
private string smtpServerName;
23
private int smtpServerPort;
24
private string userName;
25
private string password;
26
private string to;
27
private string from;
28
private string fromName;
29
private string charset;
30
private string recipientName;
31
private string subject;
32
private string body;
33
private string priority;
34
35
static string Send_Method;
36
37
38
39
public mSendMail()
40
{
41
42
}
43
44
public mSendMail(string strToName,string strTo,string strBody)
45
{
46
to = strTo;
47
recipientName = strToName;
48
body = strBody;
49
smtpCodeAdd();
50
}
51
public mSendMail(string strToName,string strTo, string strSubject, string strBody)
52
{
53
to = strTo;
54
recipientName = strToName;
55
subject = strSubject;
56
body = strBody;
57
smtpCodeAdd();
58
}
59
public mSendMail(string strToName,string strTo,string strFromName,string strFrom, string strSubject, string strBody)
60
{
61
to = strTo;
62
recipientName = strToName;
63
from = strFrom;
64
fromName = strFromName;
65
subject = strSubject;
66
body = strBody;
67
smtpCodeAdd();
68
}
69
private bool initialize()
70
{
71
try
72
{
73
if(Send_Method=="1")
74
{
75
smtpServerName = ConfigurationSettings.AppSettings["smtpServerName"];
76
smtpServerPort = Convert.ToInt32(ConfigurationSettings.AppSettings["smtpServerPort"]);
77
userName = ConfigurationSettings.AppSettings["userName"];
78
password = ConfigurationSettings.AppSettings["password"];
79
//from = ConfigurationSettings.AppSettings["from"];
80
//fromName = ConfigurationSettings.AppSettings["fromName"];
81
charset = ConfigurationSettings.AppSettings["charset"];
82
}
83
else
84
{
85
smtpServerName ="";//your smtp server
86
smtpServerPort =25;
87
userName ="";//your name
88
password ="";//your pass
89
charset ="GB2312";
90
//from = ConfigurationSettings.AppSettings["from"];
91
//fromName = ConfigurationSettings.AppSettings["fromName"];
92
}
93
94
95
96
}
97
catch
98
{
99
return false;
100
}
101
priority = "Normal";
102
//subject = "//";
103
//smtpCodeAdd();
104
return true;
105
}
106
107
private string Base64Encode(string str)
108
{
109
byte[] barray;
110
barray=Encoding.Default.GetBytes(str);
111
return Convert.ToBase64String(barray);
112
}
113
114
private void smtpCodeAdd()
115
{
116
rightCodeHT.Add("220","");
117
rightCodeHT.Add("250","");
118
rightCodeHT.Add("251","");
119
rightCodeHT.Add("354","");
120
rightCodeHT.Add("221","");
121
rightCodeHT.Add("334","");
122
rightCodeHT.Add("235","");
123
}
124
125
private bool sendCommand(string str)
126
{
127
byte[] writeBuffer;
128
writeBuffer = Encoding.Default.GetBytes(str);
129
try
130
{
131
.NetworkStm.Write(writeBuffer, 0, writeBuffer.Length);
132
}
133
catch
134
{
135
return false;
136
}
137
return true;
138
}
139
140
private bool isRight()
141
{
142
int streamSize;
143
byte[] readBuffer = new byte[1024];
144
string returnValue = "";
145
try
146
{
147
streamSize =.NetworkStm.Read(readBuffer, 0, readBuffer.Length);
148
}
149
catch
150
{
151
return false;
152
}
153
if (streamSize != 0)
154
returnValue = Encoding.Default.GetString(readBuffer, 0, streamSize);
155
if(rightCodeHT[returnValue.Substring(0,3)] == null)
156
return false;
157
return true;
158
}
159
160
public bool sendMail()
161
{
162
if (!initialize())
163
return false;
164
try
165
{
166
tcpClt = new TcpClient(smtpServerName, smtpServerPort);
167
}
168
catch
169
{
170
return false;
171
}
172
.NetworkStm = tcpClt.GetStream();
173
if (!isRight())
174
return false;
175
176
string[] sendBuffer;
177
string enter = "\r\n";
178
179
sendBuffer = new String[9];
180
sendBuffer[0] = "EHLO " + smtpServerName + enter;
181
sendBuffer[1] = "AUTH LOGIN" + enter;
182
sendBuffer[2] = Base64Encode(userName) + enter;
183
sendBuffer[3] = Base64Encode(password) + enter;
184
sendBuffer[4] = "MAIL FROM:<" + from + ">" + enter;
185
sendBuffer[5] = "RCPT TO:<" + to +">" + enter;
186
sendBuffer[6] = "DATA" + enter;
187
sendBuffer[7] = "From:" + fromName + "<" + from +">" + enter;
188
sendBuffer[7] += "To:=?" + charset.ToUpper() + "?B?"
189
+ Base64Encode(recipientName) + "?=" + "<" + to + ">" + enter;
190
sendBuffer[7] += "Subject:" + "=?" + charset.ToUpper() + "?B?"
191
+ Base64Encode(subject) + "?=" + enter;
192
sendBuffer[7] += "X-Priority:" + priority + enter;
193
sendBuffer[7] += "X-MSMail-Priority:" + priority + enter;
194
sendBuffer[7] += "Importance:" + priority + enter;
195
sendBuffer[7] += "X-Mailer: Huolx.Pubclass" + enter;
196
sendBuffer[7] += "MIME-Version: 1.0" + enter;
197
sendBuffer[7] += "Content-Type: multipart/mixed;" + enter;
198
sendBuffer[7] += " boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\"" + enter;
199
sendBuffer[7] += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter;
200
sendBuffer[7] += "Content-Type: text/html;" + enter;
201
sendBuffer[7] += " charset=\"" + charset.ToLower() + "\"" + enter;
202
sendBuffer[7] += "Content-Transfer-Encoding: base64" + enter + enter;
203
sendBuffer[7] += Base64Encode(body) + enter;
204
sendBuffer[7] += "------=_NextPart_000_00D6_01C29593.AAB31770--" + enter + enter;
205
sendBuffer[7] += enter + "." + enter;
206
sendBuffer[8] = "QUIT" + enter;
207
208
int i;
209
210
for (i=0;i<sendBuffer.Length;i++)
211
{
212
if (!sendCommand(sendBuffer[i]))
213
return false;
214
if (!isRight())
215
return false;
216
}
217
218
tcpClt.Close();
219
.NetworkStm.Close();
220
221
return true;
222
}
223
224
225
public int Send_Email(string From, string To,string FromName,string ToName,string Subject,string Body)
226
{
227
int IsSuccess = 0;
228
string s1=To;
229
int ix;
230
int iy;
231
int iz;
232
char split;
233
split=',';
234
string[] MailAddress;
235
236
ix=To.LastIndexOf("@");
237
iy=To.LastIndexOf(".");
238
iz=To.LastIndexOf(",");
239
240
if (ix>0 && iy>0 && iy>ix)
241
{
242
if (iz>0)
243
{
244
MailAddress=s1.Split(split);
245
for(int i=0;i<MailAddress.Length;i++)
246
{
247
ix=MailAddress[i].LastIndexOf("@");
248
if (MailAddress[i].Substring(ix+1)=="sina.com")
249
{Send_Method="1";}
250
else{Send_Method="0";}
251
252
mSendMail mySendMail = new mSendMail(ToName,MailAddress[i],FromName,From,Subject,Body);
253
try
254
{
255
if (mySendMail.sendMail()== true)
256
IsSuccess = 0;
257
}
258
catch
259
{
260
261
}
262
263
}
264
}
265
else
266
{
267
if (s1.Substring(ix+1)=="sina.com")
268
{Send_Method="1";}
269
else{Send_Method="0";}
270
271
mSendMail mySendMail = new mSendMail(ToName,To,FromName,From,Subject,Body);
272
try
273
{
274
if (mySendMail.sendMail()== true)
275
IsSuccess = 0;
276
}
277
catch
278
{}
279
280
}
281
}
282
else{IsSuccess=2;}
283
return IsSuccess;
284
}
285
286
public int Send_TuiJian(string From, string To,string FromName,string ToName,string Title,string NewsAddr,string Message)
287
{
288
//读取邮件内容
289
string MessagePath;
290
if(System.Configuration.ConfigurationSettings.AppSettings["MessagePath"] != null)
291
MessagePath = System.Configuration.ConfigurationSettings.AppSettings["MessagePath"].ToString();
292
else
293
MessagePath = @"D:\abc.htm";
294
string strTemplate;
295
296
StreamReader stream = new StreamReader(MessagePath,System.Text.Encoding.GetEncoding("GB2312"));
297
try
298
{
299
300
stream.BaseStream.Seek(0,SeekOrigin.Begin);
301
strTemplate = stream.ReadToEnd();
302
strTemplate.Replace("\"","'");
303
}
304
finally
305
{
306
stream.Close();
307
}
308
309
//替换
310
string tmpMessage = Message;
311
try
312
{
313
for (int i=0; i<=(Message.Length/35); i++)
314
{
315
tmpMessage = tmpMessage.Insert((i+1)*35,"<br>");
316
}
317
}
318
catch
319
{
320
}
321
Message = tmpMessage;
322
Message = Message + "<br>";
323
strTemplate = strTemplate.Insert(strTemplate.LastIndexOf("此致,礼"),Message);
324
strTemplate = strTemplate.Replace("aa",ToName);
325
strTemplate = strTemplate.Replace("bb",FromName);
326
strTemplate = strTemplate.Replace("cc",Title);
327
strTemplate = strTemplate.Replace(@"dd",NewsAddr);
328
strTemplate = strTemplate.Replace("1980年",DateTime.Now.ToShortDateString());
329
330
//发送邮件
331
int IsSuccess = 0;
332
string Subject = "想请你去看看";
333
334
//邮件地址判断
335
string s1=To;
336
int ix;
337
int iy;
338
int iz;
339
char split;
340
split=',';
341
string[] MailAddress;
342
343
ix=To.LastIndexOf("@");
344
iy=To.LastIndexOf(".");
345
iz=To.LastIndexOf(",");
346
347
if (ix>0 && iy>0 && iy>ix)
348
{
349
if (iz>0)
350
{
351
MailAddress=s1.Split(split);
352
for(int i=0;i<MailAddress.Length;i++)
353
{
354
ix=MailAddress[i].LastIndexOf("@");
355
if (MailAddress[i].Substring(ix+1)=="sina.com")
356
{Send_Method="1";}
357
else{Send_Method="0";}
358
359
mSendMail mySendMail = new mSendMail(ToName,MailAddress[i],FromName,From,Subject,strTemplate);
360
try
361
{
362
if (mySendMail.sendMail()== true)
363
IsSuccess = 0;
364
}
365
catch
366
{
367
368
}
369
370
}
371
}
372
else
373
{
374
if (s1.Substring(ix+1)=="sina.com")
375
{Send_Method="1";}
376
else{Send_Method="0";}
377
378
mSendMail mySendMail = new mSendMail(ToName,To,FromName,From,Subject,strTemplate);
379
try
380
{
381
if (mySendMail.sendMail()== true)
382
IsSuccess = 0;
383
}
384
catch
385
{}
386
387
}
388
}
389
else{IsSuccess=2;}
390
return IsSuccess;
391
392
}
393
}
394
395
}
396
using System;2
using System.Net;3
using System.Net.Sockets;4
using System.Collections;5
using System.Configuration;6
using System.Text;7
using System.Xml;8
using System.IO;9
using System.Web;10
using System.Web.Mail;11

12
namespace mail13
{14
/// <summary>15
/// Class1 的摘要说明。16
/// </summary>17
public class mSendMail18
{19
private TcpClient tcpClt;20
private.NetworkStream.NetworkStm;21
private Hashtable rightCodeHT = new Hashtable();22
private string smtpServerName;23
private int smtpServerPort;24
private string userName;25
private string password;26
private string to;27
private string from;28
private string fromName;29
private string charset;30
private string recipientName;31
private string subject;32
private string body;33
private string priority;34
35
static string Send_Method;36
37

38

39
public mSendMail()40
{41
42
}43

44
public mSendMail(string strToName,string strTo,string strBody)45
{46
to = strTo;47
recipientName = strToName;48
body = strBody;49
smtpCodeAdd();50
}51
public mSendMail(string strToName,string strTo, string strSubject, string strBody)52
{53
to = strTo;54
recipientName = strToName;55
subject = strSubject;56
body = strBody;57
smtpCodeAdd();58
}59
public mSendMail(string strToName,string strTo,string strFromName,string strFrom, string strSubject, string strBody)60
{61
to = strTo;62
recipientName = strToName;63
from = strFrom;64
fromName = strFromName;65
subject = strSubject;66
body = strBody;67
smtpCodeAdd();68
}69
private bool initialize()70
{71
try72
{73
if(Send_Method=="1")74
{75
smtpServerName = ConfigurationSettings.AppSettings["smtpServerName"];76
smtpServerPort = Convert.ToInt32(ConfigurationSettings.AppSettings["smtpServerPort"]);77
userName = ConfigurationSettings.AppSettings["userName"];78
password = ConfigurationSettings.AppSettings["password"];79
//from = ConfigurationSettings.AppSettings["from"];80
//fromName = ConfigurationSettings.AppSettings["fromName"];81
charset = ConfigurationSettings.AppSettings["charset"];82
}83
else84
{85
smtpServerName ="";//your smtp server86
smtpServerPort =25;87
userName ="";//your name88
password ="";//your pass89
charset ="GB2312";90
//from = ConfigurationSettings.AppSettings["from"];91
//fromName = ConfigurationSettings.AppSettings["fromName"]; 92
}93

94
95
96
}97
catch98
{99
return false;100
}101
priority = "Normal";102
//subject = "//";103
//smtpCodeAdd();104
return true;105
}106

107
private string Base64Encode(string str)108
{109
byte[] barray;110
barray=Encoding.Default.GetBytes(str);111
return Convert.ToBase64String(barray);112
}113

114
private void smtpCodeAdd()115
{116
rightCodeHT.Add("220","");117
rightCodeHT.Add("250","");118
rightCodeHT.Add("251","");119
rightCodeHT.Add("354","");120
rightCodeHT.Add("221","");121
rightCodeHT.Add("334","");122
rightCodeHT.Add("235","");123
}124

125
private bool sendCommand(string str)126
{127
byte[] writeBuffer;128
writeBuffer = Encoding.Default.GetBytes(str);129
try130
{131
.NetworkStm.Write(writeBuffer, 0, writeBuffer.Length);132
}133
catch134
{135
return false;136
}137
return true;138
}139

140
private bool isRight()141
{142
int streamSize;143
byte[] readBuffer = new byte[1024];144
string returnValue = "";145
try146
{147
streamSize =.NetworkStm.Read(readBuffer, 0, readBuffer.Length);148
}149
catch150
{151
return false;152
}153
if (streamSize != 0)154
returnValue = Encoding.Default.GetString(readBuffer, 0, streamSize);155
if(rightCodeHT[returnValue.Substring(0,3)] == null)156
return false;157
return true;158
}159

160
public bool sendMail()161
{162
if (!initialize())163
return false;164
try165
{166
tcpClt = new TcpClient(smtpServerName, smtpServerPort);167
}168
catch169
{170
return false;171
}172
.NetworkStm = tcpClt.GetStream();173
if (!isRight())174
return false;175

176
string[] sendBuffer;177
string enter = "\r\n";178

179
sendBuffer = new String[9];180
sendBuffer[0] = "EHLO " + smtpServerName + enter;181
sendBuffer[1] = "AUTH LOGIN" + enter;182
sendBuffer[2] = Base64Encode(userName) + enter;183
sendBuffer[3] = Base64Encode(password) + enter;184
sendBuffer[4] = "MAIL FROM:<" + from + ">" + enter;185
sendBuffer[5] = "RCPT TO:<" + to +">" + enter;186
sendBuffer[6] = "DATA" + enter;187
sendBuffer[7] = "From:" + fromName + "<" + from +">" + enter;188
sendBuffer[7] += "To:=?" + charset.ToUpper() + "?B?"189
+ Base64Encode(recipientName) + "?=" + "<" + to + ">" + enter;190
sendBuffer[7] += "Subject:" + "=?" + charset.ToUpper() + "?B?"191
+ Base64Encode(subject) + "?=" + enter;192
sendBuffer[7] += "X-Priority:" + priority + enter;193
sendBuffer[7] += "X-MSMail-Priority:" + priority + enter;194
sendBuffer[7] += "Importance:" + priority + enter;195
sendBuffer[7] += "X-Mailer: Huolx.Pubclass" + enter;196
sendBuffer[7] += "MIME-Version: 1.0" + enter;197
sendBuffer[7] += "Content-Type: multipart/mixed;" + enter;198
sendBuffer[7] += " boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\"" + enter;199
sendBuffer[7] += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter;200
sendBuffer[7] += "Content-Type: text/html;" + enter;201
sendBuffer[7] += " charset=\"" + charset.ToLower() + "\"" + enter;202
sendBuffer[7] += "Content-Transfer-Encoding: base64" + enter + enter;203
sendBuffer[7] += Base64Encode(body) + enter;204
sendBuffer[7] += "------=_NextPart_000_00D6_01C29593.AAB31770--" + enter + enter;205
sendBuffer[7] += enter + "." + enter;206
sendBuffer[8] = "QUIT" + enter;207

208
int i;209

210
for (i=0;i<sendBuffer.Length;i++)211
{212
if (!sendCommand(sendBuffer[i]))213
return false;214
if (!isRight())215
return false;216
}217

218
tcpClt.Close();219
.NetworkStm.Close();220

221
return true;222
}223

224

225
public int Send_Email(string From, string To,string FromName,string ToName,string Subject,string Body)226
{227
int IsSuccess = 0;228
string s1=To;229
int ix;230
int iy;231
int iz;232
char split;233
split=',';234
string[] MailAddress;235
236
ix=To.LastIndexOf("@");237
iy=To.LastIndexOf(".");238
iz=To.LastIndexOf(",");239

240
if (ix>0 && iy>0 && iy>ix)241
{242
if (iz>0)243
{ 244
MailAddress=s1.Split(split);245
for(int i=0;i<MailAddress.Length;i++)246
{247
ix=MailAddress[i].LastIndexOf("@");248
if (MailAddress[i].Substring(ix+1)=="sina.com")249
{Send_Method="1";}250
else{Send_Method="0";}251
252
mSendMail mySendMail = new mSendMail(ToName,MailAddress[i],FromName,From,Subject,Body);253
try254
{255
if (mySendMail.sendMail()== true)256
IsSuccess = 0;257
}258
catch259
{260
261
}262
263
}264
}265
else266
{267
if (s1.Substring(ix+1)=="sina.com")268
{Send_Method="1";}269
else{Send_Method="0";}270
271
mSendMail mySendMail = new mSendMail(ToName,To,FromName,From,Subject,Body);272
try273
{274
if (mySendMail.sendMail()== true)275
IsSuccess = 0;276
}277
catch278
{}279
280
}281
}282
else{IsSuccess=2;}283
return IsSuccess;284
}285

286
public int Send_TuiJian(string From, string To,string FromName,string ToName,string Title,string NewsAddr,string Message)287
{288
//读取邮件内容289
string MessagePath;290
if(System.Configuration.ConfigurationSettings.AppSettings["MessagePath"] != null)291
MessagePath = System.Configuration.ConfigurationSettings.AppSettings["MessagePath"].ToString();292
else293
MessagePath = @"D:\abc.htm";294
string strTemplate;295
296
StreamReader stream = new StreamReader(MessagePath,System.Text.Encoding.GetEncoding("GB2312")); 297
try298
{299
300
stream.BaseStream.Seek(0,SeekOrigin.Begin);301
strTemplate = stream.ReadToEnd();302
strTemplate.Replace("\"","'");303
}304
finally305
{306
stream.Close();307
}308

309
//替换310
string tmpMessage = Message;311
try312
{313
for (int i=0; i<=(Message.Length/35); i++)314
{315
tmpMessage = tmpMessage.Insert((i+1)*35,"<br>");316
}317
}318
catch319
{320
}321
Message = tmpMessage;322
Message = Message + "<br>";323
strTemplate = strTemplate.Insert(strTemplate.LastIndexOf("此致,礼"),Message);324
strTemplate = strTemplate.Replace("aa",ToName);325
strTemplate = strTemplate.Replace("bb",FromName);326
strTemplate = strTemplate.Replace("cc",Title);327
strTemplate = strTemplate.Replace(@"dd",NewsAddr);328
strTemplate = strTemplate.Replace("1980年",DateTime.Now.ToShortDateString());329

330
//发送邮件331
int IsSuccess = 0;332
string Subject = "想请你去看看";333
334
//邮件地址判断335
string s1=To;336
int ix;337
int iy;338
int iz;339
char split;340
split=',';341
string[] MailAddress;342
343
ix=To.LastIndexOf("@");344
iy=To.LastIndexOf(".");345
iz=To.LastIndexOf(",");346

347
if (ix>0 && iy>0 && iy>ix)348
{349
if (iz>0)350
{ 351
MailAddress=s1.Split(split);352
for(int i=0;i<MailAddress.Length;i++)353
{354
ix=MailAddress[i].LastIndexOf("@");355
if (MailAddress[i].Substring(ix+1)=="sina.com")356
{Send_Method="1";}357
else{Send_Method="0";}358
359
mSendMail mySendMail = new mSendMail(ToName,MailAddress[i],FromName,From,Subject,strTemplate);360
try361
{362
if (mySendMail.sendMail()== true)363
IsSuccess = 0;364
}365
catch366
{367
368
}369
370
}371
}372
else373
{374
if (s1.Substring(ix+1)=="sina.com")375
{Send_Method="1";}376
else{Send_Method="0";}377
378
mSendMail mySendMail = new mSendMail(ToName,To,FromName,From,Subject,strTemplate);379
try380
{381
if (mySendMail.sendMail()== true)382
IsSuccess = 0;383
}384
catch385
{}386
387
}388
}389
else{IsSuccess=2;}390
return IsSuccess;391

392
}393
}394

395
}396



浙公网安备 33010602011771号