65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Microsoft Windows CE Mail API 发送邮件

2001年11月22日

CPOL

2分钟阅读

viewsIcon

150525

downloadIcon

1

如何使用 Microsoft Mail API 将消息插入到 Windows CE 发件箱。

前言

请注意,本文档描述的是在 Pocket PC 2000 及更早版本平台上开发邮件软件。Pocket 2002 已经显著改变了操作系统处理邮件的方式。建议您阅读 Pocket PC Developers Network 上的相关文章。

引言

Windows CE 作为核心操作系统的一部分提供了电子邮件存储支持(即包含在 ROM 芯片上)。Microsoft 在库 'msgstore.lib' 中提供了一系列 API,它称之为 Microsoft Mail API 或 MAPI。这些 API 允许开发者操作系统的电子邮件文件夹。虽然这些 API 提供了操作系统电子邮件文件夹及其内部电子邮件的功能,但尚未提供发送和接收电子邮件消息的支持。Microsoft 认为,提供这些服务的责任在于电子邮件客户端的开发者。

//Includes
#include "msgstor2.h"
#include "msgstore.h"


CDummyMailClass::SendMail(CString csReciepent, CString csSubject, 
                          CString csBodyText, CString csAttachment)
{
    CTime      p_CurrentTime    = CTime::GetCurrentTime();
    SYSTEMTIME p_CurrentSystemTime;
    HANDLE     p_hMail          = NULL;    //This holds a Handle to our mail context
    BOOL       p_bReturn        = FALSE;
    MailMsg    p_MailMsg;


    //Open the mail context and return a handle to it
    p_bReturn = MailOpen(&p_hMail, TRUE)

        ASSERT(p_bReturn);

    if(!p_bReturn)
    {
        RptMailError(p_hMail);
    }

    //
    p_CurrentTime.GetAsSystemTime(p_CurrentSystemTime);

    memset(&p_MailMsg,0,sizeof(MailMsg));

    //A new message ID will assigned by the system when we call MsgPut
    p_MailMsg.dwMsgId=0;

    //Indicate that the mail is to be placed into the Outbox and its status 
    //is composed
    p_MailMsg.dwFlags=MAIL_FOLDER_OUTBOX | MAIL_STATUS_COMPOSED; 

    //Set up the time of sending as the current system time
    SystemTimeToFileTime(&p_CurrentSystemTime,&p_MailMsg.ftDate);

    //The service name as specify by msdn
    p_MailMsg.szSvcNam = _T("Windows CE Inbox Services");

    //The Messages body
    p_MailMsg.szBody = (LPTSTR) (LPCTSTR) csBodyText;


    p_bReturn = MailSetField ( &p_MailMsg, _T( "To" ), 
        (LPTSTR) (LPCTSTR) csReciepent );

    ASSERT(p_bReturn);

    p_bReturn = MailSetField ( &p_MailMsg, _T( "Subject" ),
        (LPTSTR) (LPCTSTR)  csSubject);

    ASSERT(p_bReturn);

    //Put the mail messages into the outgoing folder

    p_bReturn = MailPut ( p_hMail, &p_MailMsg);

    ASSERT(p_bReturn);


    //Close the handle to the open Mail Context, releasing the associated 
    //resources
    BOOL p_bReturn = MailClose(&p_hMail);

    ASSERT(p_bReturn);

}

好的,我们已经提供了一些基本功能。然而,不明显的是,上面的代码会将它创建的任何消息放入 Activesync 发件箱。在 Windows CE 下,邮件文件夹是分层的,标准的邮件文件夹;收件箱、发件箱、已删除邮件等,都创建在根文件夹之下。每当邮件客户端(在大多数情况下是 Microsoft Outlook)配置新的传输方式时,它也会创建一个新的根文件夹和子文件夹来保存该传输方式的消息。新安装的 Windows CE 系统已经安装并配置了 ActiveSync 传输方式(通过桌面电脑发送和接收邮件),然后用户可以配置其他传输方式,例如 IMAP 或 SMTP。因此,假设我们的应用程序需要通过简单邮件传输协议 (SMTP) 发送邮件,因此所有新消息都需要放置在 SMTP “发件箱”中。

FID    p_ParentFolderId;
FID    p_FolderId;

//Get The Folder Id of the Root Folder for the SMTP transport
p_bReturn = MailGetFolderIdEx(p_hMail, MAIL_FOLDER_NONE, 
                              &p_ParentFolderId, _T("POP3 Mail"));

ASSERT(p_bReturn);

//Get the Folder Id of the Child Folder Outbox
p_bReturn = MailGetFolderIdEx(p_hMail, p_ParentFolderId, &p_FolderId, 
                              _T("Outbox"));

ASSERT(p_bReturn);

//Specify the into folder which the new mail is be put.
p_bReturn = MailPutEx ( p_hMail, &p_MailMsg, p_FolderId );

ASSERT(p_bReturn);

所以让我们添加一些更多的功能。应用程序通常希望将文件附加到他们发送的电子邮件中。请注意,您附加到电子邮件的任何文件都会被 API 自动删除

MailAtt p_MailAttachment;
BOOL    p_bReturn;

memset(&p_MailAttachment, 0, sizeof(MailAtt));

// Setup the Mail Attachment Structure
//This must be incremented for every new attachment to a mail
p_MailAttachment.uiAttachmentNumber = 1;
p_MailAttachment.dwFlags = NULL;
// Not needed since we are sending a local file
p_MailAttachment.ulSize = NULL;
//The Orignal File Name (the file without the path)
p_MailAttachment.szOriginalName = (LPTSTR) (LPCTSTR) p_csFileName;
//The full path to the file
p_MailAttachment.szLocalName = (LPTSTR) (LPCTSTR) csAttachment;

p_bReturn = MailPutAttachment(p_hMail, &p_MailMsg, &p_MailAttachment);

ASSERT(p_bReturn);

最后说一下错误处理。Mail API 还提供了相当全面的错误信息,可以通过 MailError* API 调用访问这些信息。

INT        p_iBufferLength    = 100;
INT        p_iLineNumber    = 0;
CString    p_csErrorMsg;

MailErrorMsg (p_hMail , p_csErrorMsg.GetBuffer(p_iBufferLength), 
              p_iBufferLength, &p_iLineNumber);

p_csErrorMsg.ReleaseBuffer();

AfxMessageBox(p_csErrorMsg, MB_OK|MB_ICONSTOP);

有关更多信息,请参阅 MSDN 文章 使用 Microsoft Windows CE Mail API 。正如我所说,Mail API 的局限性在于它实际上并不发送电子邮件。如果需要此功能,ROM 上提供了一个 'smtp.dll',并由 'Outlook' 用于通过 SMTP 发送电子邮件。Microsoft 没有提供有关此库的信息,但是它的导出在头文件 'msgxport.h' 中列出。 Ray Kinsella

如有任何问题或疑问,请随时通过以下方式与我联系:

© . All rights reserved.