จัดการ ย้าย และจัดระเบียบโฟลเดอร์ IMAP
การดำเนินการโฟลเดอร์
รับข้อมูลโฟลเดอร์
การดึงข้อมูลโฟลเดอร์จากเซิร์ฟเวอร์ IMAP ง่ายมากด้วย Aspose.Email เรียก ListFolders() เมธอดของ ImapClient คลาส มันคืนอ็อบเจ็กต์ของ ImapFolderInfoCollection ประเภท. ทำการวนซ้ำผ่านชุดข้อมูลนี้และรับข้อมูลเกี่ยวกับโฟลเดอร์แต่ละรายการในลูป เมธอดนี้มีการโอเวอร์โหลด คุณสามารถส่งชื่อโฟลเดอร์เป็นพารามิเตอร์เพื่อรับรายการโฟลเดอร์ย่อย ตัวอย่างโค้ดต่อไปนี้จะแสดงวิธีดึงข้อมูลโฟลเดอร์จากเซิร์ฟเวอร์ IMAP ด้วย Aspose.Email โดยใช้เมธอดที่อธิบายไว้ในข้อมูล
// Get all folders in the currently subscribed folder
ImapFolderInfoCollection folderInfoColl = client.ListFolders();
// Iterate through the collection to get folder info one by one
foreach (ImapFolderInfo folderInfo in folderInfoColl)
{
// Folder name and get New messages in the folder
Console.WriteLine("Folder name is " + folderInfo.Name);
ImapFolderInfo folderExtInfo = client.GetFolderInfo(folderInfo.Name);
Console.WriteLine("New message count: " + folderExtInfo.NewMessageCount);
Console.WriteLine("Is it readonly? " + folderExtInfo.ReadOnly);
Console.WriteLine("Total number of messages " + folderExtInfo.TotalMessageCount);
}
ลบและเปลี่ยนชื่อโฟลเดอร์
โฟลเดอร์บนเซิร์ฟเวอร์ IMAP สามารถลบหรือเปลี่ยนชื่อด้วยบรรทัดเดียวโดยใช้ Aspose.Email:
- นี้ DeleteFolder() เมธอดรับชื่อโฟลเดอร์เป็นพารามิเตอร์.
- สำหรับ RenameFolder() เมธอด คุณต้องส่งชื่อโฟลเดอร์ปัจจุบันและชื่อโฟลเดอร์ใหม่ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีลบโฟลเดอร์จากเซิร์ฟเวอร์ IMAP และวิธีเปลี่ยนชื่อโฟลเดอร์ การทำงานแต่ละอย่างทำได้ด้วยบรรทัดโค้ดหนึ่งบรรทัด
// Delete a folder and Rename a folder
client.DeleteFolder("foldername");
client.RenameFolder("foldername", "newfoldername");
ทำงานกับโฟลเดอร์กล่องเมลการใช้พิเศษ
บางที่เก็บข้อความ IMAP มีกล่องเมลที่ใช้พิเศษ เช่น ที่ใช้เก็บข้อความร่างหรือข้อความที่ส่งแล้ว ลูกค้าเมลหลายตัวอนุญาตให้ผู้ใช้กำหนดตำแหน่งที่ควรเก็บร่างหรือข้อความที่ส่งแล้ว แต่การกำหนดค่าต้องให้ผู้ใช้รู้ว่ากล่องเมลใดที่เซิร์ฟเวอร์ได้แยกไว้สำหรับวัตถุประสงค์เหล่านี้ Aspose.Email สามารถระบุกล่องเมลพิเศษเหล่านี้โดยใช้ ImapMailboxInfo คลาสเพื่อให้ง่ายต่อการทำงานกับพวกมัน ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีเข้าถึงกล่องเมลพิเศษเหล่านี้โดยใช้ ImapMailboxInfo คลาส.
ImapClient imapClient = new ImapClient();
imapClient.Host = "<HOST>";
imapClient.Port = 993;
imapClient.Username = "<USERNAME>";
imapClient.Password = "<PASSWORD>";
imapClient.SupportedEncryption = EncryptionProtocols.Tls;
imapClient.SecurityOptions = SecurityOptions.SSLImplicit;
ImapMailboxInfo mailboxInfo = imapClient.MailboxInfo;
Console.WriteLine(mailboxInfo.Inbox);
Console.WriteLine(mailboxInfo.DraftMessages);
Console.WriteLine(mailboxInfo.JunkMessages);
Console.WriteLine(mailboxInfo.SentMessages);
Console.WriteLine(mailboxInfo.Trash);
การดำเนินการข้อความภายในโฟลเดอร์
เพิ่มข้อความใหม่ลงในโฟลเดอร์
คุณสามารถเพิ่มข้อความใหม่ลงในโฟลเดอร์โดยใช้ MailMessage และ ImapClient คลาส. สร้างแรก MailMessage อ็อบเจกต์โดยระบุค่าหัวเรื่อง, ผู้รับและผู้ส่ง จากนั้นสมัครสมาชิกโฟลเดอร์และเพิ่มข้อความลงในโฟลเดอร์ ตัวอย่างโค้ดต่อไปนี้จะแสดงวิธีเพิ่มข้อความใหม่ในโฟลเดอร์
// Create a message
MailMessage msg = new MailMessage("user@domain1.com", "user@domain2.com", "subject", "message");
// Subscribe to the Inbox folder and Append the newly created message
client.SelectFolder(ImapFolderInfo.InBox);
client.SubscribeFolder(client.CurrentFolder.Name);
client.AppendMessage(client.CurrentFolder.Name, msg);
เพิ่มหลายข้อความพร้อม MultiConnection
คุณสามารถเพิ่มหลายข้อความโดยใช้ AppendMessages เมธอดที่ให้โดย ImapClient คลาส. AppendMessages เมธอดรับรายการของ MailMessage และเพิ่มเข้ากับโฟลเดอร์ปัจจุบันหากไม่ได้ระบุโฟลเดอร์เป็นพารามิเตอร์ ImapClient ยังรองรับโหมด MultiConnection สำหรับการทำงานที่หนัก ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มหลายข้อความโดยใช้โหมด MultiConnection.
ImapClient imapClient = new ImapClient();
imapClient.Host = "<HOST>";
imapClient.Port = 993;
imapClient.Username = "<USERNAME>";
imapClient.Password = "<PASSWORD>";
imapClient.SupportedEncryption = EncryptionProtocols.Tls;
imapClient.SecurityOptions = SecurityOptions.SSLImplicit;
List<MailMessage> messages = new List<MailMessage>();
for (int i = 0; i < 20; i++)
{
MailMessage message = new MailMessage(
"<EMAIL ADDRESS>",
"<EMAIL ADDRESS>",
"Test Message - " + Guid.NewGuid().ToString(),
"IMAP Group Append with MultiConnection");
messages.Add(message);
}
imapClient.ConnectionsQuantity = 5;
imapClient.UseMultiConnection = MultiConnectionMode.Enable;
imapClient.AppendMessages(messages);
การย้ายข้อความระหว่างโฟลเดอร์
Aspose.Email สำหรับ .NET อนุญาตให้ย้ายข้อความจากโฟลเดอร์กล่องจดหมายหนึ่งไปยังอีกโฟลเดอร์หนึ่งโดยใช้ ImapClient API. การ MoveMessage วิธีการนี้ใช้รหัสข้อความที่ไม่ซ้ำและชื่อโฟลเดอร์ปลายทางสำหรับการย้ายข้อความไปยังโฟลเดอร์ปลายทาง โค้ดตัวอย่างต่อไปนี้แสดงวิธีการย้ายข้อความไปยังโฟลเดอร์กล่องจดหมายอื่น
///<summary>
/// This example shows how to move a message from one folder of a mailbox to another one using the ImapClient API of Aspose.Email for .NET
/// Available from Aspose.Email for .NET 6.4.0 onwards
/// -------------- Available API Overload Members --------------
/// Void ImapClient.MoveMessage(IConnection iConnection, int sequenceNumber, string folderName, bool commitDeletions)
/// Void ImapClient.MoveMessage(IConnection iConnection, string uniqueId, string folderName, bool commitDeletions)
/// Void ImapClient.MoveMessage(int sequenceNumber, string folderName, bool commitDeletions)
/// Void ImapClient.MoveMessage(string uniqueId, string folderName, bool commitDeletions)
/// Void ImapClient.MoveMessage(IConnection iConnection, int sequenceNumber, string folderName)
/// Void ImapClient.MoveMessage(IConnection iConnection, string uniqueId, string folderName)
/// Void ImapClient.MoveMessage(int sequenceNumber, string folderName)
/// Void ImapClient.MoveMessage(string uniqueId, string folderName)
///</summary>
using (ImapClient client = new ImapClient("host.domain.com", 993, "username", "password"))
{
string folderName = "EMAILNET-35151";
if (!client.ExistFolder(folderName))
client.CreateFolder(folderName);
try
{
MailMessage message = new MailMessage(
"from@domain.com",
"to@domain.com",
"EMAILNET-35151 - " + Guid.NewGuid(),
"EMAILNET-35151 ImapClient: Provide option to Move Message");
client.SelectFolder(ImapFolderInfo.InBox);
// Append the new message to Inbox folder
string uniqueId = client.AppendMessage(ImapFolderInfo.InBox, message);
ImapMessageInfoCollection messageInfoCol1 = client.ListMessages();
Console.WriteLine(messageInfoCol1.Count);
// Now move the message to the folder EMAILNET-35151
client.MoveMessage(uniqueId, folderName);
client.CommitDeletes();
// Verify that the message was moved to the new folder
client.SelectFolder(folderName);
messageInfoCol1 = client.ListMessages();
Console.WriteLine(messageInfoCol1.Count);
// Verify that the message was moved from the Inbox
client.SelectFolder(ImapFolderInfo.InBox);
messageInfoCol1 = client.ListMessages();
Console.WriteLine(messageInfoCol1.Count);
}
finally
{
try { client.DeleteFolder(folderName); }
catch { }
}
}
คัดลอกข้อความระหว่างโฟลเดอร์
Aspose.Email API มีความสามารถในการคัดลอกข้อความจากโฟลเดอร์กล่องจดหมายหนึ่งไปยังอีกโฟลเดอร์หนึ่ง โดยสามารถคัดลอกข้อความเดี่ยวหรือหลายข้อความโดยใช้ คัดลอกข้อความ และ CopyMessages เมธอด. CopyMessages เมธอดให้ความสามารถในการคัดลอกหลายข้อความจากโฟลเดอร์ต้นทางของกล่องเมลไปยังโฟลเดอร์กล่องเมลปลายทาง ตัวอย่างโค้ดต่อไปนี้แสดงวิธีคัดลอกข้อความไปยังโฟลเดอร์กล่องเมลอื่น.
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(),
"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(),
"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 the CopyMessages command and Verify that messages are copied to the destination folder
client.CopyMessages(new[] { uniqueId1, uniqueId2 }, folderName);
client.SelectFolder(folderName);
msgsColl = client.ListMessages();
foreach (ImapMessageInfo msgInfo in msgsColl)
Console.WriteLine(msgInfo.Subject);
}
finally
{
try
{
client.DeleteFolder(folderName);
}
catch { }
}
}