简体   繁体   English

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

[英]C++ send HTML email through Outlook

[objective] [目的]

Basic C++ console application needs to be able to send HTML emails through Outlook 2007 (which runs minimized) - attachments are not necessary. 基本的C ++控制台应用程序需要能够通过Outlook 2007发送HTML电子邮件(运行在最小化状态)-不需要附件。 This method works with plain text and I'm not sure if it can be modified to fit the requirements of HTML. 此方法适用于纯文本,我不确定是否可以对其进行修改以适合HTML的要求。 I read somewhere that perhaps you could reference an attachment and it would become the body. 我在某处读到,也许您可​​以引用附件,它将成为主体。 I'm just not sure what to do next. 我只是不确定下一步该怎么做。 Suggestions? 有什么建议吗?

[what I have so far] [到目前为止我有什么]

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;
}

Called by... 叫...

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

It looks like MAPI isn't suited for HTML emails. 看来MAPI不适合HTML电子邮件。

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

I've sent HTML emails through MFC but that isn't MAPI. 我已经通过MFC发送了HTML电子邮件,但这不是MAPI。

I was sending an requiring the same thing - i found that attaching an html file would result in the html file being used in the body of the email. 我正在发送要求相同的内容-我发现附加html文件将导致该html文件在电子邮件正文中使用。

the code below was found on this site ( somewhere ) and it works great. 在此网站(某处)找到了以下代码,它的工作原理很棒。

code

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM