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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396
