如何在.NET应用程序中将邮件从一个邮箱文件夹复制到另一个邮箱文件夹

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在.NET应用程序中将邮件从一个邮箱文件夹复制到另一个邮箱文件夹相关的知识,希望对你有一定的参考价值。

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

以上是关于如何在.NET应用程序中将邮件从一个邮箱文件夹复制到另一个邮箱文件夹的主要内容,如果未能解决你的问题,请参考以下文章

C# 如何从 Outlook 的共享邮箱发送邮件并将其保存在已发送文件夹中

263企业邮箱 如何在Outlook邮件客户端中设置IMAP收发邮件

如何在 PHP 中将电子邮件附件保存到服务器?

在Linux中如何向163邮箱发邮件

如何使用 GMail 的 SMTP 服务器从 ASP.NET 站点发送电子邮件? [复制]

FOXMAIL 7.2 本地邮件恢复