在有關短信、郵件的開發中,我們經常會使用MAPI來開發,但此次在項目開發時卻遇到些問題,就是首先MAPI不支援發送彩信,其次使用MAPI需要自己開發相應的UI界面,若是自己開發UI界面同時還需負責相關的設定功能界面的開發,這樣的話就加大了項目成本及風險。其實,使用WindowsMobile自帶的tmail.exe程式就可友善的利用其提供的短信、彩信、郵件等界面視窗來實作功能。
tmail提供一系列操作指令,利用這些指令組合就可完成相應的功能,這我已用CTmailPanel類進行了封裝,應用示例如下:
TMAIL_DATA td;
td.lpTo = L"[email protected]";//根據設定裡的預設收件人設定
td.lpSubject = L"測試";//主題
td.lpBody = L"測試文本内容";//資訊内容
td.lpAttach = L"/My picture/test.jpg";//附件
if (TMAIL_SUCCESS != CTmailPanel::SendEmail(td))
{
MessageBox(_T("發送失敗!"));
}
附錄檔案:
CTmailPanel.h
///
//
// @file
// @brief
//
// 檔案名 : TmailPanel.h
// @[email protected]版權所有: reflower
// @[email protected]作 者 : reflower
// @[email protected]建立時間: 2009-9-2 9:26:49
// @[email protected]檔案描述: 利用PPC下的tmail.exe程式進行短信、彩信及郵件的發送(彈出使用者操作界面)
// @version 版本 修改者 時間 描述@n
// @n reflower 2009-09-02
//
///
#pragma once
#ifndef _TMAILPANEL_H_
#define _TMAILPANEL_H_
typedef enum _TMAIL_ERROR
{
TMAIL_SUCCESS = 0,
TMAIL_PARAMETER_ERROR,
}TMAIL_ERROR;
typedef struct _TAMIL_STRUCT
{
LPCTSTR lpTo;//接收人(郵箱或手機号,多個收件人以分号隔開)
LPCTSTR lpCc;//抄送人,僅支援郵件發送,多個收件人以分号隔開
LPCTSTR lpBcc;//密件抄送人,僅支援郵件發送,多個收件人以分号隔開
LPCTSTR lpSubject;//主題
LPCTSTR lpBody;//文本内容
LPCTSTR lpAttach;//附件,全路徑
_TAMIL_STRUCT()
{
lpTo = NULL;
lpCc = NULL;
lpBcc = NULL;
lpSubject = NULL;
lpBody = NULL;
lpAttach = NULL;
}
}TMAIL_DATA;
class CTmailPanel
{
public:
CTmailPanel(void);
~CTmailPanel(void);
public:
//發送郵件
static TMAIL_ERROR SendEmail(TMAIL_DATA &tData);
//發送短信
static TMAIL_ERROR SendSMS(TMAIL_DATA &tData);
//發送彩信
static TMAIL_ERROR SendMMS(TMAIL_DATA &tData);
private:
static BOOL Run(CString strParameter);
};
#endif
CTmailPanel.cpp
///
//
// @file
// @brief
//
// 檔案名 : TmailPanel.cpp
// @[email protected]版權所有: reflower
// @[email protected]作 者 : reflower
// @[email protected]建立時間: 2009-9-2 9:26:49
// @[email protected]檔案描述: 利用PPC下的tmail.exe程式進行短信、彩信及郵件的發送(彈出使用者操作界面)
// @version 版本 修改者 時間 描述@n
// @n reflower 2009-09-02
//
///
#include "StdAfx.h"
#include "TmailPanel.h"
CTmailPanel::CTmailPanel(void)
{
}
CTmailPanel::~CTmailPanel(void)
{
}
//發送郵件
TMAIL_ERROR CTmailPanel::SendEmail(TMAIL_DATA &tData)
{
if (tData.lpTo == NULL
&& tData.lpCc == NULL
&& tData.lpBcc == NULL)
{
return TMAIL_PARAMETER_ERROR;
}
CString strContent,strTmp,strFormat;
strContent = _T("-service /"ActiveSync/"");
if (tData.lpTo != NULL)
{
strTmp = tData.lpTo;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -to /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpCc != NULL)
{
strTmp = tData.lpCc;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -cc /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpBcc != NULL)
{
strTmp = tData.lpBcc;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -bcc /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpSubject != NULL)
{
strTmp = tData.lpSubject;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -subject /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpBody != NULL)
{
strTmp = tData.lpBody;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -body /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpAttach != NULL)
{
strTmp = tData.lpAttach;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -attach /"%s/""),strTmp);
strContent += strFormat;
}
}
if (Run(strContent))
{
return TMAIL_SUCCESS;
}
else
{
DWORD deError = GetLastError();
return (TMAIL_ERROR)deError;
}
return TMAIL_SUCCESS;
}
//發送短信
TMAIL_ERROR CTmailPanel::SendSMS(TMAIL_DATA &tData)
{
if (tData.lpTo == NULL)
{
return TMAIL_PARAMETER_ERROR;
}
CString strContent,strTmp,strFormat;
strContent = _T("-service /"SMS/"");
if (tData.lpTo != NULL)
{
strTmp = tData.lpTo;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -to /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpSubject != NULL)
{
strTmp = tData.lpSubject;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -subject /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpBody != NULL)
{
strTmp = tData.lpBody;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -body /"%s/""),strTmp);
strContent += strFormat;
}
}
if (Run(strContent))
{
return TMAIL_SUCCESS;
}
else
{
DWORD deError = GetLastError();
return (TMAIL_ERROR)deError;
}
return TMAIL_SUCCESS;
}
//發送彩信
TMAIL_ERROR CTmailPanel::SendMMS(TMAIL_DATA &tData)
{
if (tData.lpTo == NULL)
{
return TMAIL_PARAMETER_ERROR;
}
CString strContent,strTmp,strFormat;
strContent = _T("-service /"MMS/"");
if (tData.lpTo != NULL)
{
strTmp = tData.lpTo;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -to /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpSubject != NULL)
{
strTmp = tData.lpSubject;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -subject /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpBody != NULL)
{
strTmp = tData.lpBody;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -body /"%s/""),strTmp);
strContent += strFormat;
}
}
if (tData.lpAttach != NULL)
{
strTmp = tData.lpAttach;
if (!strTmp.IsEmpty())
{
strTmp.Replace(_T("/""),_T("//""));
strFormat.Format(_T(" -attach /"%s/""),strTmp);
strContent += strFormat;
}
}
if (Run(strContent))
{
return TMAIL_SUCCESS;
}
else
{
DWORD deError = GetLastError();
return (TMAIL_ERROR)deError;
}
}
BOOL CTmailPanel::Run(CString strParameter)
{
SHELLEXECUTEINFO lpExecInfo = {0};
lpExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
lpExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
lpExecInfo.lpVerb = L"Open";
lpExecInfo.hwnd = NULL;
lpExecInfo.lpFile = L"tmail.exe";
lpExecInfo.lpDirectory = L"/windows";
lpExecInfo.nShow = SW_SHOW;
lpExecInfo.lpParameters = strParameter.GetBuffer();
lpExecInfo.hInstApp = NULL;
BOOL bRet = ShellExecuteEx(&lpExecInfo);
WaitForSingleObject(lpExecInfo.hProcess,INFINITE);
strParameter.ReleaseBuffer();
return bRet;
}