How to use vmime to receive/parse emails

Source: Internet
Author: User
Tags flush gettext posix string format

Receive emails using vmime

Vmime encapsulates the Mail format and protocol, which is very convenient to use.
Vmime is encapsulated in the vmime: net namespace for the Mail Protocol. The main objects used are:
Vmime: net: session, mainly used to maintain connections with servers
Vmime: net: store, which indicates a mail storage. This is a base class. No mail protocol has its own store (such as POP3Store and IMAPStore)
Vmime: net: folder, which indicates the folder on the mail storage. Like store, each Mail Protocol has its own folder implementation.
Vmime: net: message, indicating an online message. Unlike vmime: message, vmime: net: message may only be part of the message, for example, the Mail class information (determined by the Mail Protocol used ).
Vmime creates a store based on the Mail Protocol set in the session.
Implementation of some common operations (POP3 protocol ):
Connection email:
Vmime: utility: ref <vmime: net: session> session = vmime: create <vmime: net: session> (); // create a session
Vmime: utility: ref <vmime: net: store> store = session-> getStore (vmine_url); // Obtain the store
Store-> connect (); // connect
Vmime: utility: ref <vmime: net: folder> folder = store-> getDefaultFolder (); // create a folder with the default path (inbox)
Folder-> open (vmime: net: folder: MODE_READ_WRITE); // open it in the form of read/write
Get email:
Std: vector <vmime: utility: ref <vmime: net: message> allMessages = folder-> getMessages ();
Folder-> fetchMessages (allMessages, vmime: net: folder: FETCH_ENVELOPE); // Obtain the header information of all emails, including sender, recipients, date, and subject.
Vmime: string mailContent;
Vmime: utility: outputStreamStringAdapter out (mailContent );
ResultMsg-> extract (out); // find the desired email, download it to the local device, and save it to the string. Here, vmime: string is the typedef of std: string.
Delete email:
Folder-> deleteMessage (resultMsg-> getNumber (); // execute the Delete Command
Folder-> close (true); // close the folder to delete emails.
Problems in use:
According to the example in vmime-book, after the vmime: net: folder: FETCH_FLAGS label is added when obtaining the email, an exception is thrown, prompting that this operation is not supported.
After the folder-> deleteMessage function is executed, the email is not deleted. After capturing packets and viewing the source code, we found that the deleteMessage function sent the DELE command to the mail server, but the mail server did not immediately execute the command. It was deleted only after QUIT. In the folder destructor, the folder-> close (false) function is called to close the folder, so that before sending the QUIT command, an RSET command is sent to the mail server to recharge the deleted email status. Therefore, the deleted email is not deleted. Currently, only after the Delete command is executed, the close (true) function is explicitly executed to ensure that the QUIT command is sent immediately and the server deletes the mail.
The POP3 command actually executed by the preceding command is:
# Connection
USER xxx # USER name
PASS xxx # Password
STAT # query the number and size of emails
TOP 1 0 # view the mail header with the serial number 1
RETR 1 # accept all content in the first email
DELE 1 # delete the first email
QUIT # exit, and the server will perform the delete operation

Vmime resolution email


It is relatively simple to parse emails. You need to convert the received emails from the string to the vmime: message format, and then you can get the desired content.
First, convert the vmime: string format to vmime: message:
Vmime: utility: ref <vmime: message> mail = vmime: create <vmime: message> ();
Mail-> parse (mailContent );
Vmime also provides a simple help class vmime: messageParser for message parsing.
Message mainly contains the mail header and content. The content is split into multiple vmime: textPart due to the multi-part message format. The textPart subclasses include vmime: htmlTextPart and vmime: plainTextPart. The content-type in the mail body is text/html and text/plain.
Code:
Vmime: messageParser mp (mail );
For (int I = 0; I <mp. getTextPartCount (); ++ I) // traverses all textparts
    {
Vmime: utility: ref <const vmime: textPart> text = mp. getTextPartAt (I );
If (text-> getType (). getSubType () = vmime: mediaTypes: TEXT_HTML) // text/html
        {
Vmime: utility: ref <const vmime: htmlTextPart> htmlText = text. dynamicCast <const vmime: htmlTextPart> ();
  
Vmime: utility: outputStreamStringAdapter htmlOut (htmlContent );
Vmime: utility: charsetFilteredOutputStream utf8Out (htmlText-> getCharset (), vmime: charset ("UTF-8 & Prime;), htmlOut); // force convert the body to utf8 encoding
HtmlText-> getText ()-> extract (utf8Out );
Utf8Out. flush ();
        }
Else if (text-> getType (). getSubType () = vmime: mediaTypes: TEXT_PLAIN) // text/plain
{
Vmime: utility: ref <const vmime: plainTextPart> plainText = text. dynamicCast <const vmime: plainTextPart> ();
Vmime: utility: outputStreamStringAdapter plainOut (plainTextContent );
Vmime: utility: charsetFilteredOutputStream utf8Out (plainText-> getCharset (), vmime: charset ("UTF-8 & Prime;), plainOut );
PlainText-> getText ()-> extract (utf8Out );
Utf8Out. flush ();
}
    }

For the body of an html email, you can traverse and obtain the embeddedObject, such as the embedded attachment Image. However, there is no such requirement, so you have not tried it.
Another problem was found during actual execution. You must call vmime: platform: setHandler <vmime: platforms: posix: Before getting started :: posixHandler> (); set platform-related handler. Here we set a platform that complies with posix. windows also seems to have a corresponding handler.

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.