我正在尝试显示已发送的文件夹,但它显示该文件夹中没有子项。除了收件箱之外,所有的文件夹都是空的。我正在使用下面的代码。
using (var client = new ImapClient())
{
client.Connect(credentials.incoming_host, (int)credentials.incoming_port, credentials.incoming_ssl); //for SSL
client.Authenticate(credentials.email, credentials.password);
client.Inbox.Open(FolderAccess.ReadOnly);
var sentFolder= client.GetFolder(MailKit.SpecialFolder.Sent);
var Folders = client.GetFolders(client.PersonalNamespaces[0]);
client.Disconnect(true);
}
我尝试使用相同的文件夹发送电子邮件,然后添加如下内容:
var sentFolder = imapclient.GetFolder(SpecialFolder.Sent);
sentFolder.Append(message);
我的outlook确实检测到了它,并将其添加到已发送文件夹中。
发布于 2019-09-25 19:03:19
从MailKit自述文件中:
如果IMAP服务器支持特殊用途或XLIST (GMail)扩展,您可以获得预定义的All、Draft、标记(又称重要)、Junk、Sent、Trash等文件夹,如下所示:
if ((client.Capabilities & (ImapCapabilities.SpecialUse | ImapCapabilities.XList)) != 0) {
var drafts = client.GetFolder (SpecialFolder.Drafts);
} else {
// maybe check the user's preferences for the Drafts folder?
}
在IMAP服务器不支持特殊用途或XLIST扩展的情况下,您必须想出自己的启发式方法来获取Sent、Draft、Trash等文件夹。例如,您可以使用类似于以下内容的逻辑:
static string[] CommonSentFolderNames = { "Sent Items", "Sent Mail", "Sent Messages", /* maybe add some translated names */ };
static IFolder GetSentFolder (ImapClient client, CancellationToken cancellationToken)
{
var personal = client.GetFolder (client.PersonalNamespaces[0]);
foreach (var folder in personal.GetSubfolders (false, cancellationToken)) {
foreach (var name in CommonSentFolderNames) {
if (folder.Name == name)
return folder;
}
}
return null;
}
使用LINQ,您可以将其简化为以下内容:
static string[] CommonSentFolderNames = { "Sent Items", "Sent Mail", "Sent Messages", /* maybe add some translated names */ };
static IFolder GetSentFolder (ImapClient client, CancellationToken cancellationToken)
{
var personal = client.GetFolder (client.PersonalNamespaces[0]);
return personal.GetSubfolders (false, cancellationToken).FirstOrDefault (x => CommonSentFolderNames.Contains (x.Name));
}
另一种选择可能是允许应用程序的用户配置他或她想要用作其已发送文件夹、草稿文件夹、回收站文件夹等的文件夹。
如何处理这件事由你自己决定。
发布于 2021-02-26 07:56:15
必须打开该文件夹,否则它将显示为空。
IMailFolder personal = client.GetFolder(client.PersonalNamespaces[0]);
foreach (IMailFolder folder in personal.GetSubfolders(false, cancellationToken))
{
folder.Open(FolderAccess.ReadOnly);
Console.WriteLine($"folder.Name = {folder.Name}");
Console.WriteLine($"{folder.Name} messages : {folder.Count}");
}
https://stackoverflow.com/questions/58090638
复制相似问题