Call the default email program in the VC to send an email

xiaoxiao2021-03-06  36

Many times everyone need to send emails in the program, I have a lot of trouble, what to do, huh, huh, there is ready-made!

1. If you want to save things, use the shellexecute function:

Shellexecute (Null, Null, "Mailto: Email@263.net", NULL, NULL, SW_SHOW;

2, if you want to process some things, such as adding the default account, password, attachment, etc., you can call the MAPI function of the system. Specific usage You can check that MSDN is starting with MAPI, such as Mapilogon, MapISendmail et al. The following code demonstrates how to call the default mail program to send an email.

#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 t he frame to undo the workaround // done before the MAPI call.if {AfxMessageBox (AFX_IDP_FAILED_MAPI_SEND);} (nError = SUCCESS_SUCCESS && nError = MAPI_USER_ABORT && nError = MAPI_E_LOGIN_FAILURE!!!)}

转载请注明原文地址:https://www.9cbs.com/read-80705.html

New Post(0)