C ++通过Outlook发送HTML电子邮件

时间:2012-08-24 16:28:05

标签: c++ windows outlook-2007

[目的]

基本C ++控制台应用程序需要能够通过Outlook 2007(最小化运行)发送HTML电子邮件 - 不需要附件。此方法适用于纯文本,我不确定是否可以修改它以符合HTML的要求。我读到某个地方,或许你可以引用一个附件,它会成为身体。我只是不确定接下来该做什么。建议?

[到目前为止我所拥有的]

BOOL SendMail(CHAR *lpszFrom, CHAR *lpszTo, CHAR *lpszSubject, CHAR *lpszMessage)
{
   BOOL bSent = FALSE;

   HINSTANCE hMAPI = ::LoadLibrary(_T("mapi32.dll"));
   if(0==hMAPI) return bSent;

   typedef ULONG (FAR PASCAL *PFN_MAPILogon)(ULONG,LPTSTR,LPTSTR,FLAGS,ULONG,LPLHANDLE);
   typedef ULONG (FAR PASCAL *PFN_MAPISendMail)(LHANDLE,ULONG,lpMapiMessage,FLAGS,ULONG);
   typedef ULONG (FAR PASCAL *PFN_MAPILogoff)(LHANDLE,ULONG,FLAGS,ULONG);

   PFN_MAPILogon MAPILogon = (PFN_MAPILogon)::GetProcAddress(hMAPI,"MAPILogon");
   PFN_MAPISendMail MAPISendMail = (PFN_MAPISendMail)::GetProcAddress(hMAPI,"MAPISendMail");
   PFN_MAPILogoff MAPILogoff = (PFN_MAPILogoff)::GetProcAddress(hMAPI,"MAPILogoff");

   const BOOL bFunctionsLoaded = (0!=MAPILogon)&&(0!=MAPISendMail)&&(0!=MAPILogoff);
   ASSERT(bFunctionsLoaded);

   if(bFunctionsLoaded)
   {

      LHANDLE session = 0;
      VERIFY(SUCCESS_SUCCESS==MAPILogon(0,0,0,MAPI_NEW_SESSION,0,&session));
      ASSERT(0!=session);

      MapiRecipDesc recipient;
      ::ZeroMemory(&recipient,sizeof(recipient));
      recipient.ulRecipClass = MAPI_TO;
      recipient.lpszName = lpszTo;

      MapiMessage message;
      ::ZeroMemory(&message,sizeof(message));
      message.lpszSubject = lpszSubject;
      message.lpszNoteText = lpszMessage;
      message.nRecipCount = 1;
      message.lpRecips = &recipient;

      bSent = SUCCESS_SUCCESS == MAPISendMail(session,0,&message,0,0);

      VERIFY(SUCCESS_SUCCESS==MAPILogoff(session,0,0,0));

   }

   ::FreeLibrary(hMAPI);

   return bSent;
}

被......打电话

SendMail("from","to","subject","body");

2 个答案:

答案 0 :(得分:1)

看起来MAPI不适合HTML电子邮件。

http://support.microsoft.com/kb/268440

我通过MFC发送了HTML电子邮件,但这不是MAPI。

答案 1 :(得分:0)

我发送了一个要求相同的东西 - 我发现附加一个html文件会导致在电子邮件正文中使用html文件。

以下代码在此网站(某处)上找到,效果很好。

bool SendMail(HWND hWndParent, std::string strAttachmentFileName,  std::string strSubject,std::string& err)
{
    // The attachment must exist as a file on the system
    // or MAPISendMail will fail, so......
    if (strAttachmentFileName.empty())
        return false;

    // You may want to remove this check, but if a valid
    // HWND is passed in, the mail dialog will be made
    // modal to it's parent.
    //if (!hWndParent || !::IsWindow(hWndParent))
    //   return false;

    HINSTANCE hMAPI = ::LoadLibraryA("MAPI32.DLL");
    if (!hMAPI)
        return false;

    // Grab the exported entry point for the MAPISendMail function
    ULONG (PASCAL *SendMail)(ULONG, ULONG_PTR, 
            MapiMessage*, FLAGS, ULONG);
    (FARPROC&)SendMail = GetProcAddress(hMAPI, 
            "MAPISendMail");

    if (!SendMail)
        return false;

    //  TCHAR szFileName[_MAX_PATH];
    // TCHAR szPath[_MAX_PATH];
    // TCHAR szSubject[_MAX_PATH];
    // ::strcpy(&szFileName[0], strAttachmentFileName.c_str());
    // ::strcpy(&szPath[0], strAttachmentFileName.c_str());
    // ::strcpy(&szSubject[0], strSubject.c_str());

    MapiFileDesc fileDesc;
    ::ZeroMemory(&fileDesc, sizeof(fileDesc));
    fileDesc.nPosition = (ULONG)-1;
    fileDesc.lpszPathName = (char *)strAttachmentFileName.c_str();
    fileDesc.lpszFileName = (char *)strAttachmentFileName.c_str();

    MapiMessage message;
    ::ZeroMemory(&message, sizeof(message));
    message.lpszSubject = (char *)strSubject.c_str();//&szSubject[0];//szSubject;
    message.nFileCount = 1;
    message.lpFiles = &fileDesc;

    // Ok to send
    int nError = SendMail(0, (ULONG_PTR)hWndParent, 
            &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);

    if (nError != SUCCESS_SUCCESS && 
            nError != MAPI_USER_ABORT && 
            nError != MAPI_E_LOGIN_FAILURE)
        err =  "error";
    return false;
    err="ok";
    return true;
}