article

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

Email
Submitted on: 6/15/2016 7:57:03 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 2325
 
     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.

 
				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.
//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);
}
catch { }
}
}
 
//[VB.NET Code Sample]
Using client As New ImapClient("exchange.aspose.com", "username", "password")
	'create the destination folder
	Dim folderName As String = "EMAILNET-35242"
	If Not client.ExistFolder(folderName) Then
		client.CreateFolder(folderName)
	End If
	Try
		'Append a couple of messages to the server
		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.")
		Dim uniqueId1 As String = client.AppendMessage(message1)
		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.")
		Dim uniqueId2 As String = client.AppendMessage(message2)
		'verify that the messages have been added to the mailbox
		client.SelectFolder(ImapFolderInfo.InBox)
		Dim msgsColl As ImapMessageInfoCollection = client.ListMessages()
		For Each msgInfo As ImapMessageInfo In msgsColl
			Console.WriteLine(msgInfo.Subject)
		Next
		'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()
		For Each msgInfo As ImapMessageInfo In msgsColl
			Console.WriteLine(msgInfo.Subject)
		Next
	Finally
		Try
			client.DeleteFolder(folderName)
		Catch
		End Try
	End Try
End Using
 Overview: Aspose.Email for .NET
Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.