Moving mail (copy)
This is a move operation, not a copy. The original email is deleted from its source location.
Difference between UID and NUM
UID (Unique ID):
Each email in a mailbox has a unique identifier (UID). The UIDs of the remaining emails do not change if an email is deleted.
You can use the `mail.uid` method for operations that use UIDs. For example: `mail.uid('search', None, 'UNSEEN')`.
NUM (Message Number):
A NUM is the ordinal number of an email in the current folder. If an email is deleted or moved, the NUMs of the remaining emails are re-sorted.
You can use methods such as `mail.search` or `mail.fetch` for operations that use NUMs. For example: `mail.search(None, 'UNSEEN')`.
Python code example
Scenario: Move three emails from folder1 to folder2 using the `copy` command. This example demonstrates how to perform the operation using both the UID and NUM methods.
This code was tested in a Python 3.11.9 environment. You should thoroughly test the code before you use it in a production environment.
# -*- coding: utf-8 -*-
import imaplib
def before_after_move_email(source_folder, destination_folder):
# Select a folder
mail.select(source_folder)
# uid
result, data = mail.uid('search', None, 'UNSEEN')
print(f'Search results for {source_folder} by UID: {result}, {data}')
# # num
# result, data = mail.search(None, 'UNSEEN')
# print(f'Search results for {source_folder} by NUM: {result}, {data}')
# Select a folder
mail.select(destination_folder)
# Search for the UIDs of all emails in the mailbox. This is the recommended method.
# uid
result, data = mail.uid('search', None, 'UNSEEN')
print(f'Search results for {destination_folder} by UID: {result}, {data}')
# num
# result, data = mail.search(None, 'UNSEEN')
# print(f'Search results for {destination_folder} by NUM: {result}, {data}')
# Set mailbox information
# Configure the IMAP server
imap_server = 'imap.qiye.aliyun.com' # IMAP server address
username = 'test@example.com' # Username
password = '*******' # Password
port = 993 # Port number
source_folder = 'folder1'
destination_folder = 'folder2'
# Connect to the IMAP server
mail = imaplib.IMAP4_SSL(imap_server, port)
mail.login(username, password)
print('Before move:')
before_after_move_email(source_folder, destination_folder)
# Move the specified UID
mail.select(source_folder)
# uid
result, copy_data = mail.uid('COPY', '6', destination_folder)
# num
# result, copy_data = mail.copy('1', destination_folder)
print('After move:')
before_after_move_email(source_folder, destination_folder)
The code contains three pairs of comments: `# uid` and `# num`. You can use these comments to switch between the UID and NUM methods.
In summary, when you move an email using its UID, it is assigned the next available UID in the destination folder. When you move an email using its NUM, the emails in the destination folder are re-sorted.
Execution results
Using the UID method
When the email with UID 16 is moved, it is assigned the next available UID in the destination folder. In this example, its new UID is 14.

Using the NUM method
When the email with NUM 2 is moved, it is re-sorted and assigned NUM 1 in the destination folder.
