Mail parsing engine Fastmail library

xiaoxiao2021-03-06  80

1 Overview

The mail parsing API fully uses object-oriented technology design, using C language, developed for mail resolution and assembly. It provides some classes to resolve and assemble Internet mail, such as MimeMessage and MimeBodypart for Internet mail protocol implementation and follow RFC822 and RFC2045 specifications. These API libraries are used for applications.

1.1 term

RFC: Request for Comments, request annotation, Internet standards (draft)

Mime: Multipurpose Internet Mail Extension Protocol, multi-purpose internet mail expansion protocol

2 Hello World!

The following is a simple program for a mail parser library, which means the method of using an object-oriented API parsing message:

void main ()

{

Char * emilingata = loadingmailfile ("HelloWorld.eml");

MIMEMESSAGE Email (EmaiLData);

FastString Subject, from, BodyText;

InternetAddress Addr;

Email.getsubject (Subject);

Email.GetFrom (addr); addr.tostring (from);

Email.getTextPlain (BodyText);

Printf ("Subject:% S / NFROM:% S / NBODY:% S / N",

Subject.c_str (), from.c_str (), bodytext.c_str ());

Free (emaildata);

}

3 Mail parsing library

The mail resolution library contains a series of classes, mainly MimeMessage, MIMEBODYPART, MIMEMULTIPART, InternetHeaders, InternetDress, InternetDress, InternetDress, InternetDress, InternetdRess (mail address class) And ContentType, etc. Analysis and assembly emails mainly use these classes to assemble and decompose.

Now introducing the most important thing is the class mimeMessage that provides the main call interface API.

MIMEMessage provides a range of methods for use, such as defining a structure that acquiring address information and acquiring message text content (which can be a MIMEMULTIPART object for specific data), used to implement RFC822 and MIME specifications.

A mail content data is saved in a mimeMessage object, as well as some properties that record specific email addresses (such as Sender and Recipients). There is also a structural information of this message, as well as a content-type of its mail main body (body).

The following is to describe the possible structure inside the MIMEMessage object:

4 example

4.1 Resolution Mail

The following example details how to use a mail resolution library API to resolve an email:

/ **

* Mail source data passed through parameters

* @Param msg pointing to a string pointer to the mail source

* @Param len mail source length

* /

Void ParseMessage (Const CHAR * MSG, Const Int Len) {

/ / Define a MIMEMESSAGE mail object for parsing

// Mail object uses a string pointer and length of a string pointer and length to a mail source.

// Can also use MIMEMESSAGE Email (MSG) construction, the purpose of incoming the LEN parameter is to save another time

// Strlen () time because some mail source is relatively large.

// Note: If you only get the message header, MimeMessage only parsed the message header data and does not resolve the text body.

MIMEMESSAGE Email (MSG, LEN);

// Get the send time, this time UTC time

// Coordinated Universal Time (Utc, Formerly Referred To As "Greenwich Mean Time")

TIME_T SENTTM = email.getSentDate ();

/ / Define the string variable of the store's subject, the mail resolution library uses fastString as a string process

Faststring subject;

// Call the GetSubject () method of the mimeMessage class get the email topic, put in the subject variable

Email.getsubject (Subject);

// Print the output topic, the c_str () method is the method of the standard acquisition string content pointer

Printf ("Subject:% S / N", Subject.c_STR ());

// Define the variable of the stored sender address, where InternetDress is the class for processing the email address.

Internet ADRESS.

// Call the GetFrom () method of the MIMEMESSAGE class to get the mail sender address

Email.getFrom (from);

/ / Output address sender address, Personal is the name of the email address, address is address

Printf ("from: /"% s / "<% s> / n", from.Personal (), from.address ());

/ / Define the variable of the stored sender address, here is used by InternetDressArray because the recipient may have multiple

InternetdRessArray toaddrs;

// Call the Getto () method of the MIMEMESSAGE class Get all recipient address information

// Get other addresses such as Cccc () Getcc () admittors list the MIMEMESSAGE method list later

Email.getto (ToAddrs);

// Since InternetDressArray is a FASTARRAY array class, the following ways are output one by one.

/ / Define iterators that travers the array (this is an object-oriented design, similar to the iterator usage of the STL library container)

InternetAddressArrayiterator IT (toAddrs);

/ / Judge whether the iterator has come to the end of the array, otherwise enter the loop

While (! it.done ()) {

/ / Output mail address, iterator is equivalent to pointing to InternetAddress

Printf ("to: /"% s / "<% s> / n", IT-> Personal (), IT-> address ());

// Move forward forward

it.advance ();

}

// The traversal of the array can also use the following traditional way

For (int i = 0; i

// Since the [] operator does not calculate the array range, it is not recommended to use it. Try to use iterators,

/ / Unless you want to get the nth address directly

Printf ("to: /"% s / "<% s> / n", toaddrs [i] .personal (), toaddrs [i] .address ());

/ / Get the contents of other mail header lines

Faststring Xline;

Email.getHeader ("X-priority", XLine;

Printf ("X-priority:% s / n", XLINE.C_STR ());

// Get the text of the text. Since each email may contain a plain text and one

// html is in a stylist, so they get separately

Faststring

TEX

Tplain;

Email.getTextPlain

TEX

TPLAIN);

Printf ("BodyTextPlain:% S / N",

TEX

TPLAIN.C_STR ());

/ / Can also get the character set encoding method of plain text body at the same time, for the call to

// Select different character sets to display to the user. GetTextHtml () is also similar.

Faststring charSet;

Email.getTextPlain

TEX

TPLAIN, CHARSET);

/ / Get the message of the message HTML.

Faststring

TEX

THTML;

Email.getTexthtml

TEX

THTML);

