Sample code for 30+ languages & platforms
VBScript

Copy an IMAP Message to Another Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.Copy method, which copies one message from the currently selected mailbox to a destination mailbox. The first argument identifies the source message; the second (bUid) indicates whether the id is a UID (true) or a sequence number (false). This example copies a message from the Inbox to another mailbox.

Background: Messages can be identified two ways in IMAP: a sequence number (the message's current 1-based position, which shifts as messages are added or expunged) or a UID (a stable identifier that doesn't change). The bUid flag tells Copy which you're passing. Copying leaves the original in place and places a duplicate in the destination — use MoveMessages to relocate instead of duplicate.

Chilkat VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Demonstrates the Imap.Copy method, which copies one message from the currently selected
'  mailbox to a destination mailbox.  The 1st argument identifies the source message; the 2nd
'  (bUid) indicates whether the id is a UID (1) or a sequence number (0).

set imap = CreateObject("Chilkat.Imap")

imap.Ssl = 1
imap.Port = 993

success = imap.Connect("imap.example.com")
If (success = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If

success = imap.Login("user@example.com","myPassword")
If (success = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If

'  Select the source mailbox.
success = imap.SelectMailbox("Inbox")
If (success = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If

'  Copy message sequence number 1 to the "Archive2026" mailbox.  bUid = 0 means the id
'  is a sequence number, not a UID.
success = imap.Copy(1,0,"Archive2026")
If (success = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If

outFile.WriteLine("Message copied.")

success = imap.Disconnect()
If (success = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If


outFile.Close