本文轉貼於本人blog:singlerblog.mblogger.cn
最近接了個單子,是個不大的應用程式,有一個功能是要求程式能啟動郵件發送程式,並在郵件內文部分自動填入一些圖片和文字。這方面我以前沒有做過,查了一些資料最後實現了這個功能。現在我把程式控制外部郵件發送程式的常用方法總結如下:
一、使用mailto協議
優點:使用簡單,能自動運行系統預設的郵件程式。
缺點:功能簡單,不能添加附件,也不能在郵件內文部分添加圖片,多用於web頁面中。
例子:
CString strEmailBody;
//添加收件郵箱地址
strEmailBody = "mailto:aaaa@hotmail.com;bbbbb@hotmail.com";
//添加抄送郵箱地址
strEmailBody += "&cc=ccccc@hotmail.com";
//添加密送郵箱地址
strEmailBody += "&bcc=yyyyyy@hotmail.com";
//添加郵件主題
strEmailBody += "&subject=Test";
//添加郵件內文
strEmailBody += "&body=This is the EMail body!";
//啟動郵件發送程式
::ShellExecute(m_hWnd, "Open", strEmailBody, NULL, NULL, SW_SHOW);
二、使用MAPI
優點:可以發送附件檔案,能自動運行系統預設的郵件程式。
缺點:不能在郵件內文部分添加圖片。
例子:
//需要包含的標頭檔
#include "mapi.h"
HMODULE hMapiDll = NULL;
LPMAPISENDMAIL pSendMailFunc = NULL;
//載入 MAPI dll
hMapiDll = LoadLibrary("MAPI32.DLL");
if(hMapiDll == NULL)
{
MessageBox("Load MAPI dll failure!");
return;
}
//擷取函數地址
pSendMailFunc = (LPMAPISENDMAIL)GetProcAddress(hMapiDll, "MAPISendMail");
if(pSendMailFunc == NULL)
{
MessageBox("Get the function address failure!");
return;
}
//指定附件檔案
MapiFileDesc fileDesc;
memset(&fileDesc, 0, sizeof(fileDesc));
fileDesc.nPosition = (ULONG)-1;
fileDesc.lpszPathName = "C:/test.jpg";
fileDesc.lpszFileName = "test.jpg";
//設定郵件訊息結構體
MapiMessage message;
memset(&message, 0, sizeof(message));
//附件
message.nFileCount = 1;
message.lpFiles = &fileDesc;
//郵件主題
message.lpszSubject = "Test";
//郵件內文
message.lpszNoteText = "This is the EMail body!";
//發送
int nError = pSendMailFunc(0, 0, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
if((nError != SUCCESS_SUCCESS) && (nError != MAPI_USER_ABORT) && (nError != MAPI_E_LOGIN_FAILURE))
{
MessageBox("The EMail send failure!");
}
三、使用OLE
優點:功能強大,可添加附件,也可把圖片添加到本文部分。
缺點:只能針對Outlook
例子:
//要引用的庫,Outlook版本不同,該路徑也不同
//---------------------------------------------------------------
#import "c://Program Files//Common Files//Microsoft Shared//Office10//mso.dll" named_guids
#import "C://Program Files//Microsoft Office//Office10//MSOUTL.OLB" rename_namespace("Outlook") no_dual_interfaces rename("CopyFile","_CopyFile") //exclude("_IRecipientControl", "_DRecipientControl")
//---------------------------------------------------------------
using namespace Outlook;
//---------------------------------------------------------------
if(CoInitialize(NULL) != S_OK)
return;
_ApplicationPtr pOutlookApplication = NULL;
_MailItemPtr pOutlookMailItem = NULL;
AttachmentsPtr pOulookAttachments = NULL;
AttachmentPtr pOulookAttachment = NULL;
//建立Outlook應用執行個體
HRESULT hRes = pOutlookApplication.CreateInstance("Outlook.Application");
if(hRes != S_OK)
return;
//擷取郵件
pOutlookMailItem = pOutlookApplication->CreateItem(olMailItem);
if(pOutlookMailItem == NULL)
return;
_NameSpacePtr pNameSpace = NULL;
pNameSpace = pOutlookApplication->GetNamespace(L"MAPI");
if(pNameSpace == NULL)
return;
_variant_t covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
pNameSpace->Logon(covOptional, covOptional, covOptional, covOptional);
pOulookAttachments = pOutlookMailItem->GetAttachments();
if(pOulookAttachments == NULL)
return;
//添加附件
pOulookAttachments->Add("C:/1.jpg");
pOulookAttachments->Add("C:/test.jpg");
//指定郵件內文,如果不需要顯示圖片,可以使用PutBody
pOutlookMailItem->PutHTMLBody(L"<html><body><img src='cid:1.jpg' height=200 width=200><BR>This is the EMail body!</body></html>");
pOutlookMailItem->Display();
由於我需要把圖片插入到郵件內文,所以我選擇了第三種方法。在本文部分顯示圖片,需要先把圖片作為附件添加到郵件中,然後在PutHTMLBody函數中指定圖片檔案名稱。
以上例子均在Windows XP + Office 2000 + VC6調試通過。