信息: 将邮件保存到 MSG 复合文件

注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。

点击这里察看该文章的英文版: 171907
概要
本文包含的代码演示如何将保存到一条消息复合文档 — 专门.msg 文件--读取的任何客户端支持的.msg 文件格式。
更多信息
下面的函数将作为 in 参数有效的消息对象,并使用要创建重复的邮件并保存到其属性复合使用.msg 格式的文件。邮件的主题行用作新文件的文件名。

注意: 此参数中的主题行中的特殊字符函数可能导致意外的结果。虽然可以编写代码以避免在主题行中的特殊字符,它不是 germane 的主题和此类代码有意省略。
#define INITGUID#include <objbase.h>#define USES_IID_IMessage#include <mapix.h>#include <mapitags.h>#include <mapidefs.h>#include <mapiutil.h>#include <mapiguid.h>#include <imessage.h>// {00020D0B-0000-0000-C000-000000000046}DEFINE_GUID(CLSID_MailMessage,0x00020D0B,0x0000, 0x0000, 0xC0, 0x00, 0x0, 0x00, 0x0, 0x00, 0x00, 0x46);HRESULT SaveToMSG ( LPMESSAGE pMessage ){    HRESULT hRes = S_OK;    LPSPropValue pSubject = NULL;    LPSTORAGE pStorage = NULL;    LPMSGSESS pMsgSession =  NULL;    LPMESSAGE pIMsg = NULL;    SizedSPropTagArray ( 7, excludeTags );    char szPath[_MAX_PATH];    char strAttachmentFile[_MAX_PATH];    LPWSTR lpWideCharStr = NULL;    ULONG cbStrSize = 0L;    // create the file name in the directory where "TMP" is defined    // with subject as the filename and ".msg" extension.    // get temp file directory    GetTempPath(_MAX_PATH, szPath);    // get subject line of message to copy. This will be used as the    // new file name.    HrGetOneProp( pMessage, PR_SUBJECT, &pSubject );    // fuse path, subject, and suffix into one string    strcpy ( strAttachmentFile, szPath );    strcat ( strAttachmentFile, pSubject->Value.lpszA  );    strcat ( strAttachmentFile, ".msg");    // get memory allocation function    LPMALLOC pMalloc = MAPIGetDefaultMalloc();    // Convert new file name to WideChar    cbStrSize = MultiByteToWideChar (CP_ACP,                                     MB_PRECOMPOSED,                                     strAttachmentFile,                                     -1, lpWideCharStr, 0);    MAPIAllocateBuffer ( cbStrSize * sizeof(WCHAR),                            (LPVOID *)&lpWideCharStr );    MultiByteToWideChar (CP_ACP,                         MB_PRECOMPOSED,                         strAttachmentFile,                         -1, lpWideCharStr, cbStrSize );    // create compound file    hRes = ::StgCreateDocfile(lpWideCharStr,                              STGM_READWRITE |                              STGM_TRANSACTED |                              STGM_CREATE, 0, &pStorage);    // Open an IMessage session.    hRes = ::OpenIMsgSession(pMalloc, 0, &pMsgSession);    // Open an IMessage interface on an IStorage object    hRes = ::OpenIMsgOnIStg(pMsgSession,                            MAPIAllocateBuffer,                            MAPIAllocateMore,                            MAPIFreeBuffer,                            pMalloc,                            NULL,                            pStorage,                            NULL, 0, 0, &pIMsg);    // write the CLSID to the IStorage instance - pStorage. This will    // only work with clients that support this compound document type    // as the storage medium. If the client does not support    // CLSID_MailMessage as the compound document, you will have to use    // the CLSID that it does support.    hRes = WriteClassStg(pStorage, CLSID_MailMessage );    // Specify properties to exclude in the copy operation. These are    // the properties that Exchange excludes to save bits and time.    // Should not be necessary to exclude these, but speeds the process    // when a lot of messages are being copied.    excludeTags.cValues = 7;    excludeTags.aulPropTag[0] = PR_ACCESS;    excludeTags.aulPropTag[1] = PR_BODY;    excludeTags.aulPropTag[2] = PR_RTF_SYNC_BODY_COUNT;    excludeTags.aulPropTag[3] = PR_RTF_SYNC_BODY_CRC;    excludeTags.aulPropTag[4] = PR_RTF_SYNC_BODY_TAG;    excludeTags.aulPropTag[5] = PR_RTF_SYNC_PREFIX_COUNT;    excludeTags.aulPropTag[6] = PR_RTF_SYNC_TRAILING_COUNT;    // copy message properties to IMessage object opened on top of    // IStorage.    hRes = pMessage->CopyTo(0, NULL,                            (LPSPropTagArray)&excludeTags,                            NULL, NULL,                            (LPIID)&IID_IMessage,                            pIMsg, 0, NULL );    // save changes to IMessage object.    pIMsg -> SaveChanges ( KEEP_OPEN_READWRITE );    // save changes in storage of new doc file    hRes = pStorage -> Commit(STGC_DEFAULT);    // free objects and clean up memory    MAPIFreeBuffer ( lpWideCharStr );    pStorage->Release();    pIMsg->Release();    CloseIMsgSession ( pMsgSession );    pStorage = NULL;    pIMsg = NULL;    pMsgSession = NULL;    lpWideCharStr = NULL;    return hRes;}				
所有版本的 Outlook 和 Exchange 客户端为复合文档都支持 CLSID_MailMessage。若要使用不同的 CLSID 的唯一原因是支持使用不同的 CLISD 时将消息写入到结构化存储的其他客户端。

保存有大量的收件人或附件的邮件时,可以 CopyTo 操作将失败,并 MAPI_E_NOT_ENOUGH_MEMORY。这是因为结构化的存储流与一个已知问题。新附件或收件人是每次添加到邮件保存在结构化存储中,打开一个新的根存储文件。这些文件不会关闭,直到完成该事务。由于操作系统进行的同时打开根存储文件的数量限制,没有任何已知的解决方法。 有关其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
163202 Simultaeously 打开根存储的文件数的限制
这种限制也会影响所有版本的 Outlook。

警告:本文已自动翻译

属性

文章 ID:171907 - 上次审阅时间:10/15/2012 23:05:00 - 修订版本: 4.0

Microsoft Office Outlook 2007, Microsoft Messaging Application Programming Interface

  • kbinfo kbmsg kbmt KB171907 KbMtzh
反馈