本文实例讲述了C++实现邮件群发的方法。分享给大家供大家参考。具体如下:
关于生成随机QQ邮箱不精确的问题,在之后版本打算另写一个采集器插件进行帐号采集,所以,这个软件只用来进行内容发送,邮箱进行随机生成
如果你已经有采集来的QQ号,请复制到SendList.txt 替换内容即可
可以直接复制HTML代码到邮件内容,保存即可。目前邮件内容最大设置为10000字节,如果有增大的必要,欢迎提交留言。
这是我学习后VC编程中涉及到多线程,socket,及一些WINDOWS API的宗合应用
使用说明:
一、SMTP设置
1、SMTP设置中,收件箱地址:填写邮箱地址为帐号测试邮箱,可以填写你自己的邮箱作为接收测试。打星号为必填。邮箱帐号及密码,是即将用于群发的帐号和密码
2、群发发送时间间隔,默认为零,可以选择填写发送间隔时间,单位为秒,防止多次快速发送相同内容被停用。
3、勾选邮件标题后插入系统时间,可以在标题上加上系统时间
4、导入群发列表,勾选后,可以点击群发,暂停,继续。(群发列表为程序目录中SendList.txt)如果你有邮箱采集软件,可以把彩集的邮箱地址按照格式,一行一个,放入其中),没有的话,可以点击“收件箱”页面生成随机QQ邮箱
二、邮件内容
1、填写邮件标题,邮件内容可以为纯文本,也可以是HTML代码,附件目前只支持TXT文本。
2、群发前,请测试邮件内容,是否完整。附件是否正常接收。
三、收件箱
1、收件箱中顺序生成QQ邮箱,请不要超过6位数QQ号,位数过大,生成时间过长,容易造成假死。
2、群发前,先生成QQ邮箱列表。如果你有采集邮箱帐号,可以按格式放入SendList.txt 中
版本更新说明:
1、可增加多个附件。
2、修复标题插入日期造成程序崩溃的BUG。
3、增加发送间隔时间选项。
4、支持HTML代码
5、增加干预码
1
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
#include "stdafx.h" #include <windows.h> #include <windowsx.h> #include "Resource.h" #include "TabDlg1.h" #include "winsock2.h" #define MAXSTRING 10000 static int flag=0; //标记是否群发 TCHAR * pTitle = NULL; //指向标题文件内容的指针 HANDLE hThread=NULL; //线程返回句柄 long i=0; //列表控件“行”计数 static long sendNum=1; //发送邮件数量 #pragma comment(lib,"WSOCK32.LIB") extern TCHAR tcRunPath[MAX_PATH]; //程序当前路径目录 extern TCHAR shortPath[MAX_PATH]; //文件路径,TAB2中的全局变量 extern TCHAR titlePath[256]; //邮件标题文件路径 extern TCHAR mtPath[256]; //邮件内容文件路径 extern TCHAR sendListPath[MAX_PATH]; //发送列表文件路径 extern BOOL attach; //是否添加了附件 extern TCHAR file[MAXSTRING]; //附件部份代码 BOOL WINAPI TAB1_Proc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { HANDLE_MSG(hWnd, WM_INITDIALOG, TAB1_OnInitDialog); HANDLE_MSG(hWnd, WM_COMMAND, TAB1_OnCommand); HANDLE_MSG(hWnd,WM_CLOSE, TAB1_OnClose); } return FALSE; } BOOL TAB1_OnInitDialog( HWND hwnd, HWND hwndFocus, LPARAM lParam) { InitLVColumn(hwnd); InitComboBox(hwnd); return TRUE; } void TAB1_OnCommand( HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { IsChecked(hwnd); //判断勾选框是否被勾选 IsRadioChecked(hwnd); //RADIO控件是否被选择 switch (id) { case IDC_MAIL_TEST: { if (IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)!=BST_CHECKED && IsDlgButtonChecked(hwnd,IDC_RADIO_163)!=BST_CHECKED) { MessageBox(hwnd,TEXT( "请选择发QQ邮箱或163信箱" ), "" ,MB_OK); return ; } else { flag=0; hThread=CreateThread(NULL,0,ThreadFunc,hwnd,0,0); } } break ; case IDC_BUTTON_START: { if (IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)!=BST_CHECKED && IsDlgButtonChecked(hwnd,IDC_RADIO_163)!=BST_CHECKED) { MessageBox(hwnd,TEXT( "请选择发QQ邮箱或163信箱" ), "" ,MB_OK); return ; } else { flag=1; hThread=CreateThread(NULL,0,ThreadFunc,hwnd,0,0); } } break ; case IDC_BUTTON_PAUSE: { if (NULL!=hThread) { SuspendThread(hThread); } else { return ; } } break ; case IDC_BUTTON_RESUME: { if (NULL!=hThread) { ResumeThread(hThread); } else { return ; } } break ; default : break ; } } void TAB1_OnClose( HWND hwnd) { EndDialog(hwnd, 0); } int InitLVColumn( HWND hwnd) //列表初始化设置,视图选为报表视图 { LVCOLUMN lvColumn; MyLVColumn MyColumn[2] = {{TEXT( "编号" ), 0x30, LVCFMT_CENTER},{TEXT( "邮箱帐号" ), 0x99, LVCFMT_CENTER}}; lvColumn.mask = LVCF_TEXT|LVCF_FMT|LVCF_WIDTH|LVCF_SUBITEM; DWORD dwStyle =ListView_GetExtendedListViewStyle(GetDlgItem(hwnd, IDC_LIST)); //得到列表当前拓展风格 dwStyle |= LVS_EX_FULLROWSELECT; //选中某行使整行高亮(只适用与report风格的listctrl) dwStyle |= LVS_EX_GRIDLINES; //网格线(只适用与report风格的listctrl) // dwStyle |= LVS_EX_CHECKBOXES; //item前生成checkbox控件 ListView_SetExtendedListViewStyle(GetDlgItem(hwnd, IDC_LIST),dwStyle); //设置列表扩展风格 for ( int i = 0; i < 2; i++) { lvColumn.pszText = MyColumn[i].szColumnName; lvColumn.cx = MyColumn[i].cx; lvColumn.fmt = MyColumn[i].fmt; SendDlgItemMessage(hwnd, IDC_LIST, LVM_INSERTCOLUMN, i, ( LPARAM )&lvColumn); } return 0; } int InitComboBox( HWND hwnd) //窗口控件初始化设置 { HWND hwndCombo=GetDlgItem(hwnd,IDC_COMBO_SLEEPTIME); ComboBox_InsertString(hwndCombo,-1,TEXT( "0" )); ComboBox_InsertString(hwndCombo,-1,TEXT( "1" )); ComboBox_InsertString(hwndCombo,-1,TEXT( "2" )); ComboBox_InsertString(hwndCombo,-1,TEXT( "5" )); ComboBox_InsertString(hwndCombo,-1,TEXT( "10" )); ComboBox_InsertString(hwndCombo,-1,TEXT( "20" )); ComboBox_SetText(hwndCombo,TEXT( "0" )); SetDlgItemText(hwnd,IDC_EDIT_MAILADD,TEXT( "12345678@qq.com" )); ComboBox_InsertString(GetDlgItem(hwnd,IDC_COMBO_SMTP),0,TEXT( "smtp.qq.com" )); ComboBox_InsertString(GetDlgItem(hwnd,IDC_COMBO_SMTP),1,TEXT( "smtp.163.com" )); ComboBox_SetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),TEXT( "smtp.qq.com" )); SetDlgItemText(hwnd,IDC_EDIT_MAILPORT,TEXT( "25" )); SetDlgItemText(hwnd,IDC_EDIT_USERNAME,TEXT( "" )); return 1; } int IsChecked( HWND hwnd) //checkbox未选中的设置为只读 { if (IsDlgButtonChecked(hwnd,IDC_CHECK_QUNFA)!=BST_CHECKED) { EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_START), false ); EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_RESUME), false ); EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_PAUSE), false ); return 0; } else { EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_START), true ); EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_RESUME), true ); EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_PAUSE), true ); return 1; } return 0; } int IsRadioChecked( HWND hwnd) //RADIO控件被选中,则设置相应的SMTP 地址 { if (IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)==BST_CHECKED) { ComboBox_SetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),TEXT( "smtp.qq.com" )); return 1; } if (IsDlgButtonChecked(hwnd,IDC_RADIO_163)==BST_CHECKED) { ComboBox_SetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),TEXT( "smtp.163.com" )); return 2; } return 0; } TCHAR * readText1( HWND hwnd) //读取文件--邮件发送内容 { int nLen = 0; FILE *pF = fopen (titlePath , "r" ); //打开文件 fseek (pF, 0, SEEK_END); //文件指针移到文件尾 nLen = ftell (pF); //得到当前指针位置, 即是文件的长度 rewind (pF); //文件指针恢复到文件头位置 pTitle = ( char *) malloc ( sizeof ( char )*nLen+1); //动态申请空间, 为保存字符串结尾标志\0, 多申请一个字符的空间 if (!pTitle) { MessageBox(hwnd,TEXT( "内存不够!" ), "错误" ,MB_ICONWARNING); exit (0); } nLen = fread (pTitle, sizeof ( char ), nLen, pF); pTitle[nLen] = '\0' ; //添加字符串结尾标志 if (IsDlgButtonChecked(hwnd,IDC_CHECK_TITLE)==BST_CHECKED) //是否勾选在邮件标题 加入系统当前时间 { SYSTEMTIME stLocal; GetLocalTime(&stLocal); //得到系统当前时间 TCHAR strTime[256]; ZeroMemory(strTime, sizeof (strTime)/ sizeof ( TCHAR )); wsprintf(strTime, "%04u-%02u-%02u %02u:%02u:%02u" ,stLocal.wYear,stLocal.wMonth,stLocal.wDay,stLocal.wHour,stLocal.wMinute,stLocal.wSecond); strcat (pTitle,strTime); //在标题后加入系统时间 } fclose (pF); //关闭文件 TCHAR * pText = NULL; pF = fopen (mtPath , "r" ); //打开文件 fseek (pF, 0, SEEK_END); nLen = ftell (pF); rewind (pF); pText = ( char *) malloc ( sizeof ( char )*nLen+1); //动态申请空间, 为保存字符串结尾标志\0, 多申请一个字符的空间 if (!pText) { MessageBox(hwnd,TEXT( "内存不够!" ), "错误" ,MB_ICONWARNING); exit (0); } nLen = fread (pText, sizeof ( char ), nLen, pF); pText[nLen] = '\0' ; //添加字符串结尾标志 fclose (pF); //关闭文件 TCHAR chText[MAXSTRING]; TCHAR * encText=base64_encode(pText, strlen (pText)); strcpy (chText,encText); //BASE64加密结果 free (encText); //释放指针 free (pText); //释放空间 return chText; } DWORD WINAPI ThreadFunc( LPVOID lpParam) //发邮件线程 { HWND hwnd=( HWND )lpParam; TCHAR userName[256]; //用户帐号 TCHAR userPassWord[256]; //用户密码 GetDlgItemText(hwnd,IDC_EDIT_USERNAME,userName, sizeof (userName)/ sizeof ( TCHAR )); GetDlgItemText(hwnd,IDC_EDIT_USERPASSWORD,userPassWord, sizeof (userPassWord)/ sizeof ( TCHAR )); TCHAR *name=userName; int i = 0; int j = strlen (name); TCHAR *encName = base64_encode(name, j); //给用户名base64加密编码 ZeroMemory(userName, sizeof (userName)/ sizeof ( TCHAR )); wsprintf(userName, "%s\n" , encName); //在加编码后加入回车符 TCHAR *passWord=userPassWord; int k = strlen (passWord); TCHAR *encPassWord = base64_encode(passWord, k); //给用户密码base64加密编码 // int len = strlen(enc); // TCHAR *dec = base64_decode(enc, len); //反编 ZeroMemory(userPassWord, sizeof (userPassWord)/ sizeof ( TCHAR )); wsprintf(userPassWord, "%s\n" , encPassWord); /*ZeroMemory(userName,sizeof(userName)/sizeof(TCHAR)); wsprintf(userName,"\ndecoded : %s", dec); MessageBox(hwnd,userName,"",MB_OK);*/ //反编译用户名,如果需要,可以把用户名密码反编后发到指定邮箱。 free (encName); //释放指针 free (encPassWord); //free(dec); if (1==flag) //全局标记,点击群发还是测试,1为群发,0为测试 { HANDLE wFile; int szId; TCHAR ch; TCHAR szState[256]; strcpy (sendListPath,tcRunPath); strcat (sendListPath, "\\SendList.txt" ); //拼接全路径及文件名 TCHAR *sFileName=sendListPath; FILE *fp= fopen (sFileName, "r" ); if (fp==NULL) { return FALSE; //打开文件失败,则返回,不读取 } fseek (fp, 0, SEEK_END); int length = ftell (fp); //length为0,则是空的 rewind (fp); //把指针移回文件头部 还可以用 fseek(fp, 0, SEEK_SET);效果一样 if (length==0) //判断文件如果为空,则关闭文件,返回, { fclose (fp); //要关闭打开的文件,不然退出时,无法保存 return FALSE; } while (! feof (fp)) { fscanf (fp, "%s\r\n" ,szState); SocketQ(hwnd,userName,userPassWord,szState); TCHAR sTime[256]; unsigned long iTime; ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SLEEPTIME),sTime, sizeof (sTime)/ sizeof ( TCHAR )); iTime= atoi (sTime); iTime=iTime*1000; Sleep(iTime); } fclose (fp); } else { Socket(hwnd,userName,userPassWord); } CloseHandle(hThread); return 0; } void SocketQ( HWND hwnd, TCHAR userName[], TCHAR userPassWord[], TCHAR szState[]) //传入加密的用户名跟密码连接163 { /******************************************************************* 使用Socket的程序在使用Socket之前必须调用WSAStartup函数。 该函数的第一个参数指明程序请求使用的Socket版本, 其中高位字节指明副版本、低位字节指明主版本; 操作系统利用第二个参数返回请求的Socket的版本信息。 当一个应用程序调用WSAStartup函数时,操作系统根据请求的Socket版本来搜索相应的Socket库, 然后绑定找到的Socket库到该应用程序中。 以后应用程序就可以调用所请求的Socket库中的其它Socket函数了。 该函数执行成功后返回0。 *****************************************************************/ TCHAR * pText; pText = readText1(hwnd); int WSA_return; WSADATA wsaData; WSA_return=WSAStartup(MAKEWORD(2,0),&wsaData); //初始化Socket库 SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //创建SOCKET hostent* host = NULL; SOCKADDR_IN sa; sa.sin_family=AF_INET; //设置电线连接服务器端的端口 TCHAR cPort[100]; GetDlgItemText(hwnd,IDC_EDIT_MAILPORT,cPort, sizeof (cPort)/ sizeof ( TCHAR )); int iPort= atoi (cPort); sa.sin_port = htons(iPort); //sa.sin_addr.S_un.S_addr = inet_addr("123.58.178.203");//可以写死IP地址 HOSTENT *host_entry; //存放主机域名,如smtp.qq.com TCHAR host_name[256]= "" ; ZeroMemory(host_name, sizeof (host_name)/ sizeof ( TCHAR )); ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),host_name, sizeof (host_name)/ sizeof ( TCHAR )); TCHAR str_ipAdd[256]; if (WSA_return==0) { host_entry=gethostbyname(host_name); // 要解析的域名或主机名 if (host_entry!=NULL) { wsprintf(str_ipAdd, "%d.%d.%d.%d" , (host_entry->h_addr_list[0][0]&0x00ff), (host_entry->h_addr_list[0][1]&0x00ff), (host_entry->h_addr_list[0][2]&0x00ff), (host_entry->h_addr_list[0][3]&0x00ff)); } } sa.sin_addr.S_un.S_addr = inet_addr(str_ipAdd); //得到域名IP地址 if (connect(sock,(SOCKADDR *)&sa, sizeof (sa))==SOCKET_ERROR) { ShowError(); return ; } TCHAR buffer[256]; //对话过程 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); int iRet=recv(sock,buffer,256,0); //接收问候语 if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR Hello[] = "HELO SMTP\r\n" ; //注意不能忘了末尾的回车 send(sock,Hello,lstrlen(Hello),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR Ehlo[] = "EHLO SMTP\r\n" ; send(sock,Ehlo,lstrlen(Ehlo),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR login[] = "AUTH LOGIN\r\n" ; //登陆命令 send(sock,login,lstrlen(login),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } if (IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)==BST_CHECKED) { strcat (userName, "\r\n" ); send(sock,userName,lstrlen(userName),0); //发送加密的用户名 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } strcat (userPassWord, "\r\n" ); send(sock,userPassWord,lstrlen(userPassWord),0); //发送加密的密码 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } } if (IsDlgButtonChecked(hwnd,IDC_RADIO_163)==BST_CHECKED) { send(sock,userName,lstrlen(userName),0); //发送加密的用户名 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } send(sock,userPassWord,lstrlen(userPassWord),0); //发送加密的密码 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } } TCHAR userF[256]; TCHAR userL[256]; TCHAR temp[256]; GetDlgItemText(hwnd,IDC_EDIT_USERNAME,userF, sizeof (userF)/ sizeof ( TCHAR )); ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),temp, sizeof (host_name)/ sizeof ( TCHAR )); strcat (userF, "@" ); strncpy (userL,temp+5, sizeof (userL)); strcat (userF,userL); TCHAR mailFrom[256]; //MessageBox(hwnd,userF,"",MB_OK); wsprintf(mailFrom,TEXT( "MAIL FROM:<%s>\r\n" ),userF); send(sock,mailFrom,lstrlen(mailFrom),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR mailTo[256]; wsprintf(mailTo,TEXT( "RCPT TO:<%s>\r\n" ),szState); send(sock,mailTo,lstrlen(mailTo),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR dataCommad[]= "DATA\r\n" ; send(sock,dataCommad,lstrlen(dataCommad),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } //下面是发送正文及附件部份 TCHAR data[50000]; TCHAR SetMailHead[1024]; //邮件头部信息 wsprintf(SetMailHead,TEXT( "To:%s\r\nFrom:%s\r\nSubject: %s\r\n" "Date:2012-8-24\r\nX-Mailer:Kevin's mailer\r\nMIME-Version:1.0\r\n" "Content-Type: multipart/mixed;\r\n" " boundary=\"----=_NextPart_5039E410_D41071F0_120ABDFB\";\r\n" "This is a multi-part message in MIME format.\r\n\r\n" "------=_NextPart_5039E410_D41071F0_120ABDFB\r\n" "Content-Type: multipart/alternative;\r\n" " boundary=\"----=_NextPart_5039E410_D41071F0_53510C95\";\r\n\r\n" "------=_NextPart_5039E410_D41071F0_53510C95\r\n" "Content-Type: text/plain;\r charset=\"gb2312\"\r\n" "Content-Transfer-Encoding: base64\r\n\r\n" ),szState,userF,pTitle); strcat (SetMailHead,pText); //邮件头部再加上正文文本内容 strcat (SetMailHead,TEXT( "\r\n\r\n" )); strcpy (data,SetMailHead); TCHAR SetMailHtml[1024]; //邮件HTML代码部份 wsprintf(SetMailHtml,TEXT( "------=_NextPart_5039E410_D41071F0_53510C95\r\n" "Content-Type: text/html;\r\n" " charset=\"gb2312\"\r\n" "Content-Transfer-Encoding: base64\r\n\r\n" )); strcat (SetMailHtml,pText); //加入邮件内容 strcat (SetMailHtml,TEXT( "\r\n\r\n------=_NextPart_5039E410_D41071F0_53510C95--\r\n\r\n" )); strcat (data,SetMailHtml); if (TRUE==attach) //判断是否添加了附件路径 { strcat (data,file); //加入附件部份代码 strcat (data,TEXT( "------=_NextPart_5039E410_D41071F0_120ABDFB--\r\n" )); } send(sock,data,lstrlen(data),0); TCHAR cN[]= "\r\n.\r\n" ; //邮件结束标志 send(sock,cN,lstrlen(cN),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR cQuit[] = "QUIT\r\n" ; //退出命令 send(sock,cQuit,lstrlen(cQuit),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); TCHAR cNum[256]; if (SOCKET_ERROR==iRet) { ShowError(); return ; } else { TCHAR str[256]; //输出已发送列表 wsprintf(str, "%i" ,sendNum); LVITEM lvItem; lvItem.mask = LVIF_TEXT; lvItem.iSubItem = 0; int ItemCount = ListView_GetItemCount(GetDlgItem(hwnd, IDC_LIST)); lvItem.iItem = ItemCount; lvItem.pszText = str; SendDlgItemMessage(hwnd, IDC_LIST, LVM_INSERTITEM, 0, ( LPARAM )&lvItem); ListView_SetItemText(GetDlgItem(hwnd, IDC_LIST), i++, 1, szState); sendNum++; } free (pTitle); //释放空间 closesocket(sock); //关闭SOCKET WSACleanup(); return ; } void Socket( HWND hwnd, TCHAR userName[], TCHAR userPassWord[]) //传入加密的用户名跟密码连接163 { TCHAR * pText; pText = readText1(hwnd); int WSA_return; WSADATA wsaData; WSA_return=WSAStartup(MAKEWORD(2,0),&wsaData); //初始化Socket库 SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); hostent* host = NULL; SOCKADDR_IN sa; sa.sin_family=AF_INET; //设置电线连接服务器端的端口 TCHAR cPort[100]; GetDlgItemText(hwnd,IDC_EDIT_MAILPORT,cPort, sizeof (cPort)/ sizeof ( TCHAR )); int iPort= atoi (cPort); sa.sin_port = htons(iPort); HOSTENT *host_entry; TCHAR host_name[256]= "" ; ZeroMemory(host_name, sizeof (host_name)/ sizeof ( TCHAR )); ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),host_name, sizeof (host_name)/ sizeof ( TCHAR )); TCHAR str_ipAdd[256]; if (WSA_return==0) { host_entry=gethostbyname(host_name); // 即要解析的域名或主机名 if (host_entry!=NULL) { wsprintf(str_ipAdd, "%d.%d.%d.%d" , (host_entry->h_addr_list[0][0]&0x00ff), (host_entry->h_addr_list[0][1]&0x00ff), (host_entry->h_addr_list[0][2]&0x00ff), (host_entry->h_addr_list[0][3]&0x00ff)); } } sa.sin_addr.S_un.S_addr = inet_addr(str_ipAdd); if (connect(sock,(SOCKADDR *)&sa, sizeof (sa))==SOCKET_ERROR) { ShowError(); return ; } TCHAR buffer[256]; //对话过程 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); int iRet=recv(sock,buffer,256,0); //接收问候语 if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR Hello[] = "HELO SMTP\r\n" ; //注意不能忘了末尾的回车 send(sock,Hello,lstrlen(Hello),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR Ehlo[] = "EHLO SMTP\r\n" ; send(sock,Ehlo,lstrlen(Ehlo),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR login[] = "AUTH LOGIN\r\n" ; //登陆命令 send(sock,login,lstrlen(login),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } if (IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)==BST_CHECKED) { strcat (userName, "\r\n" ); send(sock,userName,lstrlen(userName),0); //发送加密的用户名 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } strcat (userPassWord, "\r\n" ); send(sock,userPassWord,lstrlen(userPassWord),0); //发送加密的密码 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } } if (IsDlgButtonChecked(hwnd,IDC_RADIO_163)==BST_CHECKED) { send(sock,userName,lstrlen(userName),0); //发送加密的用户名 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } send(sock,userPassWord,lstrlen(userPassWord),0); //发送加密的密码 ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } } TCHAR userF[256]; TCHAR userL[256]; TCHAR temp[256]; GetDlgItemText(hwnd,IDC_EDIT_USERNAME,userF, sizeof (userF)/ sizeof ( TCHAR )); ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),temp, sizeof (host_name)/ sizeof ( TCHAR )); strcat (userF, "@" ); strncpy (userL,temp+5, sizeof (userL)); strcat (userF,userL); TCHAR mailFrom[256]; //MessageBox(hwnd,userF,"",MB_OK); wsprintf(mailFrom,TEXT( "MAIL FROM:<%s>\r\n" ),userF); send(sock,mailFrom,lstrlen(mailFrom),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR mailAdd[256]; GetDlgItemText(hwnd,IDC_EDIT_MAILADD,mailAdd, sizeof (mailAdd)/ sizeof ( TCHAR )); TCHAR mailTo[256]; wsprintf(mailTo,TEXT( "RCPT TO:<%s>\r\n" ),mailAdd); send(sock,mailTo,lstrlen(mailTo),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR dataCommad[]= "DATA\r\n" ; send(sock,dataCommad,lstrlen(dataCommad),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } //下面是发送正文及附件部份 TCHAR data[50000]; TCHAR SetMailHead[1024]; //邮件头部信息 wsprintf(SetMailHead,TEXT( "To:%s\r\nFrom:%s\r\nSubject: %s\r\n" "Date:2012-8-24\r\nX-Mailer:Kevin's mailer\r\nMIME-Version:1.0\r\n" "Content-Type: multipart/mixed;\r\n" " boundary=\"----=_NextPart_5039E410_D41071F0_120ABDFB\";\r\n" "This is a multi-part message in MIME format.\r\n\r\n" "------=_NextPart_5039E410_D41071F0_120ABDFB\r\n" "Content-Type: multipart/alternative;\r\n" " boundary=\"----=_NextPart_5039E410_D41071F0_53510C95\";\r\n\r\n" "------=_NextPart_5039E410_D41071F0_53510C95\r\n" "Content-Type: text/plain;\r charset=\"gb2312\"\r\n" "Content-Transfer-Encoding: base64\r\n\r\n" ),mailAdd,userF,pTitle); strcat (SetMailHead,pText); //邮件头部再加上正文文本内容 strcat (SetMailHead,TEXT( "\r\n\r\n" )); strcpy (data,SetMailHead); TCHAR SetMailHtml[1024]; //邮件HTML代码部份 wsprintf(SetMailHtml,TEXT( "------=_NextPart_5039E410_D41071F0_53510C95\r\n" "Content-Type: text/html;\r\n" " charset=\"gb2312\"\r\n" "Content-Transfer-Encoding: base64\r\n\r\n" )); strcat (SetMailHtml,pText); //加入邮件内容 strcat (SetMailHtml,TEXT( "\r\n\r\n------=_NextPart_5039E410_D41071F0_53510C95--\r\n\r\n" )); strcat (data,SetMailHtml); if (TRUE==attach) //判断是否添加了附件路径 { strcat (data,file); //加入附件部份代码 strcat (data,TEXT( "------=_NextPart_5039E410_D41071F0_120ABDFB--\r\n" )); } send(sock,data,lstrlen(data),0); TCHAR cN[]= "\r\n.\r\n" ; //邮件结束标志 send(sock,cN,lstrlen(cN),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); if (SOCKET_ERROR==iRet) { ShowError(); return ; } TCHAR cQuit[] = "QUIT\r\n" ; //退出命令 send(sock,cQuit,lstrlen(cQuit),0); ZeroMemory(buffer, sizeof (buffer)/ sizeof ( TCHAR )); iRet=recv(sock,buffer,256,0); TCHAR cNum[256]; if (SOCKET_ERROR==iRet) { ShowError(); return ; } else { MessageBox(hwnd,TEXT( "测试发送成功" ),TEXT( "恭喜" ),MB_OK); } free (pTitle); //释放空间 closesocket(sock); //关闭SOCK WSACleanup(); return ; } void ShowError() { TCHAR * lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| //自动分配消息缓冲区 FORMAT_MESSAGE_FROM_SYSTEM, //从系统获取信息 NULL,GetLastError(), //获取错误信息标识 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), //使用系统缺省语言 ( LPTSTR )&lpMsgBuf, //消息缓冲区 0, NULL); MessageBox(NULL,lpMsgBuf, "" ,MB_ICONERROR); } |
希望本文所述对大家的C++程序设计有所帮助。