Simple Authentication Security Layer: This is for the authentication methods that require OAUTH2 or other methods.
//Direct creation
var saslDirect = new SaslMechanismOAuth2("", "");
//If using GMAIL, we built in the google auth flow
var googleSASL = MailWatcher.SASL_GoogleAPIS("email", "CredentialPath.json");
All of the SDK references below are available when a callback occurs for new mail messages to be processed.
Addresses
To get the various addresses:
mail.CCAddresses();
mail.FromAddresses();
mail.ToAddresses();
//Or to get to the message envelope
var env = mail.Message.Envelope;
Labels and Flags
//Add
mail.AddFlags(MailKit.MessageFlags.Answered | MailKit.MessageFlags.Seen);
mail.AddLabels("bot");
//Get
var labels = mail.GetLabels();
var flags = mail.GetFlags();
Replying
The BodyBuildercallback is how you configure the outgoing message. You can see an example here of adding an image with CIDs.
The Reply method automatically configures the method reply parameters including the correct message headers, subject, response addresses, and if includeReplyText is true, it will also quote the original message back as any normal client would do.
//Generate a reply with the correct message contents
var mReply = mail.Reply(false, (bb) =>
{
//Set HTML, and text fallback content
bb.TextBody = "text is supported";
bb.HtmlBody = "<b>html</b> is supported";
//Add an attachment
//bb.Attachments.Add("MyPDF", File.ReadAllBytes("MyPDF.pdf"));
}, includeReplyText: true);
//Send
mail.SendMessage(mReply);
Is ?
To see if the message is <FLAG>:
var isAnswered = mail.IsAnswered;
var isSeen = mail.IsSeen;
var isFlagged = mail.IsFlagged;
var isDeleted = mail.IsDeleted;
var isDraft = mail.IsDraft;
Delete Mail
To delete a message, issue the delete command.
The parameter is for expunging the message from the server as well as issuing the deleted flag.
mail.DeleteMail(true);
Attachments
You can get a list of attachment parts, and iterate over them to get the actual content, mime type, name, etc.
//This does not pull the body or content, and is a fast way of checking how many attachments a message has
var attachcount = mail.Message.Attachments.Count();
//This iterates over the bodyparts to get additional information, it is slower and should be used after there are known messages
var attachments = mail.GetAttachments();
if (attachments.Any())
{
var attach = attachments.ElementAt(0);
var name = attach.FileName;
using MemoryStream attachBytes = mail.GetAttachment(attach);
}
Querying
To query the inbox:
var uidsNotAnswered = mail.GetNotAnsweredUIDs();
if (uidsNotAnswered.Any())
{
var ListMailSummaries = mail.FetchMessages(uidsNotAnswered);
}
var uidsNotSeen = mail.GetNotSeenUIDs();
if (uidsNotSeen.Any())
{
var ListMailSummaries = mail.FetchMessages(uidsNotSeen);
}
//Or direct access querying:
var uids = mail.Folder.Search(MailKit.Search.SearchQuery.DeliveredAfter(DateTime.UtcNow.AddDays(-1)));
Sent Box
If you need to access the sent box, we provide an easy way to retrieve and open the sent mailbox.
var sentBox = mail.GetAndOpenSentFolder();
if (sentBox != null)
{
var uids = sentBox.Search(MailKit.Search.SearchQuery.DeliveredAfter(DateTime.UtcNow.AddDays(-1)));
//Fetch using the sentbox, as mail.FetchMessages uses the inbox.
var ListMailSummaries = sentBox.FetchAsync(uids, mail.MessagePullItems).GetAwaiter().GetResult(); //make sure you're "using MailKit;"
}
IMAP only allows a single mailbox to be open at once. Don't forget to call:
mail.OpenInbox();.
This verifies and prevents future issues with any subsequent calls to the mail client.
Input Text
Sometimes you just need whatever the person said, excluding their signature and reply content. This method takes several passes at retrieving just the top level user input and skipping the rest:
There are a bunch of prebuilt methods to help with a mail client. If you want to do something specific, you can get the client and folder access as so, and use any of the available methods from MailKit:
var imapClient = mail.Client;
var imapInbox = mail.Folder;
Best practice watching
We highly recommend:
Putting a stop gap on the mail receive handler, something like IsAnswered, as a way of preventing unwanted reprocessing of messages.
Catching the exception and attempting to mark it answered and label it error if the client supports labelling.