Getting started
editInstall the package
Add the Meziantou.Framework.Email package to a project:
dotnet package add Meziantou.Framework.EmailThe package does not send email: it reads and writes files. Its types are in these namespaces:
| Namespace | Types |
|---|---|
| Meziantou.Framework.Email | The common model (EmailMessage), email messages (MimeMessage), the exceptions |
| Meziantou.Framework.Email.Mapi | The Outlook items and their MAPI properties (MapiMessage, MapiContact...), .msg files (MsgFile), TNEF streams (TnefFile) |
| Meziantou.Framework.Email.Storage | The files that store items: Outlook data files (PstFile), mbox, Maildir, Apple Mail, Thunderbird, offline address books |
| Meziantou.Framework.Email.Contacts | vCard (VCard) |
| Meziantou.Framework.Email.Calendar | iCalendar (VCalendar) |
The examples of this documentation omit the using directives of these namespaces.
Convert a message
EmailMessage holds what the formats have in common: the addresses, the subject, the dates, the identifiers of the thread, the text, HTML and RTF bodies, the attachments and the embedded messages. FromMapi and FromMime create it from an item of an Outlook data file or from a MIME message, and ToMime and ToMapiBuilder write it back:
// An item of an Outlook data file, saved as an .eml file
using (var file = PstFile.Open(pstPath))
{
var item = file.GetFolder("Inbox")!.EnumerateItems().First();
EmailMessage.FromMapi(item).ToMime().WriteTo(emlPath);
}
// The .eml file, read back
var email = EmailMessage.FromMime(MimeMessage.Load(emlPath));
Assert.Equal("Welcome to Microsoft Office Outlook 2003", email.Subject);
Assert.HasCount(11, email.Attachments);In an Outlook item, the author of a message (From) is the user the message is sent on behalf of (SentRepresentingName), the Sender is the user who sent it (SenderName), and the ReplyTo addresses are the ReplyRecipients. An item that has no represented user, or the same user as its sender, has no Sender.
The other headers of a message are kept in Headers. What a format cannot store is lost when a message is converted to it: a MIME message has no RTF body, and an address without an address part (only a name) cannot be written in a MIME header.
Detect the format of a file
EmailFormatDetector.Detect returns the format of a file, of a stream or of bytes, from their content: the signatures of the binary formats (Outlook data files, .msg, TNEF, offline address books) and the first lines of the text formats (.eml, .emlx, mbox, Mork, vCard, iCalendar). A folder that has the cur, new and tmp subfolders is a Maildir folder.
var format = EmailFormatDetector.Detect(path);
Assert.Equal(EmailFileFormat.Msg, format);
Assert.Equal(EmailFileFormat.VCard, EmailFormatDetector.Detect("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Alice\r\nEND:VCARD\r\n"u8));
Assert.Equal(EmailFileFormat.Eml, EmailFormatDetector.Detect("From: alice@example.com\r\nSubject: Hello\r\n\r\nHi"u8));
Assert.Equal(EmailFileFormat.Unknown, EmailFormatDetector.Detect("Hello world"u8));A compound file is an Outlook item when it has the property stream of an item, so detecting an .msg file reads the whole file. The other formats are detected from the first 64 KB. An email message is detected from its header fields: the content must start with header fields, one of them a usual field of a message (From, Date, Subject, Received...).