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


/ Published in: C#
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. //Copying Multiple Messages From One Folder to Another
  2.  
  3. //[C# Code Sample]
  4.  
  5. using (ImapClient client = new ImapClient("exchange.aspose.com", "username", "password" ))
  6. {
  7. //create the destination folder
  8. string folderName = "EMAILNET-35242";
  9. if (!client.ExistFolder(folderName))
  10. client.CreateFolder(folderName);
  11.  
  12. try
  13. {
  14. //Append a couple of messages to the server
  15. MailMessage message1 = new MailMessage(
  16. "asposeemail.test3@aspose.com",
  17. "asposeemail.test3@aspose.com",
  18. "EMAILNET-35242 - " + Guid.NewGuid().ToString(),
  19. "EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation.");
  20. string uniqueId1 = client.AppendMessage(message1);
  21.  
  22. MailMessage message2 = new MailMessage(
  23. "asposeemail.test3@aspose.com",
  24. "asposeemail.test3@aspose.com",
  25. "EMAILNET-35242 - " + Guid.NewGuid().ToString(),
  26. "EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation.");
  27. string uniqueId2 = client.AppendMessage(message2);
  28.  
  29. //verify that the messages have been added to the mailbox
  30. client.SelectFolder(ImapFolderInfo.InBox);
  31. ImapMessageInfoCollection msgsColl = client.ListMessages();
  32. foreach (ImapMessageInfo msgInfo in msgsColl)
  33. Console.WriteLine(msgInfo.Subject);
  34.  
  35. //copy multiple messages using hte CopyMessages command
  36. client.CopyMessages(new string[] { uniqueId1, uniqueId2 }, folderName, true);
  37.  
  38. //Verify that messages are copied to the destination folder
  39. client.SelectFolder(folderName);
  40. msgsColl = client.ListMessages();
  41. foreach (ImapMessageInfo msgInfo in msgsColl)
  42. Console.WriteLine(msgInfo.Subject);
  43. }
  44. finally
  45. {
  46. try {
  47. client.DeleteFolder(folderName);
  48. }
  49. catch { }
  50. }
  51. }
  52.  
  53. //[VB.NET Code Sample]
  54.  
  55. Using client As New ImapClient("exchange.aspose.com", "username", "password")
  56. 'create the destination folder
  57. Dim folderName As String = "EMAILNET-35242"
  58. If Not client.ExistFolder(folderName) Then
  59. client.CreateFolder(folderName)
  60. End If
  61.  
  62. Try
  63. 'Append a couple of messages to the server
  64. Dim message1 As 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.")
  65. Dim uniqueId1 As String = client.AppendMessage(message1)
  66.  
  67. Dim message2 As 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.")
  68. Dim uniqueId2 As String = client.AppendMessage(message2)
  69.  
  70. 'verify that the messages have been added to the mailbox
  71. client.SelectFolder(ImapFolderInfo.InBox)
  72. Dim msgsColl As ImapMessageInfoCollection = client.ListMessages()
  73. For Each msgInfo As ImapMessageInfo In msgsColl
  74. Console.WriteLine(msgInfo.Subject)
  75. Next
  76.  
  77. 'copy multiple messages using hte CopyMessages command
  78. client.CopyMessages(New String() {uniqueId1, uniqueId2}, folderName, True)
  79.  
  80. 'Verify that messages are copied to the destination folder
  81. client.SelectFolder(folderName)
  82. msgsColl = client.ListMessages()
  83. For Each msgInfo As ImapMessageInfo In msgsColl
  84. Console.WriteLine(msgInfo.Subject)
  85. Next
  86. Finally
  87. Try
  88. client.DeleteFolder(folderName)
  89. Catch
  90. End Try
  91. End Try
  92. End Using

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.