Write an email program with VC ++

Source: Internet
Author: User

I. Overview
---- This article describes how to use visual c ++ to write an e-mail program using mapi. Mapi is included in windows,
Therefore, no additional components need to be installed. Mapi has the following three forms:
Smapi, simple mapi, simple mapi
CMC, common messaging CILS, common communication call
Complete mapi
---- Both smapi and CMC are included in the complete mapi. When you want to perform some advanced operations, such as writing your own email server,
Complete mapi is required. This article describes how to write a program that can send and receive emails. Therefore, using smapi is sufficient.
2. Compile an email program
3-1 initialize mapi
---- To use mapi, you must first initialize it. Initialization includes the following three steps:
Load mapi32.dll Dynamic Link Library
Find the mapi function address you want to call.
Log on to the email recipient
3-1-1 Load mapi32.dll
---- To load mapi, you must dynamically load a dynamic link library when running the program. The loadlibrary function provides this function,
It locates a dynamic link library and returns the hinstance handle (this handle needs to be saved ).
The loadlibrary syntax is as follows:
Loadlibrary (lplibfilename );
Here, lplibfilename is the structure variable of the lpctstr,
Is the path and name of the library to be called.
Program example:
// Call mapi32.dll and calculate the function address
Hinstance hinstmail;
Hinstmail =: loadlibrary ("mapi32.dll ");
If (hinstmail = NULL)
{
// Handle errors
// Due to space limitations, the error handling section below is omitted
}
3-1-2 determine the function address
---- Because mapi32.dll is dynamically loaded, you cannot call them at the beginning without knowing the address of the function to be called,
Instead, you need to obtain the function address through the function name, search for each function in the dynamic link library, and verify it. Therefore, the pointer must be declared for these functions.

Program example:
// It Is the function declaration pointer in mapi32.dll.
Ulong (Pascal * lpfnmapisendmail) (lhandle lhsession,
Ulong uluiparam, lpmapimessage lpmessage,
Flags flflags, ulong ulreserved );
Ulong (Pascal * lpfnmapiresolvename) (lhandle lhsession,
Ulong uluiparam, lptstr lpszname,
Flags ulflags, ulong ulreserved,
Lpmapirecipdesc far * lpprecip );
Ulong (far Pascal * lpfnmapilogon) (ulong uluiparam,
Lpstr lpszprofilename, lpstr lpszpassword,
Flags flflags, ulong ulreserved,
Lplhandle lplhsession );
Ulong (far Pascal * lpfnmapilogoff) (lhandle lhsession,
Ulong uluiparam, flags flflags,
Ulong ulreserved );
Ulong (far Pascal * lpfnmapifreebuffer) (lpvoid lpbuffer );
Ulong (far Pascal * lpfnmapiaddress) (lhandle lhsession,
Ulong uluiparam, lpstr lpszcaption,
Ulong neditfields, lpstr lpszlabels,
Ulong nrecips, lpmapirecipdesc lprecips,
Flags flflags, ulong ulreserved,
Lpulong lpnnewrecips,
Lpmapirecipdesc far * lppnewrecips );
Ulong (far Pascal * lpfnmapifindnext) (lhandle lhsession,
Ulong uluiparam, lpstr lpszmessagetype,
Lpstr lpszseedmessageid, flags flflags,
Ulong ulreserved, lpstr lpszmessageid );
Ulong (far Pascal * lpfnmapireadmail) (lhandle lhsession,
Ulong uluiparam, lpstr lpszmessageid,
Flags flflags, ulong ulreserved,
Lpmapimessage far * lppmessage );
---- To determine the address of each function, you must call getprocaddress for each function.
The getprocaddress syntax is:
Getprocaddress (hmodule, lpprocname );
Here, hmodule is the hmodule structure, which is the handle of the called DLL module;
The lpprocname is a function name in the left-side navigation pane.
Program example:
// Find the address of the mapi32.dll function and save them in the function pointer variable.
(Farproc &) lpfnmapisendmail = getprocaddress (hinstmail,
"Mapisendmail ");
(Farproc &) lpfnmapiresolvename = getprocaddress (
Hinstmail, "mapiresolvename ");
(Farproc &) lpfnmapilogon = getprocaddress (hinstmail,
"Mapilogon ");

