程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++完成郵件群發的辦法

C++完成郵件群發的辦法

編輯:關於C++

C++完成郵件群發的辦法。本站提示廣大學習愛好者:(C++完成郵件群發的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++完成郵件群發的辦法正文


本文實例講述了C++完成郵件群發的辦法。分享給年夜家供年夜家參考。詳細以下:
關於生成隨機QQ郵箱不准確的成績,在以後版本盤算另寫一個收集器插件停止帳號收集,所以,這個軟件只用來停止內容發送,郵箱停止隨機生成

假如你曾經有收集來的QQ號,請復制到SendList.txt 調換內容便可
可以直接復制HTML代碼到郵件內容,保留便可。今朝郵件內容最年夜設置為10000字節,假如有增年夜的需要,迎接提交留言。

這是我進修後VC編程中觸及到多線程,socket,及一些WINDOWS API的宗合運用
應用解釋:

1、SMTP設置

1、SMTP設置中,收件箱地址:填寫郵箱地址為帳號測試郵箱,可以填寫你本身的郵箱作為吸收測試。打星號為必填。郵箱帳號及暗碼,是行將用於群發的帳號和暗碼
2、群發發送時光距離,默許為零,可以選擇填寫發送距離時光,單元為秒,避免屢次疾速發送雷同內容被停用。
3、勾選郵件題目後拔出體系時光,可以在題目上加上體系時光
4、導入群發列表,勾選後,可以點擊群發,暫停,持續。(群發列表為法式目次中SendList.txt)假如你有郵箱收集軟件,可以把彩集的郵箱地址依照格局,一行一個,放入個中),沒有的話,可以點擊“收件箱”頁面熟成隨機QQ郵箱

2、郵件內容

1、填寫郵件題目,郵件內容可認為純文本,也能夠是HTML代碼,附件今朝只支撐TXT文本。
2、群發前,請測試郵件內容,能否完全。附件能否正常吸收。

3、收件箱

1、收件箱中次序生成QQ郵箱,請不要跨越6位數QQ號,位數過年夜,生成時光太長,輕易形成假逝世。
2、群發前,師長教師成QQ郵箱列表。假如你有收集郵箱帳號,可以按格局放入SendList.txt 中

版本更新解釋:

1、可增長多個附件。
2、修復題目拔出日期形成法式瓦解的BUG。
3、增長發送距離時光選項。
4、支撐HTML代碼
5、增長干涉碼

#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("[email protected]"));
 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++法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved