【问题标题】:C# get all headers using maikitC# 使用 maikit 获取所有标头
【发布时间】:2018-05-24 13:18:31
【问题描述】:

我正在尝试连接到 pop3 帐户并获取包含所有标题的所有电子邮件。我使用 mailkit 在 C# 中执行此任务。 问题是我搜索了如何在一个字符串中获取所有标题的示例,但我没有找到这样做的方法。

using (var client = new Pop3Client())
{
    try
    {
        client.ServerCertificateValidationCallback = (s, c, h, e) => true;
        client.Connect("pop3.host.com", 110, false);
        client.AuthenticationMechanisms.Remove("XOAUTH2");
        client.Authenticate("mail@host.com", "password");
        for (int i = 0; i < client.Count; i++)
        {
                    // below I have the problem. I dont know how request the complete headers of a message/mail
                    header = client.getGetMessageHeaders(i).ToArray;
                    Console.WriteLine("..." + header);
        }
        client.Disconnect(true);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Checking error: \n\n" + ex.Message + "\n\n\n");
    }
}

如您所见,我尝试在数组中获取标题,以便在使用此数组为字符串提供标题后,但无法使用此代码。我是 C# 新手,我在这里请求一些帮助。

非常感谢!

【问题讨论】:

    标签: c# pop3 mailkit


    【解决方案1】:

    将所有标题序列化为单个字符串的方法是执行以下操作:

    var headers = client.GetMessageHeaders (i);
    using (var stream = new MemoryStream ()) {
        headers.WriteTo (stream);
    
        var bytes = memory.ToArray ();
        var latin1 = Encoding.GetEncoding (28591);
        string header = latin1.GetString (bytes, 0, bytes.length);
    }
    

    但请记住,每个标头可能包含未声明的 8 位“文本”数据,这些数据可能不在 iso-8859-1 甚至 UTF-8 中(它可能在 any编码)。不仅如此,每个包含 8 位“文本”数据的标头甚至可能使用不同的编码。

    试图将其表示为字符串是徒劳的。

    最好将其表示为 Stream 或字节数组。

    如果您只想将标题打印到控制台,您可以这样做:

    using (var client = new Pop3Client())
    {
        try
        {
            client.ServerCertificateValidationCallback = (s, c, h, e) => true;
            client.Connect("pop3.host.com", 110, false);
            client.AuthenticationMechanisms.Remove("XOAUTH2");
            client.Authenticate("mail@host.com", "password");
            for (int i = 0; i < client.Count; i++)
            {
                        header = client.GetMessageHeaders(i);
                        header.WriteTo (Console.OpenStandardOutput ());
            }
            client.Disconnect(true);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Checking error: \n\n" + ex.Message + "\n\n\n");
        }
    }
    

    或者,如果您希望看到 decoded 标头值:

    using (var client = new Pop3Client())
    {
        try
        {
            client.ServerCertificateValidationCallback = (s, c, h, e) => true;
            client.Connect("pop3.host.com", 110, false);
            client.AuthenticationMechanisms.Remove("XOAUTH2");
            client.Authenticate("mail@host.com", "password");
            for (int i = 0; i < client.Count; i++)
            {
                        var headers = client.GetMessageHeaders(i);
                        foreach (var header in headers)
                            Console.WriteLine ("{0}: {1}", header.Field, header.Value);
            }
            client.Disconnect(true);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Checking error: \n\n" + ex.Message + "\n\n\n");
        }
    }
    

    【讨论】:

    • 您的示例非常适合我。我现在就着手处理它。非常感谢我的朋友
    猜你喜欢
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2019-06-30
    • 2011-09-13
    • 2013-10-25
    • 1970-01-01
    相关资源
    最近更新 更多