(Farproc &) lpfnmapilogoff = getprocaddress (hinstmail,
"Mapilogoff ");
(Farproc &) lpfnmapifreebuffer = getprocaddress (
Hinstmail, "mapifreebuffer ");
(Farproc &) lpfnmapiaddress = getprocaddress (hinstmail,
"Mapiaddress ");
(Farproc &) lpfnmapifindnext = getprocaddress (hinstmail,
"Mapifindnext ");
(Farproc &) lpfnmapireadmail = getprocaddress (hinstmail,
"Mapireadmail ");
3-1-3 log on to the email recipient
---- You must log on to the email system to implement various mapi functions. Mapi provides three logon options:
Log on to an existing object.
Log on to a new object and use a programming method to explain the new information.
The dialog box prompts you to log on.
---- We usually choose to log on to an existing email object, because the network partner users usually keep their own
The email program is activated. Log on to the lpfnmapilogon function provided by mapi.
The syntax of lpfnmapilogon is:
Lpfnmapilogon (lpszprofilename, lpszpassword, flflags,
Ulreserved, lplhsession );
---- Here, lpszprofilename points to a login name of less than 256 characters, and lpszpassword points to the password.
Is the lptstr structure. Flflags is a flags structure. For the value, see table 1. Ulreserved must be 0. Lplhsession is the handle of the output smapi.
Table 1: flflags values in the lpfnmapilogon Function
Value Meaning
Mapi_force_download
Download all emails of the user before the function call is returned.
If mapi_force_download is not set,
Then the letter can be downloaded in the background after the function call returns.
Mapi_new_session creates a new session,
Instead of obtaining the shared session of the environment. If mapi_new_session is not set,
Mapilogon uses an existing shared session.
Mapi_logon_ui displays a logon dialog box prompting you to enter logon information.
For example, Outlook checks users' emails.
Mapi_password_ui mapilogon only allows the user to enter the email password,
You are not allowed to change your account.
Program example:
Lhandle lhsession;
Ulong lresult = lpfnmapilogon (0, null, null, 0, 0,

& Lhsession );
If (lresult! = Success_success)
// Success_success is defined in mapi. h.
{
// Handle errors
}
3-2 read emails
---- Mapifindnext and mapireadmail use and read the two basic functions of e-mail. Mapifindnext is used to locate the first
Emails or the next email with the ID number returned. mapireadmail returns the content of the email with the ID number as the base. In addition,
A common function is mapifreebuffer, which is used to release memory.
3-2-1 locate the first region
---- To locate the first vertex, use the mapifindnext function. The function declaration is as follows:
Ulong far Pascal mapifindnext (lhandle lhsession,
Ulong uluiparam, lptstr lpszmessagetype,
Lptstr lpszseedmessageid, flags flflags,
Ulong ulreserved, lptstr lpszmessageid)
---- Here, lhsession is the session handle for submitting smapi; uluiparam is the parent form handle; lpszmessagetype
Point to a string to identify the mail type and search for it. lpszseedmessageid is the pointer to the start information Id,
When the value is 0, mapifindnext gets the first email; The flflags value is shown in table 2; The ulreserved value must be 0;
Lpszmessageid is the output value, which is a pointer to the Information ID address.
---- Table 2: flflags value in mapifindnext Function
Value Meaning
Mapi_guarantee_fifo receives emails in the order of sending time.
Mapi_long_msgid the returned letter identifier can contain up to 512 characters.
Mapi_unread_only lists emails that have not been read.
Program example:
// Find the First unread email
Char pmessageid [513];
Ulong lresult = lpfnmapifindnext (lhsession, null, null,
Null, mapi_long_msgid | mapi_unread_only,
0, pmessageid );
3-2-2 read information
After the mail ID is obtained, you can call mapireadmail
Read the actual email information. The function declaration of mapireadmail is as follows:
Ulong far Pascal mapireadmail (lhandle lhsession,
Ulong uluiparam, lptstr lpszmessageid,
Flags flflags, ulong ulreserved,
Lpmapimessage far * lppmessage );

