Parse MIME string to retrieve message information

Share

The purpose is to gain access to the message information in code by extracting it from a
raw email message. For this we will parse the content of the email using the IMessage interface.

We assume we have a string containing a MIME encoded email, which we will send as input parameter to the constructor of our parsing class. Using an ADODB object, will write the string directly into a stream and then flush it in order to access the message.

  public class EmailParser
  {
    private Message _msg = null;
 
    public EmailParser(string sContent)
    {
        ADODB.StreamClass strc = new ADODB.StreamClass();
 
        strc.Open(Missing.Value,
            ADODB.ConnectModeEnum.adModeUnknown,
            ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified,
            "",
            "");
        strc.Type = ADODB.StreamTypeEnum.adTypeText;
        strc.Charset = "UTF-8";
        strc.WriteText(sContent, ADODB.StreamWriteEnum.adWriteChar);
        strc.Position = 0;
        strc.Type = StreamTypeEnum.adTypeBinary;
        strc.Flush();
        _msg = new CDO.MessageClass();
        _msg.DataSource.OpenObject(strc, "_Stream");
    }
}

The CDO Message object provides access to the email header information, bodyparts and attachments. We can then simply expose the needed properties:

    public string Subject
    {
      get { return _msg.Subject; }
    }
    public string From
    {
      get { return  _msg.From; }
    }
    public string To
    {
      get { return _msg.To }
    }
    public string ReplyTo
    {
      get { return _msg.ReplyTo; }
    }
    public string CC
    {
      get { return _msg.CC; }
    }
    public DateTime SentOn
    {
      get { return _msg.SentOn; }
    }
    public DateTime ReceivedTime
    {
      get { return _msg.ReceivedTime; }
    }

Excepting messages with a single text body part, in which case the IMessage.Fields collection can be used directly, to proper retrieve the body message, one should iterate through the BodyPart objects that make up the hierarchy of content and structural units of a message.
Another particularity are the message attachments. A simple way to retrieve them is the following:

    foreach (CDO.IBodyPart bp in _msg.Attachments)
    {
      ADODB.Stream stm = bp.GetDecodedContentStream();
      byte[] bf = (byte[])stm.Read(stm.Size);
      string sFileName = bp.FileName;
 
      if (bp.FileName == "")
      {
        switch (bp.ContentMediaType.Trim().ToLower())
        {
          case "image/jpeg":
            sFileName = "unknown" + nEmbeddedUnknownCounter + ".jpg";
            break;
          case "image/gif":
            sFileName = "unknown" + nEmbeddedUnknownCounter + ".gif";
            break;
          default:
            sFileName = "unknown" + nEmbeddedUnknownCounter;
            break;
        }
      }
    }

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

1 thought on “Parse MIME string to retrieve message information”
  • Matt Call says:

    Thanx for this interesting post. But I had difficult time navigating around your website because I kept getting 502 bad gateway error. Just thought to let you know.

    November 15, 2009 at 10:42 pm

Comments are closed.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close