Skip to content

IMAP Read

Type: imap-read  ·  Package: SMTP / IMAP Email Activities v1.0.5  ·  Output: List<ZoanMail>

Reads emails from an IMAP server and returns a list of messages (ZoanMail). It is the starting point of processes that react to incoming mail, with filters applied on the server itself to bring only what you need. It is the recommended option for reading mail (more complete than POP3).

  1. Connects to the server (port 993 with ssl by default), signs in and opens folder (default INBOX) in read-only mode: reading does not mark emails as read or change them.
  2. Searches the server for emails that match all the filters given. By default it brings only unread emails (unreadOnly = true).
  3. If hasAttachments = true, it keeps those with attachments.
  4. Takes the limit most recent (default 10) and downloads each full message; with includeAttachments = true also the attachment content.

Text filters (subject, from): the server checks that the field contains the text, usually case-insensitively. Date filters (since, before): compared by day (no time) against the date the message reached the server; since includes that day and before excludes it.

Folder names are the server’s, with its separator ("Processed", "INBOX/Invoices" or "INBOX.Invoices" depending on the server). In Gmail, labels are folders ("[Gmail]/Sent Mail", etc.).

ParameterTypeDefaultDescription
hoststring—IMAP server (e.g. imap.gmail.com).
usernamestring—Username (usually the full address).
passwordcredential—Password or App Password. Use credential("name")["password"].
ParameterTypeDefaultDescription
portint993IMAP port.
folderstring"INBOX"Folder to read, with the name the server uses.
unreadOnlybooltrueOnly unread emails. Set false to read all.
subjectstring—Filter: subject contains this text.
fromstring—Filter: sender (name or address) contains this text.
sincestring—Filter: received on or after this date. Format YYYY-MM-DD (a DateTime is also accepted). A date that cannot be parsed is ignored.
beforestring—Filter: received before this date (exclusive). Same format as since.
hasAttachmentsboolfalseFilter: only emails with attachments.
includeAttachmentsboolfalseDownloads attachment content into .Attachments (name, MIME type and bytes). Increases time and memory with large attachments.
limitint10Maximum number of emails to return (the most recent matching ones).
securitystring"ssl"Encryption: auto, none, ssl or starttls. See Encryption and ports.

Returns a List<ZoanMail> with up to limit emails, from oldest to newest within that group. Each email has:

PropertyContent
.IdMessage UID in the folder (numeric text). Use it as mailId in the other IMAP activities with the same folder.
.Subject, .From, .To, .CcSubject, sender address and recipient addresses.
.Body / .IsHtmlPlain text of the email; if it only has HTML, the HTML with .IsHtml = true.
.SentAtDate from the Date header (sent date).
.IsReadRead state on the server.
.AttachmentsAttachments with content if includeAttachments = true; otherwise empty.

If no email matches the filters, it returns an empty list (not an error) and says so in the log.

MessageCause and fix
'host' is required. / 'username' is required.The parameter is missing or the expression returned null.
Parameter 'password' is of type 'credential' but the value was null. Verify the credential exists in the configured store.The credential does not exist or could not be read. Check the name and that the Agent is connected to Zoan Cloud.
Server authentication errorWrong username or password, the account requires an App Password, or IMAP access is disabled for the mailbox.
Folder not found errorfolder does not exist on the server. Check the exact name and the subfolder separator.
Connection or TLS errorWrong host or port, or security does not match the port (993 + ssl, 143 + starttls).

Read unread emails with “Order” in the subject and attachments, save the attachments and mark each one as read:

IMAP Read
host = "imap.gmail.com"
username = credential("mail-bot")["user"]
password = credential("mail-bot")["password"]
subject = "Order"
since = today().AddDays(-7).ToString("yyyy-MM-dd")
hasAttachments = true
limit = 25
→ output: emails
For Each items = emails itemVariable = email
└─ activities:
IMAP Save Attachment host = "imap.gmail.com" username = credential("mail-bot")["user"] password = credential("mail-bot")["password"]
mailId = email.Id outputPath = "C:/orders/" + email.Id
IMAP Mark Read host = "imap.gmail.com" username = credential("mail-bot")["user"] password = credential("mail-bot")["password"] mailId = email.Id