Here, lppmessage is the pointer to mapimessage;
Other parameters except flags have the same meaning as those of the lpfnfindnext function,
The flflags parameter values are shown in table 3:
Table 3: flflags values in the mapireadmail function:
Value Meaning
Mapi_body_as_file writes the email information to a temporary file,
And add it as the first attachment to the Attachment List.
Mapi_envelope_only reads only the mail title.
Mapi_peek does not mark it as "read" after reading the email ".
Mapi_suppress_attach the mapireadmail function does not copy attachments,
However, the mail text is written into the mapimessage structure.
Program example:
// Read the email
Long nflags = mapi_suppress_attach;
If (! Bmarkasread)
Nflags = nflags | mapi_peek;
Lresult = lpfnmapireadmail (lhsession, null, pmessageid,
Nflags, 0, & pmessage );
If (lresult! = Success_success );
Return false;
If the call is successful, you can access the mapimessage structure (using pmessage ):
Pmessage-> ulreserved: 0
Pmessage-> lpszsubject: Mail title
Pmessage-> lpsznotetext: email information
Pmessage-> lpszmessagetype: Mail type
Pmessage-> datereceived: receipt time
Pmessage-> lpszconversationid: The session thread ID of the email.
Pmessage-> flflags: The values are shown in table 4.
Table 4: flflags in the mapimessage Structure
Value Meaning
Mapi_receipt_requested is requested to receive the notification.
The client application sets this item when sending a message.
The mapi_sent email has been sent.
The mapi_unread email is in the "unread" status.
Pmessage-> lporiginator: point to the mapirecipdesc structure, which contains the sender information.
Pmessage-> nrecipcount: Number of recipients.
Pmessage-> lprecips: point to the mapirecipdesc structure array, which contains the receiver information.
Pmessage-> nfilecount: Number of attachments.
Pmessage-> lpfiles: point to the mapifiledesc structure array,
Each structure contains a file attachment.

3-2-3 release memory
---- The memory should be released before accessing another letter; otherwise, memory leakage may occur.
Program example:
// Release the memory
Lpfnmapifreebuffer (pmessage );
3-2-4 locate the next mail
Locate the next letter and still use the mapifindnext function,
For more information about the function declaration and parameter meanings, see section 3-2-1. The following shows how to locate the next mail. Program example:
// Locate the next unread letter
Ulong lresult = lpfnmapifindnext (lhsession, null, null,
Pmessageid, mapi_long_msgid | mapi_unread_only,
0, pmessageid );
3-3 Send email
---- General steps for sending an email:
---- 1. Create a mapimessage structure object
---- 2. Call mapiresolvename to make the sender name legal
---- 3. Add attachment
---- 4. Call mapisendmail to send an email
---- 5. Call mapifreebuffer to release memory
---- The following describes in detail.
3-3-1 create a mapimessage structure object
---- For the mapimessage structure, we have introduced 3-2-2. The following describes how to set the values step by step:
---- 1. allocate memory for the mapimessage object:
Mapimessage message;
Memset (& message, 0, sizeof (Message ));
---- 2. Set ulreserved to 0:
Message. ulreserved = 0;
---- 3. Set the information type pointer lpszmessagetype, which can be null:
Message. lpszmessagetype = NULL;

