技术文章 > 调用默认邮件收发程序发送带附件的邮件

调用默认邮件收发程序发送带附件的邮件

2018-12-10 22:56

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:

我们知道用ShellExecute可以调用默认邮件收发程序发送邮件,但是好像不能带带附件,下面我们用MAPI来发送一个带附件的邮件。
因为是个控制台程序,不知道怎么联库文件,导致MAPISendMail无法通过链接,程序采用了动态调用的方式。一些具体的使用方法希望大家参考MSDN中的MAPISendMail函数说明。
/*
MAPI发邮件的例子
*/
#include
#include
#include
/* */
int main(int argc, char **argv)
{
HMODULE hMod=LoadLibrary("MAPI32.DLL");
if(hMod == NULL)
{
printf("LoadLibrary fail.\n");
return 0;
}
ULONG (PASCAL *lpfnSendMail) (ULONG, ULONG, MapiMessage *, FLAGS, ULONG);
(FARPROC &)lpfnSendMail=GetProcAddress(hMod, "MAPISendMail");
if(lpfnSendMail == NULL)
{
printf("GetProcAddress fail.\n");
return 0;
}
MapiFileDesc attachment=
{
0, // ulReserved, must be 0
0, // no flags; this is a data file
(ULONG) - 1, // position not specified
argv[0], // pathname, if original filename is NULL, this is the filename
NULL, // original filename
NULL
}; // MapiFileTagExt unused
MapiRecipDesc recip={ 0, MAPI_TO, "NowCan", "nowcan@163.com", 0, NULL };
MapiMessage message=
{
0, // reserved, must be 0
"测试", // subject
"我在csdn的名称是\n\nBIOS测试结果见附件。", // note text
NULL, // NULL = interpersonal message
NULL, // no date; MAPISendMail ignores it
NULL, // no conversation ID
0L, // no flags, MAPISendMail ignores it
NULL, // no originator, this is ignored too
1, // recipients
&recip, // recipient array
1, // one attachment
&attachment
}; // the attachment structure
int nError=lpfnSendMail(0, 0, &message, MAPI_LOGON_UI | MAPI_DIALOG, 0);
// after returning from the MAPISendMail call, the window must
// be re-enabled and focus returned to the frame to undo the workaround
// done before the MAPI call.
if(nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
{
printf("SendMail fail.\n");
}
FreeLibrary(hMod);
}