SendEMailDlg.cpp
上传用户:hkeb168
上传日期:2022-07-04
资源大小:5040k
文件大小:8k
源码类别:

Email客户端

开发平台:

Visual C++

  1. // SendEMailDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "SendEMail.h"
  5. #include "SendEMailDlg.h"
  6. #include "MessBox.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CSendEMailDlg dialog
  51. CSendEMailDlg::CSendEMailDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CSendEMailDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CSendEMailDlg)
  55. // NOTE: the ClassWizard will add member initialization here
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CSendEMailDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CSendEMailDlg)
  64. // NOTE: the ClassWizard will add DDX and DDV calls here
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CSendEMailDlg, CDialog)
  68. //{{AFX_MSG_MAP(CSendEMailDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_BN_CLICKED(ID_SEND, OnSend)
  73. ON_BN_CLICKED(ID_HELP, OnHelp)
  74. ON_BN_CLICKED(ID_SHOW, OnShow)
  75. ON_BN_CLICKED(IDC_END, OnEnd)
  76. //}}AFX_MSG_MAP
  77. END_MESSAGE_MAP()
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CSendEMailDlg message handlers
  80. BOOL CSendEMailDlg::OnInitDialog()
  81. {
  82. CDialog::OnInitDialog();
  83. // Add "About..." menu item to system menu.
  84. // IDM_ABOUTBOX must be in the system command range.
  85. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  86. ASSERT(IDM_ABOUTBOX < 0xF000);
  87. CMenu* pSysMenu = GetSystemMenu(FALSE);
  88. if (pSysMenu != NULL)
  89. {
  90. CString strAboutMenu;
  91. strAboutMenu.LoadString(IDS_ABOUTBOX);
  92. if (!strAboutMenu.IsEmpty())
  93. {
  94. pSysMenu->AppendMenu(MF_SEPARATOR);
  95. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  96. }
  97. }
  98. // Set the icon for this dialog.  The framework does this automatically
  99. //  when the application's main window is not a dialog
  100. SetIcon(m_hIcon, TRUE); // Set big icon
  101. SetIcon(m_hIcon, FALSE); // Set small icon
  102. // TODO: Add extra initialization here
  103. if(AfxSocketInit(NULL)==FALSE)
  104. {
  105. AfxMessageBox("Socket Load Error");
  106. }
  107. return TRUE;  // return TRUE  unless you set the focus to a control
  108. }
  109. void CSendEMailDlg::OnSysCommand(UINT nID, LPARAM lParam)
  110. {
  111. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  112. {
  113. CAboutDlg dlgAbout;
  114. dlgAbout.DoModal();
  115. }
  116. else
  117. {
  118. CDialog::OnSysCommand(nID, lParam);
  119. }
  120. }
  121. // If you add a minimize button to your dialog, you will need the code below
  122. //  to draw the icon.  For MFC applications using the document/view model,
  123. //  this is automatically done for you by the framework.
  124. void CSendEMailDlg::OnPaint() 
  125. {
  126. if (IsIconic())
  127. {
  128. CPaintDC dc(this); // device context for painting
  129. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  130. // Center icon in client rectangle
  131. int cxIcon = GetSystemMetrics(SM_CXICON);
  132. int cyIcon = GetSystemMetrics(SM_CYICON);
  133. CRect rect;
  134. GetClientRect(&rect);
  135. int x = (rect.Width() - cxIcon + 1) / 2;
  136. int y = (rect.Height() - cyIcon + 1) / 2;
  137. // Draw the icon
  138. dc.DrawIcon(x, y, m_hIcon);
  139. }
  140. else
  141. {
  142. CDialog::OnPaint();
  143. }
  144. }
  145. // The system calls this to obtain the cursor to display while the user drags
  146. //  the minimized window.
  147. HCURSOR CSendEMailDlg::OnQueryDragIcon()
  148. {
  149. return (HCURSOR) m_hIcon;
  150. }
  151. void CSendEMailDlg::OnSend() 
  152. {
  153. // TODO: Add your control notification handler code here
  154. if(m_socket.Create()==FALSE)
  155. {
  156. AfxMessageBox("Socket Create Error");
  157. }
  158. m_socket.Init(this);
  159. GetDlgItemText(IDC_SMTP_ADDRESS,m_sSendString[0]);//提取用户输入信息
  160. GetDlgItemText(IDC_FROM_ADDRESS,m_sSendString[4]);
  161. GetDlgItemText(IDC_TO_ADDRESS,m_sSendString[5]);
  162. m_sSendString[0].TrimLeft(" ");//去掉空字符
  163. m_sSendString[0].TrimRight(" ");
  164. m_sSendString[4].TrimLeft(" ");
  165. m_sSendString[4].TrimRight(" ");
  166. m_sSendString[5].TrimLeft(" ");
  167. m_sSendString[5].TrimRight(" ");
  168. if(m_sSendString[0].IsEmpty())
  169. {
  170. AfxMessageBox("请输入SMTP服务器地址(IP)");
  171. return;
  172. }
  173. if(m_sSendString[4].IsEmpty())
  174. {
  175. AfxMessageBox("请输入你的EMail地址");
  176. return;
  177. }
  178. if(m_sSendString[5].IsEmpty())
  179. {
  180. AfxMessageBox("请输入收件人的EMail地址");
  181. return;
  182. }
  183. m_sSendString[0]="HELO " + m_sSendString[0] + "rn";
  184. CString temp1;
  185. GetDlgItemText(IDC_SMTP_ADDRESS,temp1);
  186. temp1.TrimLeft(" ");
  187. temp1.TrimRight(" ");
  188. m_sSendString[1]="EHLO " + temp1 + "rn";
  189. m_sSendString[2]="auth login Y2FpaGFuZ2p1bmp1bg==rn";
  190. m_sSendString[3]="MTQwODE0MDg=rn";
  191. m_sSendString[4]="MAIL FROM:<" +m_sSendString[4] + ">rn";
  192. m_sSendString[5]= "RCPT TO:<" + m_sSendString[5] + ">rn";
  193. m_sSendString[6]="DATArn";
  194. GetDlgItemText(IDC_EMAIL_CONTENT,m_sSendString[7]);
  195. m_sSendString[7]=m_sSendString[7]+ "rnrn.rn";
  196. m_sSendString[8]="QUITrn";
  197. CString temp;
  198. GetDlgItemText(IDC_SMTP_ADDRESS,temp);
  199. temp.TrimLeft(" ");
  200. temp.TrimRight(" ");
  201. if(m_socket.Connect(LPCSTR(temp),25/*端口号*/)==FALSE)
  202. {
  203. AfxMessageBox("Error connect to the SMTP Server");
  204. int i=GetLastError();
  205. switch(i)
  206. {
  207. case WSAENETDOWN:
  208. AfxMessageBox("WSAENETDOWN");
  209. break;
  210. case WSAEADDRINUSE:
  211. AfxMessageBox("WSAEADDRINUSE");
  212. break;
  213. case WSAEINPROGRESS:
  214. AfxMessageBox("WSAEINPROGRESS   ");
  215. break;
  216. case WSAEADDRNOTAVAIL:
  217. AfxMessageBox("WSAEADDRNOTAVAIL   ");
  218. break;
  219. case WSAECONNREFUSED:
  220. AfxMessageBox("WSAECONNREFUSED      ");
  221. break;
  222. case WSAEDESTADDRREQ:
  223. AfxMessageBox("WSAEDESTADDRREQ   ");
  224. break;
  225. case WSAEINVAL:
  226. AfxMessageBox("WSAEINVAL   ");
  227. break;
  228. case WSAETIMEDOUT:
  229. AfxMessageBox("WSAETIMEDOUT   ");
  230. break;
  231. case WSAEWOULDBLOCK:
  232. AfxMessageBox("WSAEWOULDBLOCK");
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. m_sReceivedData="";
  239. }
  240. void CSendEMailDlg::OnHelp() 
  241. {
  242. // TODO: Add your control notification handler code here
  243. CDialog dlg(IDD_ABOUTBOX);
  244. dlg.DoModal();
  245. }
  246. void CSendEMailDlg::ReceiveMessage(int count)
  247. {
  248. if(count>=9)
  249. {
  250. AfxMessageBox("邮件发送完毕!请查看SMTP回应的信息进一步确定!");
  251. m_socket.Close();
  252. return;
  253. }
  254. char sBuffer[255];
  255. int len=m_socket.Receive(sBuffer,sizeof(sBuffer));
  256. sBuffer[len]=NULL;
  257. m_sReceivedData=m_sReceivedData+sBuffer;
  258. m_sReceivedData=m_sReceivedData+m_sSendString[count];
  259. m_socket.Send(m_sSendString[count],m_sSendString[count].GetLength());
  260. }
  261. void CSendEMailDlg::OnShow() 
  262. {
  263. // TODO: Add your control notification handler code here
  264. CMessBox dlg;
  265. dlg.m_sMessage=m_sReceivedData;
  266. dlg.DoModal();
  267. }
  268. void CSendEMailDlg::OnEnd() 
  269. {
  270. // TODO: Add your control notification handler code here
  271. m_socket.Close();
  272. return;
  273. }