Python Example:
# -*- coding: utf-8 -*-
import imaplib
# Set the email information.
imap_server = 'imap.sg.aliyun.com'
username = 'test@example.net'
password = 'xxxxx'
source_folder = 'INBOX'
destination_folder = '123'
# Connect to the IMAP server
mail = imaplib.IMAP4_SSL(imap_server)
mail.login(username, password)
# Select the source folder.
mail.select(source_folder)
# Search for NUM of all emails in the mailbox, and it is not recommended to use because NUM will change.
# result, num_data = mail.search(None, 'ALL')
# Print ('Result 1:',result, num_data)
# Search the UID of all emails in the mailbox, recommend
result, uid_data = mail.uid('search', None, 'ALL')
print ('Result 2:', result, uid_data)
# If the email is found
if result == 'OK':
for uid in uid_data[0].split():
# Move the email to the destination folder
# Note: The "MOVE" command is not supported. You can use "UID COPY" first and then delete the original email.
result, copy_data = mail.uid('COPY', uid, destination_folder)
if result == 'OK':
print('uid=', uid)
# Mark the email for deletion. Note that the original email will be automatically deleted after the COPY command.
mail.uid('STORE', uid, '+FLAGS', '(\\Deleted)')
# Execute delete (Remove emails marked for deletion from the current folder)
mail.expunge()
# Logoff
mail.close()
mail.logout()