How to Migrate Emails Using IMAPSync
In this article
IMAPSync is a free, command-line tool that copies emails from one IMAP server to another, preserving folders, flags, and dates. It is ideal for migrating to a new email provider or moving mail between accounts. Before starting, you need: access to both servers (hostname, port, username, password), IMAP enabled on both, and enough disk space for temporary files if you use the --tmpdir option. Install imapsync on your computer (Linux, macOS, or Windows via WSL or Cygwin).
What You'll Need
- Source and destination IMAP server hostnames (e.g., imap.example.com)
- Ports (usually 993 for SSL, 143 for STARTTLS)
- Full email addresses and passwords for both accounts
- IMAPSync installed (download from imapsync.lamiral.info or your package manager)
- Optional: a temporary directory with free space for large attachments
Step-by-Step
- Test connectivity to both servers. Use a tool like
openssl s_client -connect imap.source.com:993(ortelnetfor non-SSL) to ensure the servers are reachable and IMAP is working. - Run a dry-run (test mode) first. Execute:
imapsync --host1 imap.source.com --user1 source@example.com --password1 'secret1' --host2 imap.destination.com --user2 dest@example.com --password2 'secret2' --dry. This shows what would be copied without making changes. Check the output for errors like authentication failures or connection issues. - Specify SSL/TLS if needed. Most modern servers use SSL on port 993. Add
--ssl1and--ssl2flags. For STARTTLS on port 143, use--starttls1and--starttls2. If unsure, test with--ssl1first. - Run the actual migration. Once the dry-run looks good, remove
--dryand run the command. Example:imapsync --host1 imap.source.com --user1 source@example.com --password1 'secret1' --ssl1 --host2 imap.destination.com --user2 dest@example.com --password2 'secret2' --ssl2. Wait for it to finish; it may take hours for large mailboxes. - Verify the migration. Log into the destination account via webmail or an email client. Check that folders, subfolders, and messages are present. Spot-check a few emails for attachments and dates.
- Handle special folders. If your source uses non-standard folder names (e.g., "Sent Items" vs "Sent"), use
--regextrans2to rename. For example, to rename "Sent Items" to "Sent":--regextrans2 's/Sent Items/Sent/'. - Use a configuration file for complex migrations. Create a text file with options (one per line) and run
imapsync --config myconfig.txt. This avoids long command lines and makes it repeatable.
Common Mistakes
- Forgetting to enable IMAP on the destination account (check server settings).
- Using the wrong port or SSL flag, leading to connection failures.
- Not running a dry-run first, which can cause accidental deletions if you use
--delete1. - Ignoring folder name differences; imapsync creates folders as-is, so mismatches cause duplicate or missing folders.
- Running the migration twice without
--skipcrossduplicatescan create duplicate emails if folders overlap. - Using a weak password with special characters that get interpreted by the shell; always quote passwords.
Tips
- Always run a dry-run first, even on small test accounts.
- Use
--automapto automatically map common folder names (e.g., "Sent Items" to "Sent"). - Add
--maxsize 100000000to skip messages larger than 100 MB if you have size limits. - Use
--useheader 'Message-ID'to avoid duplicates when re-running. - For large mailboxes, run in a
screenortmuxsession to avoid losing progress if your connection drops. - Keep the destination account under quota; if it fills up, imapsync will stop with errors.
Troubleshooting
Authentication failed: Double-check username and password. Some servers require the full email address, others just the local part. Test with a simple IMAP client first.
Connection timeout: Check firewall rules and server ports. Try using --timeout 120 to increase the timeout. Also, ensure you're using the correct SSL/TLS flag.
"Folder does not exist" errors: The destination may not have the folder yet. imapsync usually creates folders, but if you get this error, try adding --createfolders (it's default, but explicit is fine).
Duplicate messages: If you run the migration twice, duplicates can appear. Use --skipcrossduplicates or --useheader 'Message-ID' to prevent this.
Migration is very slow: For large mailboxes, increase speed with --maxbytespersecond 0 (unlimited) and --maxlinelength 1000000. Also, ensure you have a stable network connection.
Error: "Can't connect to host2" Verify the destination server is reachable and IMAP is enabled. Try connecting with a regular email client.
Frequently Asked Questions
Does imapsync delete emails on the source?
No, by default imapsync only copies emails. It will not delete anything unless you explicitly use --delete1 or --delete2 flags.
Can I migrate from Gmail to Outlook?
Yes, as long as both accounts have IMAP enabled. For Gmail, you may need to generate an app password if you have 2FA enabled.
Will imapsync copy attachments and embedded images?
Yes, it copies the full email message including attachments and inline images, as long as the message size is within server limits.
How long does a migration take?
It depends on the number and size of emails, and your connection speed. A few thousand emails can take minutes to hours. For large mailboxes (10+ GB), expect several hours.
Can I migrate only specific folders?
Yes, use --include '^FolderName' (regex) to include only certain folders, or --exclude to skip them.
Is imapsync free?
Yes, imapsync is free and open-source. However, the author asks for a donation if you use it professionally.