在有关短信、邮件的开发中,我们经常会使用MAPI来开发,但此次在项目开发时却遇到些问题,就是首先MAPI不支持发送彩信,其次使用MAPI需要自己开发相应的UI界面,若是自己开发UI界面同时还需负责相关的设置功能界面的开发,这样的话就加大了项目成本及风险。其实,使用WindowsMobile自带的tmail.exe程序就可方便的利用其提供的短信、彩信、邮件等界面窗口来实现功能。
tmail提供一系列操作指令,利用这些指令组合就可完成相应的功能,这我已用CTmailPanel类进行了封装,应用示例如下:
TMAIL_DATA td;
td.lpTo = L"default@csdn.com";//根据设置里的默认收件人设置
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
// @n@n版权所有: reflower
// @n@n作 者 : reflower
// @n@n创建时间: 2009-9-2 9:26:49
// @n@n文件描述: 利用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
// @n@n版权所有: reflower
// @n@n作 者 : reflower
// @n@n创建时间: 2009-9-2 9:26:49
// @n@n文件描述: 利用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;
}