1

我们正在使用 sendgrid 服务来发送和接收来自客户的电子邮件,并且需要从 Asp.Net Core Web API C# 应用程序中的客户邮件响应中解析邮件原始标头对象(消息 ID、日期等)。

请查找电子邮件原始标头的示例。

    Received: by mx0047p1mdw1.sendgrid.net with SMTP id 6WCVv7KAWn Wed, 27 Jul 2016 20:53:06 +0000 (UTC)
    Received: from mail-io0-f169.google.com (mail-io0-f169.google.com [209.85.223.169]) by mx0047p1mdw1.sendgrid.net (Postfix) with ESMTPS id AA9FFA817F2 for <example@example.comom>; Wed, 27 Jul 2016 20:53:06 +0000 (UTC)
    Received: by mail-io0-f169.google.com with SMTP id b62so81593819iod.3 for <example@example.comom>; Wed, 27 Jul 2016 13:53:06 -0700 (PDT)
    DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sendgrid.com; s=ga1; h=mime-version:from:date:message-id:subject:to; bh=DpB1CYYeumytcPF3q0Upvx3Sq/oF4ZblEwnuVzFwqGI=; b=GH5YTxjt6r4HoTa+94w6ZGQszFQSgegF+Jlv69YV76OLycJI4Gxdwfh6Wlqfez5yID 5dsWuqaVJZQyMq/Dy/c2gHSqVo60BKG56YrynYeSrMPy8abE/6/muPilYxDoPoEyIr/c UXH5rhOKjmJ7nICKu1o99Tfl0cXyCskE7ERW0=
    X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=DpB1CYYeumytcPF3q0Upvx3Sq/oF4ZblEwnuVzFwqGI=; b=Sq6LVHbmywBdt3sTBn19U8VOmelfoJltz8IcnvcETZsYwk96RBxN+RKMN5fOZSKw4j 15HrgdIFfyDmp67YK0ygvOITlTvZ6XY5I0PtnvDtAQt79kS3tKjI3QKJoEp/ZjIjSzlL KG7agl6cxFgBbIN0yHWBOvy3O+ZXY8tZdom1yOvULjmjW1U9JkdOs+aJ6zq4qhZX/RM/ tIgLB461eJ5V95iQDDc5Ibj9Cvy4vJfXLQRO0nLVQAT2Yz58tkEO1bDZpWOPAyUNneIL yhIWp+SpbuqhMA68mq0krG1PjmWalUbpVcGJIGuOKB9mQFFo/MqdrUCjvYnyo1jPLPeX psdQ==
    X-Gm-Message-State: AEkoousvdxmDoxLlTUYJ1AOmCGJv77xRBBlfKv6YrthH0M2NueMwlOxUD6t8nidE9uonXbdJ/DQy/chmHUnN//a4
    X-Received: by 10.107.6.101 with SMTP id 98mr38024553iog.41.1469652785829; Wed, 27 Jul 2016 13:53:05 -0700 (PDT)
    MIME-Version: 1.0
    Received: by 10.107.48.17 with HTTP; Wed, 27 Jul 2016 13:53:05 -0700 (PDT)
    From: Sender Name <example@example.com>
    Date: Wed, 27 Jul 2016 14:53:05 -0600
    Message-ID: <CAN_P_JMvV7ZpAQhOnDienypLrJmuhN=LQWweu4yScw4jQyXY2w@mail.gmail.com>
    Reference:
<CAN_P_JMvV7ZpAAhOnDienypLrJmuhN=LQWweu4yScw4jQyXY2w@mail.gmail.com>
<CAN_P_JMvV7Z6AGhOnDienypLrJmuhN=LQWweu4yScw4jQyXY2w@mail.gmail.com>
    Subject: Different File Types
    To: example@example.comom
    Content-Type: multipart/mixed; boundary=001a113f8ad03e85160538a4343c

我尝试使用 mimekit nuget 获取上述所需对象,但面临以下异常。

代码:

MimeMessage msg = new MimeMessage(/* Above strings */);

例外: 在此处输入图像描述

4

2 回答 2

2

您错误地使用了 MimeKit。

以下是使用 MimeKit 解析这些标头的方法:

using (var stream = File.Open (fileName)) {
    var headers = HeaderList.Load (stream);

    Console.WriteLine ("Message-Id: {0}", headers[HeaderId.MessageId]);
    Console.WriteLine ("Subject: {0}", headers[HeaderId.Subject]);
    Console.WriteLine ("Date: {0}", headers[HeaderId.Date]);
}

或者,如果标头已经在字符串中,您可以执行以下操作:

using (var memory = new MemoryStream (Encoding.UTF8.GetBytes (stringValue), false)) {
    var headers = HeaderList.Load (stream);
}

