【问题标题】:Add custom header to email using MailKit使用 MailKit 将自定义标题添加到电子邮件
【发布时间】:2017-04-20 22:09:03
【问题描述】:

我目前正在使用以下代码下载邮件,为其添加自定义标头,然后将该邮件添加回邮件文件夹:

using (ImapClient imap = new ImapClient())
{
    imap.ServerCertificateValidationCallback = (s, c, h, e) => true;
    imap.Connect(host, port, useSSL);

    imap.Authenticate(user, password);

    IMailFolder mailFolder = imap.GetFolder(folder);
    mailFolder.Open(FolderAccess.ReadWrite);

    if (mailFolder.Count > 0)
    {
        MimeMessage message = mailFolder.GetMessage(0);

        var header = message.Headers.FirstOrDefault(h => h.Field == "X-SomeCustomHeader");
        if (header == null)
        {
            message.Headers.Add("X-SomeCustomHeader", "SomeValue");
        }

        mailFolder.SetFlags(0, MessageFlags.Deleted, true);
        mailFolder.Expunge();

        UniqueId? newUid = mailFolder.Append(message);
        mailFolder.Expunge();

        var foundMails = mailFolder.Search(SearchQuery.HeaderContains("X-SomeCustomHeader", "SomeValue"));
        if (foundMails.Count > 0)
        {
            var foundMail = mailFolder.GetMessage(new UniqueId(foundMails.First().Id));

            Console.WriteLine(foundMail.Subject);
        }

        mailFolder.Close(true);
    }
}

此代码的问题在于,如果我在文件夹中查看电子邮件的来源,则标题不存在并且foundMails 的计数为零。

如果我查看message,它包含标题,所以如果我也查看message.WriteTo(somePath);,标题也在那里。

我做错了什么?

如果我使用 Outlook 客户端,此代码可以工作,但在 gmail 客户端上使用它会失败。

【问题讨论】:

  • Append 方法是否返回 uid(或 null)?如果它返回一个 uid,如果你这样做 mailFolder.GetMessage(uid.Value) 会发生什么? 那个消息有标题吗?
  • 还可以尝试获取ProtocolLog,以查看在附加到文件夹时是否包含标题。
  • @jstedfast 它返回一个 uid 但消息也不包含标题。
  • @jstedfast 通过我的测试,这似乎是 gmail 独有的,但不是 100%。
  • 协议日志应该有助于确认这一点。

标签: c# mailkit


【解决方案1】:

问题是在 gmail 服务器上调用删除实际上并没有删除电子邮件。要解决此问题,请将电子邮件移至垃圾箱文件夹,然后将其删除。以下辅助方法让您了解如何完成此操作:

protected void DeleteMessage(ImapClient imap, IMailFolder mailFolder, UniqueId uniqueId)
{
    if (_account.HostName.Equals("imap.gmail.com"))
    {
        IList<IMessageSummary> summaries = mailFolder.Fetch(new List<UniqueId>() { uniqueId }, MessageSummaryItems.GMailMessageId);
        if (summaries.Count != 1)
        {
            throw new Exception("Failed to find the message in the mail folder.");
        }

        mailFolder.MoveTo(uniqueId, imap.GetFolder(SpecialFolder.Trash));
        mailFolder.Close(true);

        IMailFolder trashMailFolder = imap.GetFolder(SpecialFolder.Trash);
        trashMailFolder.Open(FolderAccess.ReadWrite);

        SearchQuery query = SearchQuery.GMailMessageId(summaries[0].GMailMessageId.Value);

        IList<UniqueId> matches = trashMailFolder.Search(query);

        trashMailFolder.AddFlags(matches, MessageFlags.Deleted, true);
        trashMailFolder.Expunge(matches);

        trashMailFolder.Close(true);

        mailFolder.Open(FolderAccess.ReadWrite);
    }
    else
    {
        mailFolder.SetFlags(uniqueId, MessageFlags.Deleted, true);
        mailFolder.Expunge();
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-11
    • 2011-03-06
    • 2019-11-20
    • 2021-11-06
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2011-01-31
    • 2015-12-21
    相关资源
    最近更新 更多