You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MailKit复制邮件时遇ArgumentNullException问题求助

解决MailKit复制邮件时的ArgumentNullException问题

问题出在sourceInbox.SearchAsync(SearchOptions.All, null)这行代码——MailKit的SearchAsync方法不允许第二个参数(搜索查询)为null,必须传入有效的SearchQuery对象。

修改后的完整代码

var sourceInbox = sourceClient.Inbox;
await sourceInbox.OpenAsync(FolderAccess.ReadOnly);

var destinationInbox = destinationClient.Inbox;
await destinationInbox.OpenAsync(FolderAccess.ReadWrite);

Console.WriteLine("Znaleziono folder INBOX na serwerze źródłowym.");
Console.WriteLine("Znaleziono folder INBOX na serwerze docelowym.");

// 获取源INBOX中所有邮件的UID集合
var uids = await sourceInbox.SearchAsync(SearchOptions.All, SearchQuery.All);

foreach (var uid in uids.UniqueIds)
{
    var sourceMessage = await sourceInbox.GetMessageAsync(uid);
    await destinationInbox.AppendAsync(sourceMessage);
}

Console.WriteLine("Skopiowano wiadomości z folderu INBOX.");

await sourceClient.DisconnectAsync(true);
await destinationClient.DisconnectAsync(true);
}

关键修改说明

  1. 替换空参数:将SearchAsync的第二个参数从null改为SearchQuery.All,这是MailKit内置的查询条件,用于匹配文件夹内所有邮件,符合方法的参数要求,避免空引用异常。
  2. 清理冗余代码:原代码重复了两次断开客户端连接和输出提示的逻辑,删除重复部分,避免执行冗余操作,保证代码逻辑简洁。

内容的提问来源于stack exchange,提问作者Tomasz Myszak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:22:48