Printf ("BodytextHtml:% S / N",

TEX

THTML.C_STR ());

// Get the name of all attachments to mail.

FaststringArray filenames;

Email.GetallattachmentFileNames (filenames);

// Traversing the lookup name is the same as other array usage

/ / Get the attachment content of the specified attachment file name, if there is an attachment of the repeated name will return only the same

// The attachment data of the name. To get all other attachments, please refer to the following method.

FastString FileName ("Attr1.jpg"), Content;

Email.getatachment (filename, content);

// Get all accessories of your mail.

AttachmentPTRARRAY Attachments;

Email. GetAllattachments; ATTACHMENTS

// Total number of attachments

Int attnum = attachments.size ();

// Traverse all accessories

For (size_t i = 0; i

{

// Get the pointer to this attachment part, note: You cannot directly modify the operation of the pointer content.

MIMEBODYPART * part = attachments [i];

IF (part == null)

CONTINUE;

Faststring filename, content

/ / Get this attachment file name

Part-> getFileName (filename);

// Get this attachment content, decoded

Part-> getContent (Content);

}

/ / Get the name and content of the inline resource attachment of the message

// The method is like obtaining a normal accessory, but only calls getRelatedAttachment (), etc.

// filename parameter replacement with a CID (Content-id)

// Get the name of all inline resource attachments for the mail.

FaststringArray CIDS;

Email.getAllrelatedDattachmentCIDS (CIDS);

// Traversing the lookup name is the same as other array usage

/ / Get the attachment content of the specified intraline resource attachment file name, if there is an attachment for repeated names, will only return the first one

// The attachment data of the name. To get all other attachments, please refer to the following method. FastString CID ("333476372 $ 1097735850 $ 0600030 @ local"), content

Email.getReledattachment (CID, Content);

/ / Get all inline resource attachments for mail.

AttachmentPTRARRAY Attachments;

Email. GetAllrelatedDattachments; attachments;

// Inline resource attachment total

Int attnum = attachments.size ();

// Traverse all inline resource accessories

For (size_t i = 0; i

{

// Get the pointer to this attachment part, note: You cannot directly modify the operation of the pointer content.

MIMEBODYPART * part = attachments [i];

IF (part == null)

CONTINUE;

Faststring filename, CID, Content

/ / Get this inline resource attachment CID

Part-> getContentId (CID);

/ / Get this inline resource attachment file name

Part-> getFileName (filename);

/ / Get this inline resource attachment content, decoded

Part-> getContent (Content);

}

}

4.2 assemble mail

The following example details how to assemble an email with a mail resolution library API:

/ **

* Mail source data passed through parameters

* @Param EmaiData Storage Group The string of mail source

* /

Void CreateMessage (FastString & EmaiData)

{

/ / Define a MIMEMESSAGE mail object for assembly

MimeMessage email;

// Set the title

Email.SetSubject ("test mail");

// Set the sender

Email.set Shender ("Test@test.Net");

// Option, the back is the name of the address

Email.set Shender ("Test@test.Net", "Test Account");

// Add recipient

Email.addto ("Test1@test.net");

Email.addto ("Test2@test.Net", "Recipient

2"

);

...

// Add a CC

Email.adDCC ("Test3@test.net");

Email.addcc ("Test4@test.net", "Recipient

4 "

);

...

// Add a secret person address

Email.addbcc ("Test5@test.net");

Email.addbcc ("Test6@test.net", "Recipient

6 "

);

/ / Set a special email header

Email.addheader ("X-MAILER", "Xmail

2.0 "

);

// Set the text body, the default code GB2312 (environment variable control, how to configure the mail resolution environment)

Email.SettextPlain ("this is a test mail created by xmail");

/ / Can also specify the encoding method

Email.SettextPlain ("this is a mail encoded by gbk", "gbk");

// can of course also

Faststring bodytext;

.. .. .. // bodytext can read from other places

Email.setTextPlain (Bodytext); //

Email.SettextPlain (Bodytext, "GBK");

/ / Set HTML body, similar to the texture of the pure text.

// Note: An email can contain a plain text body and an HTML body for reader selection display.

Email.SettextHtml (" this is a test mail ");

/ / Can also specify the encoding method

Email.SettextHtml (" this is a mail encode by gbk ", "GBK");

// can of course also

Faststring bodyhtml;

.. .. .. // BodyHTML can be read from other places

Email.SetTextHtml (Bodyhtml);

// or like this

Email.SettextHtml (Bodyhtml, "GBK");

// Add attachments

FastString FileName ("Attr1.jpg"), FileData;

.. .. .. // filename and FileData can read from other places

Email.Addattachment (FileData, FileName, "Image / JPEG");

// can also

Email.addattachment (FileData, "Attr1.jpg", "Image / JPEG");

// Note: If you do not specify the third parameter behind, that is, the MIMETYPE type of the attachment

// MimeMessage will find it in the Mimetypes data mapping table based on the FILENAME extension.

// Note: MIMETYPES mapping table can be configured, refer to the "Configuration Mail Analytical Environment" later

// So can also be called like this

Email.Addattachment (FileData, FileName);

Email.addattachment (FileData, "Attr1.jpg");

// Add an embedded resource attachment, similar to adding normal accessories.

// Note: You must set the HTML text of the message before you can add the embedded resource attachment, otherwise you will not add it.

// src is the URL of the resource attachment in the HTML text, including path and file name

// CID is the CID value of the addition of the resource attachment

// Return Value Count is the number of resource attachments in HTML

FastString SRC ("/ images / attr1.jpg"), CID, FileData

.. .. // src and filedata can read from other places

INT count = email.addrelatedAttachment (FileData, SRC, CID, "Image / JPEG");

// can also be like this

Count = email.addrelatedAttachment (FileData, "/Images/attr1.jpg", CID, "Image / JPEG");

Count = email.addrelatedAttachment (FileData, "/Images/attr1.jpg", CID);

Count = email.addrelatedAttachment (FileData, "/Images/attr1.jpg", CID);

// Note: If you re-overwate the mail html text, call setTextHtml (),

// mimeMessage will automatically traverse all resource attachments, delete the resource attachments that are not referenced.

}

4.3 Modify Mail

The following example details how to use a mail resolution library API to modify an email: / **

* Mail source data passed through parameters

* @Param EmaiData Storage Group The string of mail source

* /

Void CreateMessage (FastString & EmaiData)

{

/ / Define a MIMEMESSAGE SIT object is used to modify

// Method and the above parsing and assembly are similar, analyzing and assembling the method of calling.

MIMEMESSAGE Email (EmaiLData);

// Let's introduce the deleted function

/ / Clear mail plain text body

Email.removetextPlain ();

// Clear email html body

Email.removetexthtml ();

/ / Delete an attachment of the specified file name

FastString FileName ("attr1.jpg");

Email.removeattachment (filename);

Email.removeattachment ("attr1.jpg");

/ / Delete an attachment of the specified location, get the location in the array of getAllatTachments (), starting from 0

Email.removeattachment (2);

/ / Delete all attachments

Email.removeallattachments ();

/ / Delete all embedded resource attachments

Email.removeallRelatedAttachments ();

/ / You can also call the above assembly method to update the specified data

// Update mail source data

// Note: You must redefine the variable storage new source data

Faststring newdata;

Email.toString (NewData);

/ / Update the return value string

Emaildata = newdata;

}

4.4 Advanced Features

The mail parsing engine library API also has more complex and more powerful advanced features that can be assembled and resolved in a message that meets RFC822 and RFC2045. Please refer to the test program mimeutils.cpp and related documents in the resolution library.

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

New Post(0)