如果您想使用方便的 MimeMessage API 来读取预解析的电子邮件地址、引用等,那么您需要做的就是确保标题输入以双组换行符(\r\n\r\n\n\n)结尾。

/* the last header should already end with "\r\n", so just add one more: */
using (var memory = new MemoryStream (Encoding.UTF8.GetBytes (stringValue + "\r\n"), false)) {
    var message = MimeMessage.Load (stream);

    Console.WriteLine ("Message-Id: {0}", message.MessageId);
    Console.WriteLine ("Subject: {0}", message.Subject);
    Console.WriteLine ("Date: {0}", message.Date);
}
于 2019-08-01T11:41:30.267 回答
1

尝试以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication123
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "Received: by mx0047p1mdw1.sendgrid.net with SMTP id 6WCVv7KAWn Wed, 27 Jul 2016 20:53:06 +0000 (UTC)\n" +
                "Received: from mail-io0-f169.google.com (mail-io0-f169.google.com [209.85.223.169]) by mx0047p1mdw1.sendgrid.net (Postfix) with ESMTPS id AA9FFA817F2 for <example@example.comom>; Wed, 27 Jul 2016 20:53:06 +0000 (UTC)\n" +
                "Received: by mail-io0-f169.google.com with SMTP id b62so81593819iod.3 for <example@example.comom>; Wed, 27 Jul 2016 13:53:06 -0700 (PDT)\n" +
                "DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sendgrid.com; s=ga1; h=mime-version:from:date:message-id:subject:to; bh=DpB1CYYeumytcPF3q0Upvx3Sq/oF4ZblEwnuVzFwqGI=; b=GH5YTxjt6r4HoTa+94w6ZGQszFQSgegF+Jlv69YV76OLycJI4Gxdwfh6Wlqfez5yID 5dsWuqaVJZQyMq/Dy/c2gHSqVo60BKG56YrynYeSrMPy8abE/6/muPilYxDoPoEyIr/c UXH5rhOKjmJ7nICKu1o99Tfl0cXyCskE7ERW0=\n" +
                "X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=DpB1CYYeumytcPF3q0Upvx3Sq/oF4ZblEwnuVzFwqGI=; b=Sq6LVHbmywBdt3sTBn19U8VOmelfoJltz8IcnvcETZsYwk96RBxN+RKMN5fOZSKw4j 15HrgdIFfyDmp67YK0ygvOITlTvZ6XY5I0PtnvDtAQt79kS3tKjI3QKJoEp/ZjIjSzlL KG7agl6cxFgBbIN0yHWBOvy3O+ZXY8tZdom1yOvULjmjW1U9JkdOs+aJ6zq4qhZX/RM/ tIgLB461eJ5V95iQDDc5Ibj9Cvy4vJfXLQRO0nLVQAT2Yz58tkEO1bDZpWOPAyUNneIL yhIWp+SpbuqhMA68mq0krG1PjmWalUbpVcGJIGuOKB9mQFFo/MqdrUCjvYnyo1jPLPeX psdQ==\n" +
                "X-Gm-Message-State: AEkoousvdxmDoxLlTUYJ1AOmCGJv77xRBBlfKv6YrthH0M2NueMwlOxUD6t8nidE9uonXbdJ/DQy/chmHUnN//a4\n" +
                "X-Received: by 10.107.6.101 with SMTP id 98mr38024553iog.41.1469652785829; Wed, 27 Jul 2016 13:53:05 -0700 (PDT)\n" +
                "MIME-Version: 1.0\n" +
                "Received: by 10.107.48.17 with HTTP; Wed, 27 Jul 2016 13:53:05 -0700 (PDT)\n" +
                "From: Sender Name <example@example.com>\n" +
                "Date: Wed, 27 Jul 2016 14:53:05 -0600\n" +
                "Message-ID: <CAN_P_JMvV7ZpAQhOnDienypLrJmuhN=LQWweu4yScw4jQyXY2w@mail.gmail.com>\n" +
                "Subject: Different File Types\n" +
                "To: example@example.comom\n" +
                "Content-Type: multipart/mixed; boundary=001a113f8ad03e85160538a4343c\n";

            Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
            string pattern = "^(?'key'[^:]+):(?'value'.*)";

            StringReader reader = new StringReader(input);
            string line = "";
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();
                if ((line.Length > 0) && line.Contains(":"))
                {
                    Match match = Regex.Match(line, pattern);
                    string key = match.Groups["key"].Value.Trim();
                    string value = match.Groups["value"].Value.Trim();
                    if (!dict.ContainsKey(key))
                    {
                        dict.Add(key, new List<string>() { value });
                    }
                    else
                    {
                        dict[key].Add(value);
                    }

                }
            }
        }
    }

}
于 2019-08-01T09:22:26.670 回答