C++调用outlook发送电子邮件替代shellexecute

231人阅读 评论(1) 收藏 举报

 公司有个即时通讯软件,以前调用shellexecute发送电子邮件,发现这个函数在有些机器经常失效,后上codeproject.com找了些资料,自己修改封装,测试效果不错,好东西大家共分享:

 

#ifndef __SENDFILETO_H__
#define __SENDFILETO_H__

#include <mapi.h>
#include <string>

class CSendFileTo
{
public:
    static bool SendMailAttach(const std::string& strAttachmentFileName, const std::string& strSubject=_T(""))
 {
  if (strAttachmentFileName.empty())
   return false;

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

  ULONG (PASCAL *SendMail)(ULONG, ULONG_PTR, MapiMessage*, FLAGS, ULONG);
  (FARPROC&)SendMail = GetProcAddress(hMAPI, _T("MAPISendMail"));

  if (!SendMail)
        {
            ::FreeLibrary(hMAPI);
            return false;
        }
   

  char szFileName[_MAX_PATH];
  char szPath[_MAX_PATH];
  char szSubject[_MAX_PATH];
       
        strcpy(szFileName,strAttachmentFileName.c_str());
        strcpy(szPath, strAttachmentFileName.c_str());
        strcpy(szSubject, strSubject.c_str());

  /*::StrCpy(szFileName, strAttachmentFileName.GetString());
  ::StrCpy(szPath, strAttachmentFileName.GetString());
  ::StrCpy(szSubject, strSubject.GetString());*/

  MapiFileDesc fileDesc;
  ::ZeroMemory(&fileDesc, sizeof(fileDesc));
  fileDesc.nPosition = (ULONG)-1;
  fileDesc.lpszPathName = szPath;
  fileDesc.lpszFileName = szFileName;

  MapiMessage message;
  ::ZeroMemory(&message, sizeof(message));
  message.lpszSubject = szSubject;
  message.nFileCount = 1;
  message.lpFiles = &fileDesc;

  int nError = SendMail(0, (ULONG)::GetDesktopWindow(), &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);

  if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
        {
            ::FreeLibrary(hMAPI);
            return false;
        }
   
        ::FreeLibrary(hMAPI);
  return true;
 }

    static bool SendMail(char* szToEmail, char* szToName = "")
    {
        /*
        MapiFileDesc fileDesc;
        memset(&fileDesc, 0, sizeof(fileDesc));
        fileDesc.nPosition = (ULONG)-1;

        //USES_CONVERSION;

        CString strFullPath("c://test.txt");
        CString strFileName;
        int nPos = strFullPath.ReverseFind(_T('//'));
        if (nPos != -1)
        {
            strFileName = strFullPath.Right(strFullPath.GetLength() - nPos - 1);
        }
        else
        {
            strFileName = strFullPath;
        }

        fileDesc.lpszPathName = strFullPath.GetBuffer();
        fileDesc.lpszFileName = strFileName.GetBuffer();
        */

        MapiMessage message;
        memset(&message, 0, sizeof(message));

        message.lpszSubject = "";
        message.nFileCount = 0;
        message.lpFiles = NULL;
        message.flFlags = MAPI_SENT;
        message.ulReserved = 0;

        /*
        MapiRecipDesc sender = {0};
        sender.ulRecipClass = MAPI_ORIG;
        sender.lpszName = T2A("ken");
        char szAdderss[100] = "SMTP:";
        strncat(szAdderss, sender.lpszName, 100);
        sender.lpszAddress = szAdderss;
        sender.ulReserved = 0;


        message.lpOriginator = &sender;
        */

        MapiRecipDesc recv = {0};
        recv.ulRecipClass = MAPI_TO;
        recv.lpszAddress = szToEmail;
        recv.lpszName = szToName;

        message.lpRecips = &recv;
        message.nRecipCount = 1;

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

        ULONG (PASCAL *SendMail)(ULONG, ULONG_PTR, MapiMessage*, FLAGS, ULONG);
        (FARPROC&)SendMail = GetProcAddress(hMAPI, _T("MAPISendMail"));

        if (!SendMail)
        {
          
            ::FreeLibrary(hMAPI);
            return false;
        }


        int nError = SendMail(0, (ULONG)::GetDesktopWindow(),
            &message, MAPI_DIALOG | MAPI_LOGON_UI , 0);

        if (nError != SUCCESS_SUCCESS &&
            nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
        {
            ::FreeLibrary(hMAPI);
            return false;
        }

        ::FreeLibrary(hMAPI);
        return true;

    }
};

#endif

 

你可能感兴趣的:(C++调用outlook发送电子邮件替代shellexecute)