« Communicating with an IMAP4 server in .NET | Main | Email messages with .NET »

September 14, 2008

Formatting MimeMessages using .NET

Overview

       This article will demonstrate how to format Mime Messages using Email Factory for .NET.

Code Example
      
      
Download licenseEmail.txtDownload MMessage.cs   |  Download MMessage.vb

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Collections;
   4:  using System.Text;
   5:  using Jscape.Email;
   6:  using System.IO;
   7:   
   8:  namespace MMessageNet
   9:  {
  10:      class MMessage
  11:      {
  12:          MimeMessage message = null;
  13:   
  14:          public MMessage()
  15:          {
  16:              message = new MimeMessage();
  17:          }
  18:   
  19:          public MMessage(string filename)
  20:          {
  21:              FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
  22:              BinaryReader r = new BinaryReader(fs);
  23:              byte[] data = r.ReadBytes((int)fs.Length);
  24:              message = new MimeMessage(data);
  25:              fs.Close();
  26:              r.Close();
  27:              fs.Dispose();
  28:          }
  29:   
  30:          public void addHeader(string name, string value)
  31:          {
  32:              message.AddHeader(new MimeHeader(name, value));
  33:          }
  34:   
  35:          public string getHeaderValue(string name)
  36:          {
  37:              return message.GetHeaderValue(name);
  38:          }
  39:   
  40:          public void setBody(string body)
  41:          {
  42:              message.SetBody(body);
  43:          }
  44:   
  45:          public string getBody()
  46:          {
  47:              return message.GetBody();
  48:          }
  49:   
  50:          public void addPart(string filename)
  51:          {
  52:              byte[] data = null;
  53:              StreamReader sr = File.OpenText(filename);
  54:              data = Encoding.Default.GetBytes(sr.ReadToEnd());
  55:              message.AddPart(new MimeMessage(data));
  56:              sr.Close();
  57:              sr.Dispose();
  58:          }
  59:   
  60:          public MimeMessage getPart(int index)
  61:          {
  62:              return message.GetPart(index);
  63:          }
  64:   
  65:          static void Main(string[] args)
  66:          {
  67:              try
  68:              {
  69:                  MMessage m = new MMessage();
  70:                  m.addHeader("X-Mailer", "Microsoft Outlook");
  71:                  Console.WriteLine(m.getHeaderValue("X-Mailer"));
  72:                  m.setBody("Go over status of project.\r\nThanks\r\nBob");
  73:                  Console.WriteLine(m.getBody());
  74:              }
  75:              catch (Exception e)
  76:              {
  77:                  Console.WriteLine(e.Message);
  78:              }
  79:   
  80:              Console.WriteLine("Press any key to exit ... ");
  81:              Console.Read();
  82:          }
  83:      }
  84:  }

  Lines 1-6   : Necessary import statements.
  Line  16    : Initialize MimeMessage instance.
  Lines 19-28 : Initialize MimeMessage instance from filename.
  Lines 30-33 : Add MimeMessage header.
  Lines 35-38 : Get MimeMessage header value.
  Lines 40-43 : Set MimeMessage body.
  Lines 45-48 : Get MimeMessage body.
  Lines 50-58 : Add message part to existing MimeMessage.
  Lines 60-63 : Get MimeMessage part by index.
  Line  69    : Initialize MMessage instance.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e550fd70338833010534a9d02e970c

Listed below are links to weblogs that reference Formatting MimeMessages using .NET:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.