码迷,mamicode.com
首页 > 编程语言 > 详细

C++实现邮件群发的方法

时间:2016-06-22 23:36:09      阅读:709      评论:0      收藏:0      [点我收藏+]

标签:

这篇文章主要介绍了C++实现邮件群发的方法,较为详细的分析了邮件发送的原理与C++相关实现技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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 #include "stdafx.h"
  2 #include <windows.h>
  3 #include <windowsx.h>
  4 #include "Resource.h"
  5 #include "TabDlg1.h"
  6 #include "winsock2.h"
  7 #define MAXSTRING 10000
  8 static int flag=0;     //标记是否群发
  9 TCHAR* pTitle = NULL;     //指向标题文件内容的指针
 10 HANDLE hThread=NULL;     //线程返回句柄
 11 long i=0;        //列表控件“行”计数
 12 static long sendNum=1;    //发送邮件数量
 13 #pragma comment(lib,"WSOCK32.LIB")
 14 extern TCHAR tcRunPath[MAX_PATH];  //程序当前路径目录
 15 extern TCHAR shortPath[MAX_PATH];  //文件路径,TAB2中的全局变量
 16 extern TCHAR titlePath[256];   //邮件标题文件路径
 17 extern TCHAR mtPath[256];    //邮件内容文件路径
 18 extern TCHAR sendListPath[MAX_PATH]; //发送列表文件路径
 19 extern BOOL attach;     //是否添加了附件
 20 extern TCHAR file[MAXSTRING];   //附件部份代码
 21 BOOL WINAPI TAB1_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 22 {
 23  switch(uMsg)
 24  {
 25   HANDLE_MSG(hWnd, WM_INITDIALOG, TAB1_OnInitDialog);
 26   HANDLE_MSG(hWnd, WM_COMMAND, TAB1_OnCommand);
 27   HANDLE_MSG(hWnd,WM_CLOSE, TAB1_OnClose);
 28  }
 29  return FALSE;
 30 }
 31 BOOL TAB1_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
 32 {
 33  InitLVColumn(hwnd);
 34  InitComboBox(hwnd);
 35  return TRUE;
 36 }
 37 void TAB1_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
 38 { 
 39  IsChecked(hwnd);   //判断勾选框是否被勾选
 40  IsRadioChecked(hwnd); //RADIO控件是否被选择
 41  switch(id)
 42  {
 43   case IDC_MAIL_TEST:
 44   {  
 45    if(IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)!=BST_CHECKED && IsDlgButtonChecked(hwnd,IDC_RADIO_163)!=BST_CHECKED)
 46    {
 47     MessageBox(hwnd,TEXT("请选择发QQ邮箱或163信箱"),"",MB_OK);
 48     return;
 49    }
 50    else
 51    {
 52     flag=0;
 53     hThread=CreateThread(NULL,0,ThreadFunc,hwnd,0,0);
 54    }
 55   }
 56   break;
 57   case IDC_BUTTON_START:
 58   {  
 59    if(IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)!=BST_CHECKED && IsDlgButtonChecked(hwnd,IDC_RADIO_163)!=BST_CHECKED)
 60    {
 61     MessageBox(hwnd,TEXT("请选择发QQ邮箱或163信箱"),"",MB_OK);
 62     return;
 63    }
 64    else
 65    {
 66     flag=1;
 67     hThread=CreateThread(NULL,0,ThreadFunc,hwnd,0,0);
 68    }
 69   }
 70   break;
 71   case IDC_BUTTON_PAUSE:
 72   {   
 73    if(NULL!=hThread)
 74    {
 75     SuspendThread(hThread); 
 76    }
 77    else
 78    {
 79     return;
 80    }
 81   }
 82   break;
 83   case IDC_BUTTON_RESUME:
 84   {   
 85    if(NULL!=hThread)
 86    {
 87     ResumeThread(hThread); 
 88    }
 89    else
 90    {
 91     return;
 92    }
 93   }
 94   break;
 95   default:
 96   break;
 97  }
 98 }
 99 void TAB1_OnClose(HWND hwnd)
