C# Requires Chilkat v11.0.0+
C#
Find an email by subject and fetch it by IMAP UID
See more Email Object Examples
Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.
Chilkat C# Downloads
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Chilkat.Imap imap = new Chilkat.Imap();
imap.Ssl = true;
imap.Port = 993;
imap.RequireSslCertVerify = true;
imap.PeekMode = true;
imap.AutoDownloadAttachments = true;
success = imap.Connect("imap.example.com");
if (success != true) {
Debug.WriteLine(imap.LastErrorText);
return;
}
success = imap.Login("MAIL_USER","MAIL_PASSWORD");
if (success != true) {
Debug.WriteLine(imap.LastErrorText);
return;
}
success = imap.SelectMailbox("INBOX");
if (success != true) {
Debug.WriteLine(imap.LastErrorText);
return;
}
Chilkat.MessageSet matches = new Chilkat.MessageSet();
success = imap.QueryMbx("SUBJECT \"Sample message\"",true,matches);
if (success != true) {
Debug.WriteLine(imap.LastErrorText);
return;
}
if (matches.Count == 0) {
Debug.WriteLine("No matching message in INBOX.");
success = imap.Disconnect();
if (success != true) {
Debug.WriteLine(imap.LastErrorText);
return;
}
return;
}
// The search returned UIDs. Use the same identifier type for the fetch.
uint uid = matches.GetId(0);
Chilkat.Email email = new Chilkat.Email();
success = imap.FetchEmail(false,uid,true,email);
if (success != true) {
Debug.WriteLine(imap.LastErrorText);
return;
}
Debug.WriteLine("Mailbox INBOX, UIDVALIDITY " + Convert.ToString((int) imap.UidValidity) + ", UID " + Convert.ToString(uid));
success = imap.Disconnect();
if (success != true) {
Debug.WriteLine(imap.LastErrorText);
return;
}
// The downloaded Email remains usable after disconnecting.
Debug.WriteLine("Subject: " + email.Subject + " Files: " + Convert.ToString(email.NumAttachments));
success = email.SaveEml("imap-received.eml");
if (success != true) {
Debug.WriteLine(email.LastErrorText);
return;
}
Debug.WriteLine("Saved imap-received.eml.");