使用mailkit发送的文件夹为空

问题描述 投票:0回答:3

我试图显示已发送的文件夹,但它显示该文件夹中没有子文件夹。除收件箱外,所有文件夹都是空的。我正在使用以下代码。

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 确实检测到它并添加到已发送文件夹中。

c# email mailkit
3个回答
5
投票

来自 MailKit 自述文件:

如果 IMAP 服务器支持 SPECIAL-USE 或 XLIST (GMail) 扩展,您可以获取预定义的“全部”、“草稿”、“标记”(又名“重要”)、“垃圾”、“已发送”、“垃圾箱”等文件夹,如下所示:

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 服务器不支持 SPECIAL-USE 或 XLIST 扩展,您必须想出自己的启发法来获取“已发送”、“草稿”、“垃圾箱”等文件夹。例如,您可能会使用与此类似的逻辑:

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));
}

另一个选项可能是允许应用程序的用户配置他或她想要用作其“已发送”文件夹、“草稿”文件夹、“废纸篓”文件夹等的文件夹。

如何处理这件事取决于你。


0
投票

必须打开该文件夹,否则会显示为空。

                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}");

                }

0
投票

除了 @jstedfast 的精彩答案之外,这对我也有用。我正在连接到 Microsoft Exchange IMAP,它似乎不支持 SpecialUse 或 XLIST。它确实本地化文件夹名称,这可能会使使用硬编码的文件夹名称启发式变得不可靠。

请注意,我的示例查找垃圾文件夹而不是发送的文件夹(在专业版上,它在生产中进行了测试,与此处所写的完全一样)。

private async Task<IMailFolder?> GetTrashFolderAsync(CancellationToken cancellationToken)
{
    if (_imap.Capabilities.HasFlag(ImapCapabilities.SpecialUse) || _imap.Capabilities.HasFlag(ImapCapabilities.XList))
    {
        return _imap.GetFolder(SpecialFolder.Trash);
    }

    _logger?.LogDebug("IMAP server does not support SpecialUse or XList.");

    var personal = _imap.GetFolder(_imap.PersonalNamespaces[0]);
    foreach (var folder in await personal.GetSubfoldersAsync(false, cancellationToken))
    {
        if (folder.Attributes.HasFlag(FolderAttributes.Trash))
        {
            _logger?.LogInformation("Found trash folder {fullName} {attr}", folder.FullName, folder.Attributes);
            return folder;
        }
    }

    _logger?.LogWarning("Found no IMAP trash folder.");
    return null;
}
© www.soinside.com 2019 - 2024. All rights reserved.