MciDlg.cpp

上传用户:chaming
上传日期:2015-08-07
资源大小:34k
文件大小:8k
源码类别:Email客户端
开发平台:Visual C++
  1. // MciDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Mci.h"
  5. #include "MciDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #include <mapi.h>
  12. ////////////////////加上这几行///////////////////////////////
  13. #include <stdlib.h>
  14. /* 定义返回值*/
  15. #define SENDEMAIL_SUCCESS               0 //邮件成功发送
  16. #define SENDEMAIL_MAPI_NOT_INSTALLED    1 //没有安装MAPI Server
  17. #define SENDEMAIL_MAPILOAD_FAILED       2 //加载 MAPI32.DLL 失败
  18. #define SENDEMAIL_LOGON_FAILED          3 //无法登陆到 MAPI Server 或者 用户取消了登陆框
  19. #define SENDEMAIL_SEND_FAILED           4 //消息发送失败
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CMciDlg dialog
  22. CMciDlg::CMciDlg(CWnd* pParent /*=NULL*/)
  23. : CDialog(CMciDlg::IDD, pParent)
  24. {
  25. //{{AFX_DATA_INIT(CMciDlg)
  26. // NOTE: the ClassWizard will add member initialization here
  27. //}}AFX_DATA_INIT
  28. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  29. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  30. }
  31. void CMciDlg::DoDataExchange(CDataExchange* pDX)
  32. {
  33. CDialog::DoDataExchange(pDX);
  34. //{{AFX_DATA_MAP(CMciDlg)
  35. // NOTE: the ClassWizard will add DDX and DDV calls here
  36. //}}AFX_DATA_MAP
  37. }
  38. BEGIN_MESSAGE_MAP(CMciDlg, CDialog)
  39. //{{AFX_MSG_MAP(CMciDlg)
  40. ON_WM_PAINT()
  41. ON_WM_QUERYDRAGICON()
  42. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  43. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  44. //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CMciDlg message handlers
  48. BOOL CMciDlg::OnInitDialog()
  49. {
  50. CDialog::OnInitDialog();
  51. // Set the icon for this dialog.  The framework does this automatically
  52. //  when the application's main window is not a dialog
  53. SetIcon(m_hIcon, TRUE); // Set big icon
  54. SetIcon(m_hIcon, FALSE); // Set small icon
  55. // TODO: Add extra initialization here
  56. return TRUE;  // return TRUE  unless you set the focus to a control
  57. }
  58. // If you add a minimize button to your dialog, you will need the code below
  59. //  to draw the icon.  For MFC applications using the document/view model,
  60. //  this is automatically done for you by the framework.
  61. void CMciDlg::OnPaint() 
  62. {
  63. if (IsIconic())
  64. {
  65. CPaintDC dc(this); // device context for painting
  66. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  67. // Center icon in client rectangle
  68. int cxIcon = GetSystemMetrics(SM_CXICON);
  69. int cyIcon = GetSystemMetrics(SM_CYICON);
  70. CRect rect;
  71. GetClientRect(&rect);
  72. int x = (rect.Width() - cxIcon + 1) / 2;
  73. int y = (rect.Height() - cyIcon + 1) / 2;
  74. // Draw the icon
  75. dc.DrawIcon(x, y, m_hIcon);
  76. }
  77. else
  78. {
  79. CDialog::OnPaint();
  80. }
  81. }
  82. // The system calls this to obtain the cursor to display while the user drags
  83. //  the minimized window.
  84. HCURSOR CMciDlg::OnQueryDragIcon()
  85. {
  86. return (HCURSOR) m_hIcon;
  87. }
  88. void CMciDlg::OnOK() 
  89. {
  90. // TODO: Add extra validation here
  91. CDialog::OnOK();
  92. }
  93. void CMciDlg::OnButton1() 
  94. {
  95. // TODO: Add your control notification handler code here
  96. HINSTANCE hInstMail;
  97. hInstMail=::LoadLibrary("MAPI32.DLL");
  98. if(!hInstMail)
  99. {
  100. AfxMessageBox("加载 MAPI32.DLL 出错!");
  101. }
  102. ULONG (PASCAL *lpfnMAPISendMail) (LHANDLE lhSession, ULONG ulUIParam, lpMapiMessage lpMessage, 
  103. FLAGS flFlags, ULONG ulReserved);
  104. ULONG (PASCAL *lpfnMAPIResolveName) (LHANDLE lhSession, ULONG ulUIParam, LPTSTR lpszName, 
  105. FLAGS ulFlags, ULONG ulReserved,lpMapiRecipDesc FAR *lppRecip);
  106. ULONG (FAR PASCAL *lpfnMAPILogon)(ULONG ulUIParam, LPSTR lpszProfileName, LPSTR lpszPassword, 
  107. FLAGS flFlags, ULONG ulReserved, LPLHANDLE lplhSession);
  108. ULONG (FAR PASCAL *lpfnMAPILogoff)(LHANDLE lhSession, ULONG ulUIParam, FLAGS flFlags,
  109. ULONG ulReserved);
  110. ULONG (FAR PASCAL *lpfnMAPIFreeBuffer)(LPVOID lpBuffer);
  111. ULONG (FAR PASCAL *lpfnMAPIAddress)(LHANDLE lhSession,ULONG ulUIParam, LPSTR lpszCaption,
  112. ULONG nEditFields, LPSTR lpszLabels,ULONG nRecips, lpMapiRecipDesc lpRecips,
  113. FLAGS flFlags, ULONG ulReserved, LPULONG lpnNewRecips, lpMapiRecipDesc FAR *lppNewRecips);
  114. ULONG (FAR PASCAL *lpfnMAPIFindNext)(LHANDLE lhSession,ULONG ulUIParam, LPSTR lpszMessageType, 
  115. LPSTR lpszSeedMessageID, FLAGS flFlags,ULONG ulReserved, LPSTR lpszMessageID);
  116. ULONG (FAR PASCAL *lpfnMAPIReadMail)(LHANDLE lhSession, ULONG ulUIParam, LPSTR lpszMessageID,
  117. FLAGS flFlags, ULONG ulReserved,lpMapiMessage FAR *lppMessage);
  118. (FARPROC&) lpfnMAPISendMail = GetProcAddress(hInstMail,"MAPISendMail");
  119. (FARPROC&) lpfnMAPIResolveName = GetProcAddress(hInstMail,"MAPIResolveName");
  120. (FARPROC&) lpfnMAPILogon = GetProcAddress(hInstMail,"MAPILogon");
  121. (FARPROC&) lpfnMAPILogoff = GetProcAddress(hInstMail,"MAPILogoff");
  122. (FARPROC&) lpfnMAPIFreeBuffer = GetProcAddress(hInstMail,"MAPIFreeBuffer");
  123. (FARPROC&) lpfnMAPIAddress = GetProcAddress(hInstMail,"MAPIAddress");
  124. (FARPROC&) lpfnMAPIFindNext = GetProcAddress(hInstMail,"MAPIFindNext");
  125. (FARPROC&) lpfnMAPIReadMail = GetProcAddress(hInstMail,"MAPIReadMail");
  126. LHANDLE lhSession;
  127. ULONG lResult = lpfnMAPILogon(0, NULL, NULL, MAPI_NEW_SESSION  , 0, &lhSession);
  128. if (lResult != SUCCESS_SUCCESS)
  129. //SUCCESS_SUCCESS在MAPI.H中被定义(0)
  130. {
  131. AfxMessageBox("登陆MAPI Server 出错!");
  132. }
  133. MapiMessage message;
  134. memset(&message, 0, sizeof(message));
  135. message.ulReserved = 0;
  136. message.lpszMessageType = NULL;
  137. char subject[512];
  138. strcpy(subject, "MAPI.DLL测试");
  139. message.lpszSubject =subject;
  140. char text[5000];
  141. strcpy(text, "嗨,这是一个MAPI.DLL测试!");
  142. message.lpszNoteText = text;
  143. message.flFlags = MAPI_SENT;
  144. message.lpOriginator = NULL;
  145. message.nRecipCount = 1;
  146. char recipient[512];
  147. strcpy(recipient, "zhaoxn@sdb.com.cn");
  148. lResult = lpfnMAPIResolveName(lhSession, 0, recipient,0, 0, &message.lpRecips);
  149. message.lpRecips->ulRecipClass = MAPI_TO;
  150. lResult = lpfnMAPISendMail(lhSession, 0, &message, 0, 0);
  151.     if(lResult != SUCCESS_SUCCESS)
  152. AfxMessageBox("发送邮件失败!");
  153.     lpfnMAPILogoff(lhSession, 0, 0, 0);
  154. lpfnMAPIFreeBuffer(message.lpRecips);
  155. }
  156. void CMciDlg::OnButton2() 
  157. {
  158. // TODO: Add your control notification handler code here
  159.     int rc = SendEmail("SMTP:hangwire@sina.com", "一个小测试",
  160.               "你好,n这是测试信息。");
  161. }
  162. int CMciDlg::SendEmail(const char* Address,
  163.   const char* Subject, 
  164.   const char* Text)
  165. {
  166.     int             iResult;
  167.     UINT            iMapiInstalled;
  168.     HINSTANCE       hMAPIInst;
  169.     LPMAPILOGON     pMAPILogon;
  170.     LPMAPILOGOFF    pMAPILogoff;
  171.     LPMAPISENDMAIL  pMAPISendMail;
  172.     LHANDLE         lhSession;
  173.     iResult         = SENDEMAIL_SUCCESS;
  174.     iMapiInstalled  = GetProfileInt("Mail", "MAPI", 0);
  175.     if(! iMapiInstalled)
  176.         return SENDEMAIL_MAPI_NOT_INSTALLED;
  177.     hMAPIInst       = LoadLibrary("MAPI32.DLL");
  178.     if(!hMAPIInst)
  179.         return SENDEMAIL_MAPILOAD_FAILED;
  180.     pMAPILogon      = (LPMAPILOGON)
  181.                       GetProcAddress(hMAPIInst, "MAPILogon");
  182.     pMAPILogoff     = (LPMAPILOGOFF)
  183.                       GetProcAddress(hMAPIInst, "MAPILogoff");
  184.     pMAPISendMail   = (LPMAPISENDMAIL)
  185.                       GetProcAddress(hMAPIInst, "MAPISendMail");
  186.     if(pMAPILogon(0, NULL, NULL, MAPI_LOGON_UI, 0, &lhSession)
  187.         != SUCCESS_SUCCESS)
  188.     {
  189.         iResult = SENDEMAIL_LOGON_FAILED;
  190.     }
  191.     else        /* Send the Message         */
  192.     {
  193.         ULONG       Result;
  194.         MapiMessage Msg;
  195.         MapiRecipDesc Recipients[1];
  196.         Recipients[0].ulReserved = 0;
  197.         Recipients[0].ulRecipClass = MAPI_TO;
  198.         Recipients[0].lpszName = (char*)Address;
  199.         Recipients[0].lpszAddress = (char*)Address;
  200.         Recipients[0].ulEIDSize = 0;
  201.         Recipients[0].lpEntryID = 0;
  202.         memset(&Msg, 0, sizeof(Msg));
  203.         Msg.lpszSubject         = (char*)Subject;
  204.         Msg.lpszNoteText        = (char*)Text;
  205.         Msg.nRecipCount         = 1;
  206.         Msg.lpRecips            = Recipients;
  207. // lResult = lpfnMAPISendMail(lhSession, 0, &message, 0, 0);
  208.         Result      = pMAPISendMail(lhSession, 0, &Msg, 0, 0);
  209.         if(Result != SUCCESS_SUCCESS)
  210.             iResult = SENDEMAIL_SEND_FAILED;
  211.         pMAPILogoff(lhSession, 0, 0, 0);
  212.     }
  213.     FreeLibrary(hMAPIInst);
  214.     return iResult;
  215. }