How to Copy Message from One Mailbox Folder to Another in .NET Apps

This technical tip explains how .NET developers can Copy Message from one Mailbox folder to another. Aspose.Email API provides the capability to copy message from one mailbox folder to another. It allows copying a single as well as multiple messages using the CopyMessage and CopyMessages methods. The CopyMessages method provides the capability to copy multiple messages from source folder of a mailbox to the destination mailbox folder. The following code sample illustrates this by copying two messages.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//your code here...//Copying Multiple Messages From One Folder to Another
//[C# Code Sample]
using (ImapClient client = new ImapClient("exchange.aspose.com", "username", "password" ))
{
    //create the destination folder
    string folderName = "EMAILNET-35242";
    if (!client.ExistFolder(folderName))
        client.CreateFolder(folderName);
    try
    {
        //Append a couple of messages to the server
        MailMessage message1 = new MailMessage(
            "asposeemail.test3@aspose.com",
            "asposeemail.test3@aspose.com",
            "EMAILNET-35242 - " + Guid.NewGuid().ToString(),
            "EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation.");
        string uniqueId1 = client.AppendMessage(message1);
        MailMessage message2 = new MailMessage(
            "asposeemail.test3@aspose.com",
            "asposeemail.test3@aspose.com",
            "EMAILNET-35242 - " + Guid.NewGuid().ToString(),
            "EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation.");
        string uniqueId2 = client.AppendMessage(message2);
        //verify that the messages have been added to the mailbox
        client.SelectFolder(ImapFolderInfo.InBox);
        ImapMessageInfoCollection msgsColl = client.ListMessages();
        foreach (ImapMessageInfo msgInfo in msgsColl)
            Console.WriteLine(msgInfo.Subject);
        //copy multiple messages using hte CopyMessages command
        client.CopyMessages(new string[] { uniqueId1, uniqueId2 }, folderName, true);
        //Verify that messages are copied to the destination folder
        client.SelectFolder(folderName);
        msgsColl = client.ListMessages();
        foreach (ImapMessageInfo msgInfo in msgsColl)
            Console.WriteLine(msgInfo.Subject);
    }
    finally
    {
        try {
            client.DeleteFolder(folderName);
        }
X

Url: http://www.aspose.com/.net/email-component.aspx

Language: C# | User: Sheraz Khan | Created: Jun 15, 2016 | Tags: Copy Message from Mailbox Copy multiple messages from one folder to another copy messages from source folder .NET Email component .NET Outlook API