---- 4. Set the letter title (lpszsubject ):
Char subject [512];
Strcpy (subject, ssubject );
Message. lpszsubject = subject;
---- 5. Set the mail content:
Char text [5000];
Strcpy (text, smessage );
Message. lpsznotetext = text;
---- 6. Set the flflags. For details, see table 4 in section 3-2-2:
Message. flflags = mapi_sent;
---- 7. Use a pointer to the mapirecipdesc structure to set the sender information (lporiginator) or set it to NULL:
Message. lporiginator = NULL;
---- 8. Set the number of recipients (nrecipcount), which can be 1 or more:
Message. nrecipcount = 1;
---- 9. Set the receiver information (lprecips). For details, see section 3-3-2.
---- 10. Set the number of attachments (nfilecount)
---- 11. Set the attachment information. For details, see section 3-3-3.
B3-3-2 correctly sets Recipient Information
---- When setting the receiver information, you should use the mapiresolvename function to allocate memory for the mapirecipdesc structure object,
Returns a pointer, Which is saved in the lprecips of the mapimessage structure. The function declaration of mapiresolvename is as follows:
Ulong far Pascal mapiresolvename (lhandle lhsession,
Ulong uluiparam, lptstr lpszname,
Flags flflags, ulong ulreserved,
Lpmapirecipdesc far * lpprecip)
---- Here, lpprecip is the pointer returned previously. Other parameters except flags have the same meaning as the previous functions.
For flflags values, see table 5.
Table 5: flflags values in mapiresolvename
Value Meaning
The mapi_ AB _nomodify dialog box is read-only. If mapi_dialog is set,
This option is ignored.

Mapi_dialog display a name solution dialog box
If you need mapi_logon_ui, a dialog box is displayed to log on to the console.
Mapi_new_session creates a session
Program example:
Char recipient [512];
Strcpy (recipient, STO );
Lresult = lpfnmapiresolvename (lhsession, 0, recipient,
0, 0, & message. lprecips );
3-3-3 add attachments
---- The following program example shows how to include attachments in an email.
There is only one note: The flflags value in the mapifiledesc structure. For details, see table 6.
Table 6: flflags values in the mapifiledesc Structure
Value Meaning
Mapi_ole attachment is an OLE object.
Mapi_ole_static attachment is a static OLE object.
0 attachments will be treated as data files
Program example:
// Set the Attachment Information
Cstring Spath, sfilename;
Mapifiledesc fileinfo;
Char path [512];
Char filename [512];
If (sattachment = "")
Message. nfilecount = 0;
Else
{
Int NPOs = sattachment. reversefind ('\\');
If (NPOs =-1)
{
Spath = sattachment;
}
Else
{
Spath = sattachment;
Sfilename = sattachment. mid (NPOs + 1 );
}
Strcpy (path, Spath );
Strcpy (filename, sfilename );
Message. nfilecount = 1;
Fileinfo. ulreserved = 0;
Fileinfo. flflags = 0;
Fileinfo. nposition = smessage. getlength ()-1;
Fileinfo. lpszpathname = path;
Fileinfo. lpszfilename = filename;
Fileinfo. lpfiletype = NULL;
Message. lpfiles = & m_fileinfo;
}
3-3-4 send email
---- Use mapisendmail to send an email. Its statement is as follows:
Ulong far Pascal mapisendmail (lhandle lhsession,
Ulong uluiparam, lpmapimessage lpmessage,
Flags flflags, ulong ulreserved)
---- The allowed flflags values include mapi_dialog, mapi_logon_ui, and mapi_new_session,
Its meaning is the same as that in the previous functions.
Program example:
Lresult = lpfnmapisendmail (0, 0, & m_message, 0, 0 );
3-3-5 release memory
Program example:
Lpfnmapifreebuffer (m_message.lprecips );
Iv. Summary
---- This article gives a detailed introduction and demonstrates the core part of writing an email program,
If you want to write an email program, you still need to handle it:
---- 1. Add the error handling code. Due to space limitations, only two errors are left blank in the program example in this article,
Compare their similarities and differences. Email programs are very error-prone, So in addition to the two, the main function
After the call is complete, add error processing or use try throw Catch Block to process exceptions.
---- 2. Add the UI processing.
---- In addition, the method described in this article is relatively simple. In fact, the email-related procedures are far more complex than this,
Therefore, if you need to write a powerful email program, you must be proficient in protocols such as mapi and SMTP/POP3;
If you want to write an email server,
Read some information about Exchange Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.