**ניהול, העברה וארגון תיקיות IMAP**
פעולות על תיקיות
קבלת מידע על תיקייה
קבלת מידע על תיקיות משרת IMAP היא קלה מאוד עם Aspose.Email. קרא ל- ListFolders() שיטה של ImapClient מחלקה. היא מחזירה אובייקט של אוסף מידע תיקייה Imap סוג. עבר על האוסף הזה וקבל מידע על כל תיקייה בנפרד בלולאה. המתודה מעודנת. ניתן להעביר שם תיקייה כפרמטר כדי לקבל רשימת תתי‑תיקיות. קטע הקוד הבא מראה כיצד לקבל מידע על תיקיות משרת 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 { }
}
}
העתקת הודעות בין תיקיות
API של Aspose.Email מספקת את האפשרות להעתיק הודעה מתיקיית תיבת דואר אחת לאחרת. היא מאפשרת העתקת הודעה אחת או מספר הודעות באמצעות ה‑ CopyMessage ו 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 { }
}
}