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

时间:2024-01-06 22:28:02

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

 //必须包括 mapi.h 头文件
#include "mapi.h" /* code 为非Unicode版本 */
/*********************************************************************
* 函数名称:CSendEMailDlg::OnSendMapi
* 说明: 调用MAPI函数发送邮件。
* 作者: Geng
* 时间 : 2003-04-22 20:08:30
*********************************************************************/
void CSendEMailDlg::OnSendMapi()
{
UpdateData(true); //装入MAPI32.DLL动态库
HMODULE hMod = LoadLibrary("MAPI32.DLL"); if (hMod == NULL)
{
//AfxMessageBox(AFX_IDP_FAILED_MAPI_LOAD);
MessageBox(NULL,"加载失败","提示",MB_OK);
return;
} //获取发送邮件的函数地址
ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
(FARPROC&)lpfnSendMail = GetProcAddress(hMod, "MAPISendMail"); if (lpfnSendMail == NULL)
{
AfxMessageBox(AFX_IDP_INVALID_MAPI_DLL);
return;
} int nFileCount = ; //有多少个附件需要发送,在此我设置为1 //分配内存保存附件信息 不能使用静态数组,因为不知道要发送附件的个数
MapiFileDesc* pFileDesc = (MapiFileDesc*)malloc(sizeof(MapiFileDesc) * nFileCount);
memset(pFileDesc,,sizeof(MapiFileDesc) * nFileCount); //分配内存保存附件文件路径
TCHAR* pTchPath = (TCHAR*)malloc(MAX_PATH * nFileCount); CString szText;
for(int i = ;i < nFileCount;i++)
{
TCHAR* p = pTchPath + MAX_PATH * i;
m_list.GetText(i,szText);
strcpy(p,szText); (pFileDesc + i)->nPosition = (ULONG)-;
(pFileDesc + i)->lpszPathName = p;
(pFileDesc + i)->lpszFileName = p;
} //收件人结构信息
MapiRecipDesc recip;
memset(&recip,,sizeof(MapiRecipDesc));
recip.lpszAddress = xxx@.com;//收件人邮箱地址
recip.ulRecipClass = MAPI_TO; //邮件结构信息
MapiMessage message;
memset(&message, , sizeof(message));
message.nFileCount = nFileCount; //文件个数
message.lpFiles = pFileDesc; //文件信息
message.nRecipCount = ; //收件人个数
message.lpRecips = &recip; //收件人
message.lpszSubject = "hello"; //主题
message.lpszNoteText = "This is test"; //正文内容 //发送邮件
int nError = lpfnSendMail(, ,&message, MAPI_LOGON_UI|MAPI_DIALOG, ); if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT
&& nError != MAPI_E_LOGIN_FAILURE)
{
// AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
MessageBox(NULL,"发送失败","提示",MB_OK);
} //不要忘了释放分配的内存
free(pFileDesc);
free(pTchPath);
FreeLibrary(hMod);
}
 /*Unidode 版本*/

 #include "mapi.h"
#include <atlstr.h> std::string UnicodeToANSI(const wstring& wstr)
{
int unicodeLen = ::WideCharToMultiByte(CP_ACP,,wstr.c_str(),-,NULL,, NULL ,NULL); if(unicodeLen == ) return std::string(""); char *pChar= new char[unicodeLen+]; memset(pChar , , sizeof( char ) * (unicodeLen+)); ::WideCharToMultiByte(CP_ACP,,wstr.c_str(),-,pChar,unicodeLen, NULL ,NULL); pChar[unicodeLen]=; string str = pChar; delete [] pChar;
pChar=NULL; return str;
} int main()
{ //装入MAPI32.DLL动态库
HMODULE hMod = LoadLibrary(L"MAPI32.DLL"); if (hMod == NULL)
{ return -;
} //获取发送邮件的函数地址
ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
(FARPROC&)lpfnSendMail = GetProcAddress(hMod, "MAPISendMail"); if (lpfnSendMail == NULL)
{ return -;
} int nFileCount = ; //有多少个附件需要发送 //分配内存保存附件信息 不能使用静态数组,因为不知道要发送附件的个数
MapiFileDesc* pFileDesc = (MapiFileDesc*)malloc(sizeof(MapiFileDesc) * nFileCount);
memset(pFileDesc,,sizeof(MapiFileDesc) * nFileCount); //分配内存保存附件文件路径
CHAR* pTchPath = (CHAR*)malloc(MAX_PATH * nFileCount); CString szText("C:\\ 网站.txt"); for(int i = ;i < nFileCount;i++)
{
CHAR* p = pTchPath + MAX_PATH * i; string temp = UnicodeToANSI(szText.GetBuffer());
strcpy (p,temp.c_str()); (pFileDesc + i)->nPosition = (ULONG)-;
(pFileDesc + i)->lpszPathName = p;
(pFileDesc + i)->lpszFileName = p;
} //收件人结构信息
MapiRecipDesc recip;
memset(&recip,,sizeof(MapiRecipDesc));
recip.lpszAddress = "2xxxxxx1@qq.com";
recip.ulRecipClass = MAPI_TO; recip.lpszName = "Your Name"; //邮件结构信息
MapiMessage message;
memset(&message, , sizeof(message));
message.nFileCount = nFileCount; //文件个数
message.lpFiles = pFileDesc; //文件信息
message.nRecipCount = ; //收件人个数
message.lpRecips = &recip; //收件人
message.lpszSubject = "hello"; //主题
message.lpszNoteText = "This is test"; //正文内容 //发送邮件
int nError = lpfnSendMail(, ,&message, MAPI_LOGON_UI|MAPI_DIALOG, ); if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT
&& nError != MAPI_E_LOGIN_FAILURE)
{
//错误提示信息
} //不要忘了释放分配的内存
free(pFileDesc);
free(pTchPath);
FreeLibrary(hMod); return ;
}

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

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