Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component

Index of Chilkat Blog Posts

March 19, 2008

Loading an Email Object from a Byte Array

This C# example demonstrates how to load an email object from a byte array:

private void LoadEmailFromByteArray()
    // First, load an email from a file into a byte array.
    // (This example really begins after we have the exact bytes
    // from the file in a byte array.)
    byte[] emailBytes = System.IO.File.ReadAllBytes("email.eml");
    // There are two approaches.
    // (1) Convert the byte array to a string and then call
    // Chilkat.Email.SetFromMimeText to load it into an email object.
    // However, if an 8bit encoding is used, you'll need to know
    // the character encoding in order to convert the bytes to
    // a string. This is the reason for 7bit (quoted-printable) encodings
    // in MIME -- so you don't have to deal with this mess.
    // If the content-type of each part within the MIME is quoted-printable
    // or base64, you can assume us-ascii.
    // (2) A second approach is to use Chilkat.Mime to load the byte array
    // into a Chilkat.Mime object and then convert that to a Chilkat.Email
    // object.  The Chilkat.Mime object provides a LoadMimeBytes method intended
    // to handle binary MIME and MIME with 8bit content-types.
    // Approach #1: Convert to a string and load into a Chilkat.Email object:
    string mimeStr = System.Text.Encoding.ASCII.GetString(emailBytes);
    Chilkat.Email email = new Chilkat.Email();
    // Approach #2: Load bytes into Chilkat.Mime, then convert to Chilkat.Email:
    Chilkat.Mime mime = new Chilkat.Mime();
    // The following two methods return true/false success statuses, and they really should be checked...
    mime.UnlockComponent("Anything for 30-day trial");

Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.