Most of the time, you need to send emails in the program. It is too troublesome to compile your own code!
1. Use the ShellExecute Function to save trouble:
ShellExecute (null, null, "mailto: email@263.net", null, null, sw_show );
2. If you want to handle more things by yourself, such as adding the default account, password, and attachment, you can call the mapi function of the system. For specific usage, you can check that msdn starts with mapi, such as mapilogon and mapisendmail. The following code demonstrates how to call the default mail program to send emails.
// The mapi. h header file must be included
# Include "mapi. H"
/*************************************** ******************************
* Function name: csendemaildlg: onsendmapi
* Description: Call the mapi function to send an email.
* Author: Geng
* Time: 20:08:30
**************************************** *****************************/
Void csendemaildlg: onsendmapi ()
{
Updatedata (true );
// Load the dynamic mapi32.dll Library
Hmodule hmod = loadlibrary ("mapi32.dll ");
If (hmod = NULL)
{
Afxmessagebox (afx_idp_failed_mapi_load );
Return;
}
// Obtain the email sending function address
Ulong (Pascal * lpfnsendmail) (ulong, ulong, mapimessage *, flags, ulong );
(Farproc &) lpfnsendmail = getprocaddress (hmod, "mapisendmail ");
If (lpfnsendmail = NULL)
{
Afxmessagebox (afx_idp_invalid_mapi_dll );
Return;
}
Int nfilecount = m_list.getcount (); // Number of attachments to be sent
// Allocate memory to save the attachment information. Do not use static arrays because you do not know the number of attachments to be sent.
Mapifiledesc * pfiledesc = (mapifiledesc *) malloc (sizeof (mapifiledesc) * nfilecount );
Memset (pfiledesc, 0, sizeof (mapifiledesc) * nfilecount );
// Allocate memory to save the attachment file path
Tchar * ptchpath = (tchar *) malloc (max_path * nfilecount );
Cstring sztext;
For (INT I = 0; I <nfilecount; I ++)
{
Tchar * P = ptchpath + max_path * I;
M_list.gettext (I, sztext );
Strcpy (p, sztext );
(Pfiledesc + I)-> nposition = (ulong)-1;
(Pfiledesc + I)-> lpszpathname = P;
(Pfiledesc + I)-> lpszfilename = P;
}
// Recipient structure information
Mapirecipdesc recip;
Memset (& recip, 0, sizeof (mapirecipdesc ));
Recip. lpszaddress = m_szemailmapi.getbuffer (0 );
Recip. ulrecipclass = mapi_to;
Recip. lpszname = "Your name ";
// Mail structure information
Mapimessage message;
Memset (& message, 0, sizeof (Message ));
Message. nfilecount = nfilecount; // number of files
Message. lpfiles = pfiledesc; // File Information
Message. nrecipcount = 1; // number of recipients
Message. lprecips = & recip; // recipient
Message. lpszsubject = m_szsubject.getbuffer (0); // subject
Message. lpsznotetext = m_sztext.getbuffer (0); // body content
// Save the window pointer of this program, because the window of this program should be returned after the mail is sent
Cwnd * pparentwnd = cwnd: getsafeowner (null, null );
// Send an email
Int nerror = lpfnsendmail (0, 0, & message, mapi_logon_ui | mapi_dialog, 0 );
If (nerror! = Success_success & nerror! = Mapi_user_abort
& Nerror! = Mapi_e_login_failure)
{
Afxmessagebox (afx_idp_failed_mapi_send );
}
// Return program
Pparentwnd-> setactivewindow ();
// Do not forget to release the allocated memory
Free (pfiledesc );
Free (ptchpath );
Freelibrary (hmod );
}