MailExecute[cmd,target]
executes the mail command cmd on the target mail server connection, folder, file or item(s).
MailExecute
MailExecute[cmd,target]
executes the mail command cmd on the target mail server connection, folder, file or item(s).
Details
- Possible commands cmd for a mail item target include:
-
"Delete" delete a mail item "Flags" list flags for a mail item "SetFlags"{"flag1",…} set flags for a mail item "ClearFlags" clear all flags for a mail item "ClearFlags"{"flag1",…} clear specified flags for a mail item "Copy"MailFolder[…] copy mail item to the specified folder "Upload"MailFolder[…] upload mail item to the specified folder - Possible commands cmd for a mail file target include:
-
"UploadEMLFile"MailFolder[…] upload "EML" type files to the specified folder "UploadMBOXFile"MailFolder[…] upload "MBOX" type files to the specified folder - Possible commands cmd for a mail folder target include:
-
"Expunge" permanently remove all deleted items from a mail folder "Download" download all messages in the specified folder "MarkFolderRead" mark the entire mail folder as read "Create""name" create a folder using "name" in specified folder - Possible commands cmd for a mail server connection target include:
-
"Create""name" create a folder using "name" in the server root directory - For commands operating on mail items, MailExecute[cmd,{MailItem[…],MailItem[…],…}] operates on multiple items.
- MailExecute returns Success or Failure for the following commands: "Delete", "Upload", "UploadEMLFile", "UploadMBOXFile" and "Expunge".
- Commands that manipulate flags return the final list of flags for a mail item, or an association of results for a list of mail items.
- "Copy" returns the resulting MailItem object.
- "Create" and "MarkFolderRead" return the resulting MailFolder object.
- "Download" returns the resulting local File object.
Examples
open all close allBasic Examples (1)
Scope (1)
connection = MailServerConnect[]folder = connection["Example"]Download all emails from the folder:
directory = MailExecute["Download", folder]files = FileNames["*.eml", directory]Import one email as an Association and list its keys:
Keys[Import[First[files]]]Applications (1)
You can use MailExecute to permanently delete spam email messages. Connect to a mail server:
connection = MailServerConnect[]trash = connection["Trash"]Get three messages from the folder:
emails = trash[1 ;; 3]MailExecute["Delete", emails]Count messages marked for deletion:
MailSearch[trash, <|"Deleted" -> True|>][Length]Permanently delete the messages:
MailExecute["Expunge", trash]Neat Examples (1)
connection = MailServerConnect[];junk = connection["Junk"];Search the folder for all items that mention the word "free":
items = MailSearch[junk, <|"Text" -> "free"|>];Number of emails found by the mail search:
items[Length]items[MailExecute["Delete", #]&, "MailItem"]Check that all the flags include "Deleted":
items[MailExecute["Flags", #]& /* Values /* Union, "MailItem"]Related Guides
Text
Wolfram Research (2018), MailExecute, Wolfram Language function, https://reference.wolfram.com/language/ref/MailExecute.html (updated 2020).
CMS
Wolfram Language. 2018. "MailExecute." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/MailExecute.html.
APA
Wolfram Language. (2018). MailExecute. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MailExecute.html
BibTeX
@misc{reference.wolfram_2026_mailexecute, author="Wolfram Research", title="{MailExecute}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/MailExecute.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_mailexecute, organization={Wolfram Research}, title={MailExecute}, year={2020}, url={https://reference.wolfram.com/language/ref/MailExecute.html}, note=[Accessed: 13-June-2026]}