Posted By

sherazam on 06/15/16


Tagged

email folder message copy Net to from api multiple messages source Outlook component mailbox another one


Versions (?)

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


 / Published in: C#
 

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

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

Report this snippet  

You need to login to post a comment.