VC++ 利用MAPI实现在程序中调用默认的电子邮件程序发送EMAIL(可以添加附件)。

1.利用ShellExecute 可以条用默认邮件客户端,但不能发送带附件的邮件

mailto:用户账号@邮件服务器地址?subject=邮件主题&body=邮件正文  
  如:ShellExecute(handle, ‘open’, ‘ mailto:who@mail.neu.edu.cn?subject=Hello&Body=This is a test’, nil, nil, SW_SHOWNORMAL);打开新邮件窗口,并自动填入收件人地址、邮件主题和邮件正文。若邮件正文包括多行文本,则必须在每行文本之间加入换行转义字符%0a。

转载:http://blog.csdn.net/hczhiyue/article/details/6974593

2.

转载:http://www.vckbase.com/index.php/wv/452.html

转载:http://blog.csdn.net/elcoteq983/article/details/7286560

 

 1 //必须包括 mapi.h 头文件
 2 #include "mapi.h"                     
 3  
 4  /* code 为非Unicode版本 */
 5 /*********************************************************************
 6  * 函数名称:CSendEMailDlg::OnSendMapi
 7  * 说明:  调用MAPI函数发送邮件。
 8  * 作者:  Geng
 9  * 时间 : 2003-04-22 20:08:30 
10 *********************************************************************/
11 void CSendEMailDlg::OnSendMapi() 
12 {
13         UpdateData(true);
14  
15         //装入MAPI32.DLL动态库
16         HMODULE hMod = LoadLibrary("MAPI32.DLL");
17  
18         if (hMod == NULL)
19         {
20                //AfxMessageBox(AFX_IDP_FAILED_MAPI_LOAD);
21                MessageBox(NULL,"加载失败","提示",MB_OK);
22                return;
23         }
24  
25         //获取发送邮件的函数地址
26         ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
27         (FARPROC&)lpfnSendMail = GetProcAddress(hMod, "MAPISendMail");
28  
29         if (lpfnSendMail == NULL)
30         {
31                AfxMessageBox(AFX_IDP_INVALID_MAPI_DLL);
32                return;
33         }
34  
35         int nFileCount = 1;   //有多少个附件需要发送,在此我设置为1
36  
37         //分配内存保存附件信息 不能使用静态数组,因为不知道要发送附件的个数
38         MapiFileDesc* pFileDesc = (MapiFileDesc*)malloc(sizeof(MapiFileDesc) * nFileCount);
39         memset(pFileDesc,0,sizeof(MapiFileDesc) * nFileCount);
40  
41         //分配内存保存附件文件路径
42         TCHAR* pTchPath = (TCHAR*)malloc(MAX_PATH * nFileCount);
43  
44         CString szText;
45         for(int i = 0;i < nFileCount;i++)
46         {
47                TCHAR* p = pTchPath + MAX_PATH * i;
48                m_list.GetText(i,szText);
49                strcpy(p,szText);
50  
51                (pFileDesc + i)->nPosition = (ULONG)-1;
52                (pFileDesc + i)->lpszPathName = p;
53                (pFileDesc + i)->lpszFileName = p;
54         }
55  
56         //收件人结构信息
57         MapiRecipDesc recip;
58         memset(&recip,0,sizeof(MapiRecipDesc));
59         recip.lpszAddress      = xxx@163.com;//收件人邮箱地址
60         recip.ulRecipClass = MAPI_TO;
61  
62         //邮件结构信息
63         MapiMessage message;
64         memset(&message, 0, sizeof(message));
65         message.nFileCount     = nFileCount;                         //文件个数
66         message.lpFiles        = pFileDesc;                          //文件信息
67         message.nRecipCount    = 1;                                  //收件人个数
68         message.lpRecips       = &recip;                             //收件人
69         message.lpszSubject    = "hello";                         //主题
70         message.lpszNoteText   = "This is test";              //正文内容
71  
72         //发送邮件
73         int nError = lpfnSendMail(0, 0,&message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
74  
75         if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT 
76                        && nError != MAPI_E_LOGIN_FAILURE)
77         {
78               // AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
79              MessageBox(NULL,"发送失败","提示",MB_OK);
80         }
81  
82         //不要忘了释放分配的内存
83         free(pFileDesc);
84         free(pTchPath);
85         FreeLibrary(hMod);
86 }

 

  1 /*Unidode 版本*/
  2 
  3 #include "mapi.h"
  4 #include <atlstr.h>
  5 
  6 std::string UnicodeToANSI(const wstring& wstr)
  7 {
  8     int unicodeLen = ::WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,NULL,0, NULL ,NULL);
  9 
 10     if(unicodeLen == 0) return std::string("");
 11 
 12     char *pChar= new char[unicodeLen+1];
 13 
 14     memset(pChar , 0 , sizeof( char ) * (unicodeLen+1));
 15 
 16     ::WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,pChar,unicodeLen, NULL ,NULL);
 17 
 18     pChar[unicodeLen]=0;
 19 
 20     string str = pChar;
 21 
 22     delete [] pChar;
 23     pChar=NULL;
 24 
 25     return str;
 26 }
 27 
 28 int main()
 29 {
 30 
 31 
 32         //装入MAPI32.DLL动态库
 33         HMODULE hMod = LoadLibrary(L"MAPI32.DLL");
 34 
 35         if (hMod == NULL)
 36         {
 37             
 38             return -1;
 39         }
 40 
 41         //获取发送邮件的函数地址
 42         ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
 43         (FARPROC&)lpfnSendMail = GetProcAddress(hMod, "MAPISendMail");
 44 
 45         if (lpfnSendMail == NULL)
 46         {
 47             
 48             return -1;
 49         }
 50 
 51         int nFileCount = 1;   //有多少个附件需要发送
 52 
 53         //分配内存保存附件信息 不能使用静态数组,因为不知道要发送附件的个数
 54         MapiFileDesc* pFileDesc = (MapiFileDesc*)malloc(sizeof(MapiFileDesc) * nFileCount);
 55         memset(pFileDesc,0,sizeof(MapiFileDesc) * nFileCount);
 56 
 57         //分配内存保存附件文件路径
 58         CHAR* pTchPath = (CHAR*)malloc(MAX_PATH * nFileCount);
 59 
 60         CString szText("C:\\ 网站.txt");
 61 
 62         for(int i = 0;i < nFileCount;i++)
 63         {
 64             CHAR* p = pTchPath + MAX_PATH * i;
 65             
 66             string temp = UnicodeToANSI(szText.GetBuffer());
 67             strcpy   (p,temp.c_str());
 68 
 69 
 70             (pFileDesc + i)->nPosition = (ULONG)-1;
 71             (pFileDesc + i)->lpszPathName = p;
 72             (pFileDesc + i)->lpszFileName = p;
 73         }
 74 
 75         //收件人结构信息
 76         MapiRecipDesc recip;
 77         memset(&recip,0,sizeof(MapiRecipDesc));
 78         recip.lpszAddress      = "2xxxxxx1@qq.com";
 79         recip.ulRecipClass     = MAPI_TO;
 80 
 81         recip.lpszName          = "Your Name";
 82 
 83         //邮件结构信息
 84         MapiMessage message;
 85         memset(&message, 0, sizeof(message));
 86         message.nFileCount     = nFileCount;                         //文件个数
 87         message.lpFiles        = pFileDesc;                          //文件信息
 88         message.nRecipCount    = 1;                                  //收件人个数
 89         message.lpRecips       = &recip;                             //收件人
 90         message.lpszSubject    = "hello";           //主题
 91         message.lpszNoteText   = "This is test";              //正文内容
 92 
 93         
 94 
 95         //发送邮件
 96         int nError = lpfnSendMail(0, 0,&message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
 97 
 98         if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT 
 99             && nError != MAPI_E_LOGIN_FAILURE)
100         {
101            //错误提示信息
102         }
103 
104 
105         //不要忘了释放分配的内存
106         free(pFileDesc);
107         free(pTchPath);
108         FreeLibrary(hMod);
109 
110     return 0;
111 }

 

 

转载:http://blog.csdn.net/ghz/article/details/13279

转载:http://blog.csdn.net/waterathena/article/details/3346395

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值