在VC中調用預設的電子郵件程式發送郵件

來源:互聯網
上載者:User
程式|發送郵件

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



1、想省事兒的,用ShellExecute函數:



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




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



#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);
(FARPROC&)lpfnSendMail = GetProcAddress(hMod, "MAPISendMail");

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

ASSERT(lpfnSendMail != NULL);

TCHAR szPath[_MAX_PATH] = "C:\Winnt\setup.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);
}
}





相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。