Implement mail delivery using MAPI
Original: Deltacat Creation: 2004.09.16 Last Update: 2004.11.01 Copyright Notice: Author preserves copyright. Please keep the documentation and the integrity of the documentation when reprint.
Keywords: Visual C Mail Send Simple Mapi
First, the problem is proposed
We often need to add mail support in our application, mainly to send mail. Currently, common methods are called external programs, programming directly to SMTP protocol, or using the MAPI interface. MAPI is a set of interfaces provided by Microsoft for email. Open MSDN Library, you can find it under Messaging and Collaboration Services. MAPI uses complicated, so Microsoft provides several simple solutions, including CMC (Common Messaging Calls) and Simple Mapi.
This paper mainly describes a simple implementation of using Simple Mapi to send a message for sending an accessory. There is a small piece after the article describes the development of the complete MAPI. For some reason, it is not recommended. If you need more complex features, I think it is still good to encapsulate SMTP and POP3. Second, Simple Mapi Introduction
Simple Mapi includes a set of functions and related data structures, which can be used in multiple languages such as C / C , VB. This article is based on VC design. Implement a complete mail sending process, actually only one function "MapISendmail ()", full function table and related introduction to the MSDN Library-> Platform SDK-> Messaging and Collaboration Services. MAPI is closely related to the mail system. You must use the MAPI function. The system must have a mail system that supports MAPI, such as Outlook, Outlook Express, Eudora, Netscape, etc. Here, you have to mention it, use very widely foxmail (5.0 version) ) Does not seem to support MAPI, but there is no in-depth study. If any master discovery is actually supported, please inform me how to do it. Windows provides a file mapi.h that contains all relevant data types definitions.
Third, function design
I need to achieve the following features: Customizable emails, including at least one recipient, optional items have titles, body, several copies, porters, attachments. Automatic transmission (no user intervention) can be selected as needed, or pop-up the mail writing window. For ease of use and expansion, I use a class to implement.
1, the initialization of the MAPI library
Including two steps: load the MAPI library to get the function entry address.
/ / -------------------------------------------------------------------------------------------- ------------------------------------ m_hlibrary = :: loadLibrary (_t ("mapi32.dll" )); If (null == m_hlibrary) {return :: getLastError ();} // lpMapIndmail, etc. All define in
Sending a mail function is to encapsulate MapISendmail (). The parameter definition of this API function is explained below.
Ulong Far Pascal Mapisndmail (LHANDLE LHSESSION, ULONG ULUIPARAM, LPMAPIMESSAGE LPMESSAGE, FLAGS FLFLAGS, ULONG ULRESERVED);
Uluiparam can be 0, if the Mapi_Dialog flag is set, it is best to pass it to its parent window handle. LHSESSION and ULRESERVED two parameters, it is possible to set it to 0. Flflags has three effective bit, named Mapi_Dialog, Mapi_logon_ui, Mapi_new_Session. If you want the program to pop up a standard mail writing dialog, set MAPI_DIALOG, it is recommended not to set MAPI_Logon_UI and MAPI_New_Session. The focus is the lpMessage parameter, which points to a Mapimestage type structure, which defines all the information of a message.
3, Structure Mapimessage
First define a variable of a MapiMessage type when you use it. Now we simply set lpszsubject (title), LPSZNOTETEXT, LPSZNOTETEXT, LPORIGINATOR, NRECIPCOUNT (Recipient Count, including TO, CC, GCC), LPRCIPS (a group containing all recipients) , NFileCount, LPFiles (incorporative array containing each attachment). NRECIPCOUNT and NFILECOUNT values must be consistent with the actual number of recipients and attachments.
4, Structure MapiRecipdesc
UlrecipClass, LPSZNAME, LPSZADDRESS
5, Structure MapIfileDesc
There must be set with lpszpathname, nposiotion two parameters there is a place to pay special attention. In the actual coding process, several Outlook can't pop up the window (automatically send it automatically), but the same process, the mail customer is set to OutlooKexpress. There is no problem. A headache. After repeated inspection, this parameters of nposiotion were found. It indicates the location of the attachment in the message. For some mail clients, such as OutlookExpress, this parameter may be ignored, and the client is automatically arranged. So there is no problem. For other clients, such as Outlook, it is always scheduled for this value. If the number of attachments is more than one, this value is if the value is the same, then it will cause an error. But it is very troublesome and there is no significance of your own calculation. The solution is to set it to -1, indicating that the customer's software is arranged. :) Four, how to work?
Mapisendmail () calls the system default mail client to send an email. For the way of pop-up editing windows, its behavior is similar to another function mapidocuments (), but you can customize the title, recipient, and more. MaPisEndDocuments () just simply prepares a blank message (including attachments), referring to MSDN. For automatic transmission. Need to cancel "Warning" when there are other applications attempt to send mail when other applications tries to send mail. For Outlook, it seems that the SIMPLE MAPI has not yet prohibited the safety warning.
The class I designed includes three interface functions, send (), addfiles (), addrecips (), only send () is required. Declare an object when using it, then call the send () function to send an email. Two add functions can be called before Send () as needed.
Five, legacy issues
My development environment is Windows XP SP2 CHS, there are several questions, I hope some masters can be solved.
1. Warning issues when automatically sent. Can I solve using Simple MAPI? 2, the default Outlook Express is the system mail client, you can send it immediately. If it is Outlook, you will be put on the Outlook send queue, then if Outlook is not running, it will not be sent. How can I guarantee whether or not the mail customer software is running, I can send the email immediately. 3, for the default "Hotmail", only the account ending with @ Hotmail.com can be sent normally, and @ msn.com can not. At the same time, when sending, Hotmail will change the attachment on the hard disk (add "^" symbol on the end and change the file attribute to read-only, but if it is uploaded, the file will be restored.
Six, use MAPI
The reason why MAPI is used, initially in order to obtain the address of the system and the problem 2 above the ending. Mapi is more complicated relative to Simple MAPI. Specific use methods can refer to the MSDN document "Handling An Outgoing Message", "Handling The Address Book, and" Sending or Receiving A Message On Demand ". Here is only a few key issues.
1, only Microsoft Outlook supports MAPI, including Microsoft Outlook Express, no MAPI calls are supported. 2, Mapi seems complicated, in fact, almost all data stores and access is a key interface: IMapITable. That is, all data stores are structural structures. I figured out this, and then the encoding is very embarrassed. Whether it is sending a mail or an operation address, an access data process is a form. : P 3, the above problem 2 still cannot be resolved. According to the description of "Sending or Receiving A Message On Demand", the key is the flushqueues () function, but I have tried various methods that can not take it into effect. After a few days, I finally suspected that Microsoft had a problem. So I got a question and answer online, there was such a paragraph in the middle: "The imapistatus :: flushqueueues method APPEARS NOT To Work at all, more more. Call it and you'll get a successful return code but nothing happens as a result I completely give up. "--Pete maclean. :( 7. Conclusion
This article is the experience of adding mail to the application. In the process, I have encountered a lot of problems, and he wrote him, mainly to give yourself a record, and I first finish my programming process into documents. If this article can help some friends who do the same need, I will make me very happy. I also hope that friends who have experience in this area can solve my legacy problem.
Welcome to send mail to catking@163.com to discuss various issues in programming, share fun.