We are using sendgrid services to sending and receive emails from customer and need to parse the mail raw headers objects (Message-ID,Date etc.) from customer mail response in Asp.Net Core WeB API C# application.
Please find the example of Email Raw headers.
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 <[email protected]>; Wed, 27 Jul 2016 20:53:06 +0000 (UTC)
Received: by mail-io0-f169.google.com with SMTP id b62so81593819iod.3 for <[email protected]>; 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 <[email protected]>
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: [email protected]
Content-Type: multipart/mixed; boundary=001a113f8ad03e85160538a4343c
I tried to get the above required objects by using mimekit nuget but facing below exception.
Code:
MimeMessage msg = new MimeMessage(/* Above strings */);
You're using MimeKit incorrectly.
Here's how you would parse those headers with 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]);
}
Or, if the headers are already in a string, you could do something like this:
using (var memory = new MemoryStream (Encoding.UTF8.GetBytes (stringValue), false)) {
var headers = HeaderList.Load (stream);
}
If you would like to use the convenient MimeMessage API's for reading pre-parsed email addresses, references, etc. then all you need to do is make sure that the header input ends with a double set of newlines (\r\n\r\n
or \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);
}