1

I need to process raw email messages and pull out the To and From addresses. I could just search for 'To: ' but I'd rather do this in a structured way to avoid issues.

How can I build a MIME message from raw email text, then get the To/From headers?

I've seen MimeKit used for sending emails before but don't seem to be able to find a way to hydrate MimeKit message object from a raw string so I can pull out the To/From headers. I get this runtime error if I just try passing raw email text into the constructor:

Exception: System.ArgumentException: Unknown initialization parameter: System.String
   at MimeKit.MimeMessage..ctor(Object[] args)
   at AWSS3Test.Services.CallS3.ListingObjectsAsync()

Google has returned nothing of use.

It doesn't have to be MIMEKit, but how can I do this?

niico
  • 11,206
  • 23
  • 78
  • 161
  • 1
    Have you seen the documentation? https://github.com/jstedfast/MimeKit#parsing-messages You'll have to either wrap your message with a stream or read it directly from disk – phuzi Dec 11 '20 at 10:34
  • Your link didn't work but you set me on the right track thanks. I'll answer my own question, needs to be a stream and use Load method not constructor. – niico Dec 11 '20 at 10:51

1 Answers1

2

It needs to use .Load not the constructor and string needs to be converted to a stream

var message = new MimeMessage();
byte[] byteArray = Encoding.ASCII.GetBytes(TestEmail);
MemoryStream stream = new MemoryStream(byteArray);
message = MimeMessage.Load(stream);
niico
  • 11,206
  • 23
  • 78
  • 161