在VC中呼叫預設的電子郵件程式傳送郵件 (轉)

worldblog發表於2007-12-09
在VC中呼叫預設的電子郵件程式傳送郵件 (轉)[@more@]

很多時候大家需要在中傳送,自己編又太麻煩,怎麼辦,呵呵,有現成的!


1、想省事兒的,用Execute:


ShellExecute(to:NULL,NULL,"mailto:@263",NULL,NULL,SW_SHOW'>NULL,NULL,"mailto:email@263.net",NULL,NULL,SW_SHOW);


2、如果想自己多處理一些東西的話,比如加上預設的帳號、密碼、附件等,就可以的M函式。具體的用法大家可以去查MSDN都是以MAPI開頭的,如MAPILogon、MAPI等。下面這段程式碼演示如何呼叫預設的郵件程式傳送郵件。


#include "mapi.h" 

void CTestMapiDlg::OnSendMail()
{
HMODULE hMod = LoadLibrary("MAPI32.DLL");

if (hMod == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_MAPI_LOAD);
return;
}

ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
(FROC&)lpfnSendMail = GetProcAddress(hMod, "MAPISendMail");

if (lpfnSendMail == NULL)
{
AfxMessageBox(AFX_IDP_INVALID_MAPI_DLL);
return;
}

ASSERT(lpfnSendMail != NULL);

TCHAR szPath[_MAX_PATH] = "C:Winntsetup.log";
TCHAR szTitle[_MAX_PATH] = "setup.log";

// prepare the file description (for the attachment)
MapiFileDesc fileDesc;
memset(&fileDesc, 0, sizeof(fileDesc));
fileDesc.nPosition = (ULONG)-1;

fileDesc.lpszPathName = szPath;
fileDesc.lpszFileName = szTitle;

// prepare the message (empty with 1 attachment)
MapiMessage message;
memset(&message, 0, sizeof(message));
message.nFileCount = 1;
message.lpFiles = &fileDesc;

int nError = lpfnSendMail(0, 0,
&message, MAPI_LOGON_UI¦MAPI_DIALOG, 0);

// after returning from the MAPISendMail call, the window must
// be re-enabled and focus returned to the frame to undo the workaround
// done before the MAPI call.
if (nError != SUCCESS_SUCCESS &&
nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
{
  AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
}
}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-990587/,如需轉載,請註明出處,否則將追究法律責任。

相關文章