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

December 18, 2007

Saving Base64 Encoded Email Attachment to Byte Array or Filesystem

The Chilkat Email component makes it so you don’t need to concern yourself with how the internals of an email (i.e. the MIME) is formatted. Regardless of the encoding (base64, quoted-printable, etc) or charset(s) used in the MIME, the coding is the same. For example, this VB.NET code extracts an MS-WORD doc from an email. A method such as GetAttachmentData returns the bytes of the attachment regardless of how it was encoded in the MIME. The same applies to method or properties that return strings. For example, the email.Subject property always returns a Unicode string. It doesn’t matter if the email used a different charset, or if the string was "Q" or "B" encoded in the MIME…

        Dim email As New Chilkat.Email()
        ' How many attachments?
        If (email.NumAttachments < 1) Then
            Exit Sub
        End If
        If (email.GetAttachmentFilename(0).EndsWith(".doc")) Then
            Exit Sub
        End If
        ' Get the 1st attachment (i.e. the attachment at index 0)
        Dim msWordData As Byte()
        msWordData = email.GetAttachmentData(0)
        ' Write the bytes to a file.
        System.IO.File.WriteAllBytes("test.doc", msWordData)
        ' Do the same thing without first copying the attachment to a byte array:
        ' Save this specific attachment:
        email.SaveAttachedFile(0, "c:/myAttachmentsDir")

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.