100 {
101  EndDialog(hwnd, 0);
102 }
103 int InitLVColumn(HWND hwnd)    //列表初始化设置,视图选为报表视图
104 {
105  LVCOLUMN lvColumn;
106  MyLVColumn MyColumn[2] = {{TEXT("编号"), 0x30, LVCFMT_CENTER},{TEXT("邮箱帐号"), 0x99, LVCFMT_CENTER}};
107  lvColumn.mask = LVCF_TEXT|LVCF_FMT|LVCF_WIDTH|LVCF_SUBITEM;
108  DWORD dwStyle =ListView_GetExtendedListViewStyle(GetDlgItem(hwnd, IDC_LIST));   //得到列表当前拓展风格
109  dwStyle |= LVS_EX_FULLROWSELECT; //选中某行使整行高亮(只适用与report风格的listctrl)
110  dwStyle |= LVS_EX_GRIDLINES;  //网格线(只适用与report风格的listctrl)
111 // dwStyle |= LVS_EX_CHECKBOXES;  //item前生成checkbox控件
112  ListView_SetExtendedListViewStyle(GetDlgItem(hwnd, IDC_LIST),dwStyle);     //设置列表扩展风格
113  for(int i = 0; i < 2; i++)
114  {
115   lvColumn.pszText = MyColumn[i].szColumnName;
116   lvColumn.cx = MyColumn[i].cx;
117   lvColumn.fmt = MyColumn[i].fmt;
118   SendDlgItemMessage(hwnd, IDC_LIST, LVM_INSERTCOLUMN, i, (LPARAM)&lvColumn);
119  }
120   
121  return 0;
122 }
123 int InitComboBox(HWND hwnd)//窗口控件初始化设置
124 {
125  HWND hwndCombo=GetDlgItem(hwnd,IDC_COMBO_SLEEPTIME);
126  ComboBox_InsertString(hwndCombo,-1,TEXT("0"));
127  ComboBox_InsertString(hwndCombo,-1,TEXT("1"));
128  ComboBox_InsertString(hwndCombo,-1,TEXT("2"));
129  ComboBox_InsertString(hwndCombo,-1,TEXT("5"));
130  ComboBox_InsertString(hwndCombo,-1,TEXT("10"));
131  ComboBox_InsertString(hwndCombo,-1,TEXT("20"));
132  ComboBox_SetText(hwndCombo,TEXT("0"));
133  SetDlgItemText(hwnd,IDC_EDIT_MAILADD,TEXT("12345678@qq.com"));
134  ComboBox_InsertString(GetDlgItem(hwnd,IDC_COMBO_SMTP),0,TEXT("smtp.qq.com"));
135  ComboBox_InsertString(GetDlgItem(hwnd,IDC_COMBO_SMTP),1,TEXT("smtp.163.com"));
136  ComboBox_SetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),TEXT("smtp.qq.com"));
137  SetDlgItemText(hwnd,IDC_EDIT_MAILPORT,TEXT("25")); 
138  SetDlgItemText(hwnd,IDC_EDIT_USERNAME,TEXT(""));
139   
140  return 1;
141 }
142 int IsChecked(HWND hwnd)  //checkbox未选中的设置为只读
143 {
144  if(IsDlgButtonChecked(hwnd,IDC_CHECK_QUNFA)!=BST_CHECKED)
145  {
146   EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_START), false);
147   EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_RESUME), false);
148   EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_PAUSE), false);
149   return 0;
150  }
151  else
152  {
153   EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_START), true);
154   EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_RESUME), true);
155   EnableWindow(GetDlgItem(hwnd, IDC_BUTTON_PAUSE), true);
156   return 1;
157  }
158  return 0;
159 }
160 int IsRadioChecked(HWND hwnd) //RADIO控件被选中,则设置相应的SMTP 地址
161 {
162  if(IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)==BST_CHECKED)
163  {
164   ComboBox_SetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),TEXT("smtp.qq.com"));
165   return 1;
166  }
167  if(IsDlgButtonChecked(hwnd,IDC_RADIO_163)==BST_CHECKED)
168  {
169   ComboBox_SetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),TEXT("smtp.163.com"));
170   return 2;
171  }
172  return 0;
173 }
174 TCHAR* readText1(HWND hwnd)    //读取文件--邮件发送内容
175 {
176  int nLen = 0;
177  FILE *pF = fopen(titlePath , "r"); //打开文件
178  fseek(pF, 0, SEEK_END);    //文件指针移到文件尾
179  nLen = ftell(pF);     //得到当前指针位置, 即是文件的长度
180  rewind(pF);       //文件指针恢复到文件头位置
181  pTitle = (char*) malloc(sizeof(char)*nLen+1);//动态申请空间, 为保存字符串结尾标志\0, 多申请一个字符的空间
182  if(!pTitle)
183  {
184   MessageBox(hwnd,TEXT("内存不够!"),"错误",MB_ICONWARNING);
185   exit(0);
186  }
187  nLen = fread(pTitle, sizeof(char), nLen, pF);
188  pTitle[nLen] = \0;   //添加字符串结尾标志
189  if(IsDlgButtonChecked(hwnd,IDC_CHECK_TITLE)==BST_CHECKED) //是否勾选在邮件标题 加入系统当前时间
190  {
191   SYSTEMTIME stLocal;
192   GetLocalTime(&stLocal); //得到系统当前时间
193   TCHAR strTime[256];
194   ZeroMemory(strTime,sizeof(strTime)/sizeof(TCHAR));
195   wsprintf(strTime,"%04u-%02u-%02u %02u:%02u:%02u",stLocal.wYear,stLocal.wMonth,stLocal.wDay,stLocal.wHour,stLocal.wMinute,stLocal.wSecond);
196   strcat(pTitle,strTime); //在标题后加入系统时间
197  }
198  fclose(pF);     //关闭文件
199  TCHAR* pText = NULL;
200  pF = fopen(mtPath , "r"); //打开文件
201  fseek(pF, 0, SEEK_END); 
202  nLen = ftell(pF); 
203  rewind(pF); 
204  pText = (char*) malloc(sizeof(char)*nLen+1); //动态申请空间, 为保存字符串结尾标志\0, 多申请一个字符的空间
205  if(!pText)
206  {
207   MessageBox(hwnd,TEXT("内存不够!"),"错误",MB_ICONWARNING);
208   exit(0);
209  }
210  nLen = fread(pText, sizeof(char), nLen, pF);
211  pText[nLen] = \0;   //添加字符串结尾标志
212  fclose(pF);     //关闭文件
213  TCHAR chText[MAXSTRING];  
214  TCHAR* encText=base64_encode(pText,strlen(pText));  
215  strcpy(chText,encText);  //BASE64加密结果
216  free(encText);    //释放指针
217  free(pText);     //释放空间
218  return chText;
219 }
220 DWORD WINAPI ThreadFunc(LPVOID lpParam)//发邮件线程
221 {
222  HWND hwnd=(HWND)lpParam;
223  TCHAR userName[256];    //用户帐号
224  TCHAR userPassWord[256];   //用户密码
225  GetDlgItemText(hwnd,IDC_EDIT_USERNAME,userName,sizeof(userName)/sizeof(TCHAR));
226  GetDlgItemText(hwnd,IDC_EDIT_USERPASSWORD,userPassWord,sizeof(userPassWord)/sizeof(TCHAR));
227  TCHAR *name=userName; 
228  int i = 0; 
229  int j = strlen(name); 
230  TCHAR *encName = base64_encode(name, j);    //给用户名base64加密编码  
231  ZeroMemory(userName,sizeof(userName)/sizeof(TCHAR)); 
232  wsprintf(userName,"%s\n", encName);      //在加编码后加入回车符
233  TCHAR *passWord=userPassWord; 
234  int k = strlen(passWord); 
235  TCHAR *encPassWord = base64_encode(passWord, k);  //给用户密码base64加密编码    
236  // int len = strlen(enc); 
237  // TCHAR *dec = base64_decode(enc, len);    //反编 
238  ZeroMemory(userPassWord,sizeof(userPassWord)/sizeof(TCHAR)); 
239  wsprintf(userPassWord,"%s\n", encPassWord);
240  /*ZeroMemory(userName,sizeof(userName)/sizeof(TCHAR)); 
241  wsprintf(userName,"\ndecoded : %s", dec);
242  MessageBox(hwnd,userName,"",MB_OK);*/     //反编译用户名,如果需要,可以把用户名密码反编后发到指定邮箱。
243  free(encName);           //释放指针
244  free(encPassWord);
245  //free(dec); 
246  if(1==flag)            //全局标记,点击群发还是测试,1为群发,0为测试
247  {
248    HANDLE wFile;
249    int szId;
250    TCHAR ch;
251    TCHAR szState[256];
252    strcpy(sendListPath,tcRunPath);
253    strcat(sendListPath,"\\SendList.txt");   //拼接全路径及文件名
254    TCHAR *sFileName=sendListPath;
255    FILE *fp=fopen(sFileName,"r");
256    if(fp==NULL)
257    {
258     return FALSE;        //打开文件失败,则返回,不读取
259    }
260    fseek(fp, 0, SEEK_END);
261    int length = ftell(fp);      //length为0,则是空的
262    rewind(fp);         //把指针移回文件头部 还可以用 fseek(fp, 0, SEEK_SET);效果一样
263    if(length==0)         //判断文件如果为空,则关闭文件,返回,
264    {
265     fclose(fp);        //要关闭打开的文件,不然退出时,无法保存
266     return FALSE;
267    }
268    while(!feof(fp))
269    { 
270     fscanf(fp,"%s\r\n",szState);
271     SocketQ(hwnd,userName,userPassWord,szState);
272     TCHAR sTime[256];    
273     unsigned long iTime;
274     ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SLEEPTIME),sTime,sizeof(sTime)/sizeof(TCHAR));
275     iTime=atoi(sTime);
276     iTime=iTime*1000;
277     Sleep(iTime);
278    } 
279    fclose(fp);
280  }
281  else
282  {
283   Socket(hwnd,userName,userPassWord); 
284  }
285  CloseHandle(hThread); 
286  return 0;
287 }
288 void SocketQ(HWND hwnd,TCHAR userName[],TCHAR userPassWord[],TCHAR szState[])//传入加密的用户名跟密码连接163
289 {
290    /*******************************************************************
291    使用Socket的程序在使用Socket之前必须调用WSAStartup函数。
292    该函数的第一个参数指明程序请求使用的Socket版本,
293    其中高位字节指明副版本、低位字节指明主版本;
294    操作系统利用第二个参数返回请求的Socket的版本信息。
295    当一个应用程序调用WSAStartup函数时,操作系统根据请求的Socket版本来搜索相应的Socket库,
296    然后绑定找到的Socket库到该应用程序中。
297    以后应用程序就可以调用所请求的Socket库中的其它Socket函数了。
298    该函数执行成功后返回0。
299    *****************************************************************/
300   TCHAR* pText;
301   pText = readText1(hwnd);
302   int WSA_return;
303   WSADATA wsaData;   
304   WSA_return=WSAStartup(MAKEWORD(2,0),&wsaData);   //初始化Socket库  
305   SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//创建SOCKET
306   hostent* host = NULL; 
307   SOCKADDR_IN sa;
308   sa.sin_family=AF_INET;         //设置电线连接服务器端的端口
309   TCHAR cPort[100];
310   GetDlgItemText(hwnd,IDC_EDIT_MAILPORT,cPort,sizeof(cPort)/sizeof(TCHAR));
311   int iPort=atoi(cPort);
312   sa.sin_port = htons(iPort);   
313   //sa.sin_addr.S_un.S_addr = inet_addr("123.58.178.203");//可以写死IP地址   
314    HOSTENT *host_entry;         //存放主机域名,如smtp.qq.com
315    TCHAR host_name[256]="";
316    ZeroMemory(host_name,sizeof(host_name)/sizeof(TCHAR)); 
317    ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),host_name,sizeof(host_name)/sizeof(TCHAR));
318    TCHAR str_ipAdd[256];
319    if(WSA_return==0)
320    {
321    host_entry=gethostbyname(host_name);     // 要解析的域名或主机名 
322    if(host_entry!=NULL)
323    {
324    wsprintf(str_ipAdd,"%d.%d.%d.%d",
325     (host_entry->h_addr_list[0][0]&0x00ff),
326     (host_entry->h_addr_list[0][1]&0x00ff),
327     (host_entry->h_addr_list[0][2]&0x00ff),
328     (host_entry->h_addr_list[0][3]&0x00ff));
329      
330    }
331    }
332   sa.sin_addr.S_un.S_addr = inet_addr(str_ipAdd);   //得到域名IP地址
333   if(connect(sock,(SOCKADDR *)&sa,sizeof(sa))==SOCKET_ERROR)
334   {
335   ShowError();
336   return;
337   }
338   TCHAR buffer[256];          //对话过程  
339   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR));
340   int iRet=recv(sock,buffer,256,0);      //接收问候语  
341   if(SOCKET_ERROR==iRet)
342   {
343    ShowError();   
344    return;
345   }
346   TCHAR Hello[] = "HELO SMTP\r\n";      //注意不能忘了末尾的回车  
347   send(sock,Hello,lstrlen(Hello),0); 
348   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
349   iRet=recv(sock,buffer,256,0);
350   if(SOCKET_ERROR==iRet)
351   {
352    ShowError();   
353    return;
354   }
355   TCHAR Ehlo[] = "EHLO SMTP\r\n";  
356   send(sock,Ehlo,lstrlen(Ehlo),0); 
357   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
358   iRet=recv(sock,buffer,256,0);
359   if(SOCKET_ERROR==iRet)
360   {
361    ShowError();   
362    return;
363   }
364   TCHAR login[] = "AUTH LOGIN\r\n";      //登陆命令
365   send(sock,login,lstrlen(login),0); 
366   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
367   iRet=recv(sock,buffer,256,0);
368   if(SOCKET_ERROR==iRet)
369   {
370    ShowError();   
371    return;
372   }
373   if(IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)==BST_CHECKED)
374   {
375    strcat(userName,"\r\n");
376    send(sock,userName,lstrlen(userName),0);   //发送加密的用户名
377    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
378    iRet=recv(sock,buffer,256,0);
379    if(SOCKET_ERROR==iRet)
380    {
381     ShowError();   
382     return;
383    }
384    strcat(userPassWord,"\r\n");
385    send(sock,userPassWord,lstrlen(userPassWord),0); //发送加密的密码
386    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
387    iRet=recv(sock,buffer,256,0);
388    if(SOCKET_ERROR==iRet)
389    {
390     ShowError();   
391     return;
392    }
393   }
394   if(IsDlgButtonChecked(hwnd,IDC_RADIO_163)==BST_CHECKED)
395   {
396    send(sock,userName,lstrlen(userName),0);   //发送加密的用户名
397    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
398    iRet=recv(sock,buffer,256,0);
399    if(SOCKET_ERROR==iRet)
400    {
401     ShowError();   
402     return;
403    }
404    send(sock,userPassWord,lstrlen(userPassWord),0); //发送加密的密码
405    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
406    iRet=recv(sock,buffer,256,0);
407    if(SOCKET_ERROR==iRet)
408    {
409     ShowError();   
410     return;
411    }
412   }
413   TCHAR userF[256];
414   TCHAR userL[256];
415   TCHAR temp[256];
416   GetDlgItemText(hwnd,IDC_EDIT_USERNAME,userF,sizeof(userF)/sizeof(TCHAR)); 
417   ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),temp,sizeof(host_name)/sizeof(TCHAR));
418   strcat(userF,"@");
419   strncpy(userL,temp+5,sizeof(userL));
420   strcat(userF,userL);
421   TCHAR mailFrom[256];
422   //MessageBox(hwnd,userF,"",MB_OK);
423   wsprintf(mailFrom,TEXT("MAIL FROM:<%s>\r\n"),userF);
424   send(sock,mailFrom,lstrlen(mailFrom),0);
425   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
426   iRet=recv(sock,buffer,256,0);
427   if(SOCKET_ERROR==iRet)
428   {
429    ShowError();   
430    return;
431   }
432    TCHAR mailTo[256];
433    wsprintf(mailTo,TEXT("RCPT TO:<%s>\r\n"),szState);
434    send(sock,mailTo,lstrlen(mailTo),0);
435    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
436    iRet=recv(sock,buffer,256,0);
437    if(SOCKET_ERROR==iRet)
438    {
439     ShowError();   
440     return;
441    }
442   TCHAR dataCommad[]="DATA\r\n";
443   send(sock,dataCommad,lstrlen(dataCommad),0); 
444   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
445   iRet=recv(sock,buffer,256,0);
446   if(SOCKET_ERROR==iRet)
447   {
448    ShowError();   
449    return;
450   }
451   //下面是发送正文及附件部份
452   TCHAR data[50000];
453   TCHAR SetMailHead[1024];      //邮件头部信息
454   wsprintf(SetMailHead,TEXT("To:%s\r\nFrom:%s\r\nSubject: %s\r\n"
455          "Date:2012-8-24\r\nX-Mailer:Kevin‘s mailer\r\nMIME-Version:1.0\r\n"
456          "Content-Type: multipart/mixed;\r\n"
457          " boundary=\"----=_NextPart_5039E410_D41071F0_120ABDFB\";\r\n"
458          "This is a multi-part message in MIME format.\r\n\r\n"
459          "------=_NextPart_5039E410_D41071F0_120ABDFB\r\n"
460          "Content-Type: multipart/alternative;\r\n"
461          " boundary=\"----=_NextPart_5039E410_D41071F0_53510C95\";\r\n\r\n"
462          "------=_NextPart_5039E410_D41071F0_53510C95\r\n"
463          "Content-Type: text/plain;\r charset=\"gb2312\"\r\n"
464          "Content-Transfer-Encoding: base64\r\n\r\n"),szState,userF,pTitle);
465   strcat(SetMailHead,pText);     //邮件头部再加上正文文本内容
466   strcat(SetMailHead,TEXT("\r\n\r\n"));
467   strcpy(data,SetMailHead);
468   TCHAR SetMailHtml[1024];     //邮件HTML代码部份
469   wsprintf(SetMailHtml,TEXT("------=_NextPart_5039E410_D41071F0_53510C95\r\n"
470          "Content-Type: text/html;\r\n"
471          " charset=\"gb2312\"\r\n"
472          "Content-Transfer-Encoding: base64\r\n\r\n"));
473   strcat(SetMailHtml,pText);     //加入邮件内容
474   strcat(SetMailHtml,TEXT("\r\n\r\n------=_NextPart_5039E410_D41071F0_53510C95--\r\n\r\n"));
475   strcat(data,SetMailHtml);
476   if(TRUE==attach)       //判断是否添加了附件路径
477   {
478    strcat(data,file);      //加入附件部份代码
479    strcat(data,TEXT("------=_NextPart_5039E410_D41071F0_120ABDFB--\r\n"));
480   }
481   send(sock,data,lstrlen(data),0);
482   TCHAR cN[]="\r\n.\r\n";     //邮件结束标志
483   send(sock,cN,lstrlen(cN),0);
484   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
485   iRet=recv(sock,buffer,256,0);
486    if(SOCKET_ERROR==iRet)
487    {
488     ShowError();   
489     return;
490    }
491   TCHAR cQuit[] = "QUIT\r\n";     //退出命令
492   send(sock,cQuit,lstrlen(cQuit),0); 
493   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
494   iRet=recv(sock,buffer,256,0);
495   TCHAR cNum[256];
496   if(SOCKET_ERROR==iRet)
497   {
498    ShowError();   
499    return;
500   }  
501   else
502   { 
503    TCHAR str[256];      //输出已发送列表
504    wsprintf(str,"%i",sendNum);
505    LVITEM lvItem;
506    lvItem.mask = LVIF_TEXT;
507    lvItem.iSubItem = 0;
508    int ItemCount = ListView_GetItemCount(GetDlgItem(hwnd, IDC_LIST));
509    lvItem.iItem = ItemCount; 
510    lvItem.pszText = str;
511    SendDlgItemMessage(hwnd, IDC_LIST, LVM_INSERTITEM, 0, (LPARAM)&lvItem); 
512    ListView_SetItemText(GetDlgItem(hwnd, IDC_LIST), i++, 1, szState);       
513    sendNum++;
514   }
515   free(pTitle);        //释放空间  
516   closesocket(sock);       //关闭SOCKET 
517   WSACleanup();
518   return;
519 }
520 void Socket(HWND hwnd,TCHAR userName[],TCHAR userPassWord[])//传入加密的用户名跟密码连接163
521 {
522   TCHAR* pText;
523   pText = readText1(hwnd);
524   int WSA_return;
525   WSADATA wsaData;   
526   WSA_return=WSAStartup(MAKEWORD(2,0),&wsaData);//初始化Socket库
527   SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
528   hostent* host = NULL; 
529   SOCKADDR_IN sa;
530   sa.sin_family=AF_INET;      //设置电线连接服务器端的端口
531   TCHAR cPort[100];
532   GetDlgItemText(hwnd,IDC_EDIT_MAILPORT,cPort,sizeof(cPort)/sizeof(TCHAR));
533   int iPort=atoi(cPort);
534   sa.sin_port = htons(iPort);  
535    HOSTENT *host_entry;
536    TCHAR host_name[256]="";
537    ZeroMemory(host_name,sizeof(host_name)/sizeof(TCHAR)); 
538    ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),host_name,sizeof(host_name)/sizeof(TCHAR));
539    TCHAR str_ipAdd[256];
540    if(WSA_return==0)
541    {   
542    host_entry=gethostbyname(host_name); // 即要解析的域名或主机名 
543     if(host_entry!=NULL)
544     {
545      wsprintf(str_ipAdd,"%d.%d.%d.%d",
546     (host_entry->h_addr_list[0][0]&0x00ff),
547     (host_entry->h_addr_list[0][1]&0x00ff),
548     (host_entry->h_addr_list[0][2]&0x00ff),
549     (host_entry->h_addr_list[0][3]&0x00ff));
550       
551     }
552    }
553   sa.sin_addr.S_un.S_addr = inet_addr(str_ipAdd);  
554   if(connect(sock,(SOCKADDR *)&sa,sizeof(sa))==SOCKET_ERROR)
555   {
556    ShowError();
557    return; 
558   }
559   TCHAR buffer[256];      //对话过程 
560   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR));  
561   int iRet=recv(sock,buffer,256,0);  //接收问候语 
562   if(SOCKET_ERROR==iRet)
563   {
564    ShowError();   
565    return;
566   }
567   TCHAR Hello[] = "HELO SMTP\r\n";  //注意不能忘了末尾的回车 
568   send(sock,Hello,lstrlen(Hello),0); 
569   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
570   iRet=recv(sock,buffer,256,0);
571   if(SOCKET_ERROR==iRet)
572   {
573    ShowError();   
574    return;
575   }
576   TCHAR Ehlo[] = "EHLO SMTP\r\n";  
577   send(sock,Ehlo,lstrlen(Ehlo),0); 
578   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
579   iRet=recv(sock,buffer,256,0);
580   if(SOCKET_ERROR==iRet)
581   {
582    ShowError();   
583    return;
584   }
585   TCHAR login[] = "AUTH LOGIN\r\n";      //登陆命令
586   send(sock,login,lstrlen(login),0); 
587   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
588   iRet=recv(sock,buffer,256,0);
589   if(SOCKET_ERROR==iRet)
590   {
591    ShowError();   
592    return;
593   }
594   if(IsDlgButtonChecked(hwnd,IDC_RADIO_QQ)==BST_CHECKED)
595   {
596    strcat(userName,"\r\n");
597    send(sock,userName,lstrlen(userName),0);   //发送加密的用户名
598    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
599    iRet=recv(sock,buffer,256,0);
600    if(SOCKET_ERROR==iRet)
601    {
602     ShowError();   
603     return;
604    }
605    strcat(userPassWord,"\r\n");
606    send(sock,userPassWord,lstrlen(userPassWord),0); //发送加密的密码
607 // 何问起 hovertree.com
608    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
609    iRet=recv(sock,buffer,256,0);
610    if(SOCKET_ERROR==iRet)
611    {
612     ShowError();   
613     return;
614    }
615   }
616   if(IsDlgButtonChecked(hwnd,IDC_RADIO_163)==BST_CHECKED)
617   {
618    send(sock,userName,lstrlen(userName),0);   //发送加密的用户名
619    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
620    iRet=recv(sock,buffer,256,0);
621    if(SOCKET_ERROR==iRet)
622    {
623     ShowError();   
624     return;
625    }
626    send(sock,userPassWord,lstrlen(userPassWord),0); //发送加密的密码
627    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
628    iRet=recv(sock,buffer,256,0);
629    if(SOCKET_ERROR==iRet)
630    {
631     ShowError();   
632     return;
633    }
634   }
635   TCHAR userF[256];
636   TCHAR userL[256];
637   TCHAR temp[256];
638   GetDlgItemText(hwnd,IDC_EDIT_USERNAME,userF,sizeof(userF)/sizeof(TCHAR)); 
639   ComboBox_GetText(GetDlgItem(hwnd,IDC_COMBO_SMTP),temp,sizeof(host_name)/sizeof(TCHAR));
640   strcat(userF,"@");
641   strncpy(userL,temp+5,sizeof(userL));
642   strcat(userF,userL);
643   TCHAR mailFrom[256];
644   //MessageBox(hwnd,userF,"",MB_OK);
645   wsprintf(mailFrom,TEXT("MAIL FROM:<%s>\r\n"),userF);
646   send(sock,mailFrom,lstrlen(mailFrom),0);
647   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
648   iRet=recv(sock,buffer,256,0);
649   if(SOCKET_ERROR==iRet)
650   {
651    ShowError();   
652    return;
653   }
654    TCHAR mailAdd[256];
655    GetDlgItemText(hwnd,IDC_EDIT_MAILADD,mailAdd,sizeof(mailAdd)/sizeof(TCHAR));
656    TCHAR mailTo[256];
657    wsprintf(mailTo,TEXT("RCPT TO:<%s>\r\n"),mailAdd);
658    send(sock,mailTo,lstrlen(mailTo),0);
659    ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
660    iRet=recv(sock,buffer,256,0);
661    if(SOCKET_ERROR==iRet)
662    {
663     ShowError();   
664     return;
665    }
666   TCHAR dataCommad[]="DATA\r\n";
667   send(sock,dataCommad,lstrlen(dataCommad),0); 
668   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
669   iRet=recv(sock,buffer,256,0);
670   if(SOCKET_ERROR==iRet)
671   {
672    ShowError();   
673    return;
674   }
675   //下面是发送正文及附件部份
676   TCHAR data[50000];
677   TCHAR SetMailHead[1024];      //邮件头部信息
678   wsprintf(SetMailHead,TEXT("To:%s\r\nFrom:%s\r\nSubject: %s\r\n"
679          "Date:2012-8-24\r\nX-Mailer:Kevin‘s mailer\r\nMIME-Version:1.0\r\n"
680          "Content-Type: multipart/mixed;\r\n"
681          " boundary=\"----=_NextPart_5039E410_D41071F0_120ABDFB\";\r\n"
682          "This is a multi-part message in MIME format.\r\n\r\n"
683          "------=_NextPart_5039E410_D41071F0_120ABDFB\r\n"
684          "Content-Type: multipart/alternative;\r\n"
685          " boundary=\"----=_NextPart_5039E410_D41071F0_53510C95\";\r\n\r\n"
686          "------=_NextPart_5039E410_D41071F0_53510C95\r\n"
687          "Content-Type: text/plain;\r charset=\"gb2312\"\r\n"
688          "Content-Transfer-Encoding: base64\r\n\r\n"),mailAdd,userF,pTitle);
689   strcat(SetMailHead,pText);      //邮件头部再加上正文文本内容
690   strcat(SetMailHead,TEXT("\r\n\r\n"));
691   strcpy(data,SetMailHead);
692   TCHAR SetMailHtml[1024];      //邮件HTML代码部份
693   wsprintf(SetMailHtml,TEXT("------=_NextPart_5039E410_D41071F0_53510C95\r\n"
694          "Content-Type: text/html;\r\n"
695          " charset=\"gb2312\"\r\n"
696          "Content-Transfer-Encoding: base64\r\n\r\n"));
697   strcat(SetMailHtml,pText);      //加入邮件内容
698   strcat(SetMailHtml,TEXT("\r\n\r\n------=_NextPart_5039E410_D41071F0_53510C95--\r\n\r\n"));
699   strcat(data,SetMailHtml);
700   if(TRUE==attach)        //判断是否添加了附件路径
701   {
702    strcat(data,file);       //加入附件部份代码
703    strcat(data,TEXT("------=_NextPart_5039E410_D41071F0_120ABDFB--\r\n"));
704   }
705   send(sock,data,lstrlen(data),0);
706   TCHAR cN[]="\r\n.\r\n";      //邮件结束标志
707   send(sock,cN,lstrlen(cN),0);
708   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
709   iRet=recv(sock,buffer,256,0);
710   if(SOCKET_ERROR==iRet)
711   {
712    ShowError();   
713    return;
714   }
715   TCHAR cQuit[] = "QUIT\r\n";      //退出命令
716   send(sock,cQuit,lstrlen(cQuit),0); 
717   ZeroMemory(buffer,sizeof(buffer)/sizeof(TCHAR)); 
718   iRet=recv(sock,buffer,256,0);
719   TCHAR cNum[256];
720   if(SOCKET_ERROR==iRet)
721   {
722    ShowError();   
723    return;
724   }  
725   else
726   {  
727    MessageBox(hwnd,TEXT("测试发送成功"),TEXT("恭喜"),MB_OK);
728   }
729    free(pTitle);         //释放空间
730   closesocket(sock);        //关闭SOCK  
731   WSACleanup();
732   return;
733 }
734 void ShowError()
735 {
736  TCHAR* lpMsgBuf;
737  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|  //自动分配消息缓冲区
738  FORMAT_MESSAGE_FROM_SYSTEM,      //从系统获取信息
739  NULL,GetLastError(),        //获取错误信息标识
740  MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),   //使用系统缺省语言
741  (LPTSTR)&lpMsgBuf,        //消息缓冲区
742  0,
743  NULL);
744  MessageBox(NULL,lpMsgBuf,"",MB_ICONERROR); 
745 }
hovertree

希望本文所述对大家的C++程序设计有所帮助。

推荐:http://www.cnblogs.com/roucheng/p/cppjy.html

C++实现邮件群发的方法

标签:

原文地址:http://www.cnblogs.com/roucheng/p/cppmail.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!