Attachment Class


Provides properties and methods for presenting an e-mail attachment.

System.Object
    EAGetMail.Attachment

[Visual Basic]
Public Class Attachment
[C#]
public class Attachment
[C++]
public __gc class Attachment
[JScript]
public class Attachment

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Public Constructors

Attachment Constructor Initializes a new instance of the Attachment class.

Public Properties

Content Gets the Content (binary data) of the embedded attachment.
ContentID Gets the Content-ID of the embedded attachment.
ContentType Gets the Content-Type of the attachment.
EncodedContent Gets the enconded content (raw data) of the attachment.
Headers Gets the HeaderCollection for headers of the e-mail message.
Name Gets the name of the attachment.

Public Methods

SaveAs Saves the attachment to a local file.

Remarks

It is not recommended to construct Attachment class instance directly. To get attachments of email, please use Mail.Attachments property, this property returns an array for Attachment class instances.

Example

[Visual Basic, C#, C++, JScript.NET] To get the full samples of EAGetMail, please refer to Samples section.

[Visual Basic]
Imports EAGetMail

Public Sub ParseAttachment()
    Dim oMail As New Mail("TryIt")
    oMail.Load("c:\test.eml", False)
    Dim atts() As Attachment = oMail.Attachments
    Dim tempFolder As String = "c:\temp"
    Dim count As Integer = atts.Length
    If (Not System.IO.Directory.Exists(tempFolder)) Then
        System.IO.Directory.CreateDirectory(tempFolder)
    End If

    For i As Integer = 0 To count - 1
        Dim att As Attachment = atts(i)
        'this attachment is in OUTLOOK RTF format, decode it here.
        If (String.Compare(att.Name, "winmail.dat") = 0) Then
            Dim tatts() As Attachment
            Try
                tatts = Mail.ParseTNEF(att.Content, True)
                Dim y As Integer = tatts.Length
                For x As Integer = 0 To y - 1
                    Dim tatt As Attachment = tatts(x)
                    Dim tattname As String = String.Format("{0}\{1}", tempFolder, tatt.Name)
                    tatt.SaveAs(tattname, True) 
                Next
            Catch ep As Exception
                Console.WriteLine(ep.Message)
            End Try
        Else
            Dim attname As String = String.Format("{0}\{1}", tempFolder, att.Name)
            att.SaveAs(attname, True)
        End If
    Next
End Sub

[C#]
using System;
using System.Collections;
using EAGetMail;

public void ParseAttachment()
{
    Mail oMail = new Mail("TryIt");
    oMail.Load( "c:\\test.eml", false );

    Attachment [] atts = oMail.Attachments;
    int count = atts.Length;
    string  tempFolder = "c:\\temp";
    if( !System.IO.Directory.Exists( tempFolder ))
        System.IO.Directory.CreateDirectory( tempFolder );

    for( int i = 0; i < count; i++ )
    {
        Attachment att = atts[i];
        //this attachment is in OUTLOOK RTF format, decode it here.
        if( String.Compare( att.Name, "winmail.dat" ) == 0 )
        {
            Attachment[] tatts = null;
            try
            {
                tatts = Mail.ParseTNEF( att.Content, true );
            }
            catch(Exception ep )
            {
                Console.WriteLine( ep.Message );
                continue;
            }
            int y = tatts.Length;
            for( int x = 0; x < y; x++ )
            {
                Attachment tatt = tatts[x];
                string tattname = String.Format( "{0}\\{1}", tempFolder, tatt.Name );
                tatt.SaveAs( tattname , true );
            }
            continue;
        }
        string attname = String.Format( "{0}\\{1}", tempFolder, att.Name );
        att.SaveAs( attname , true );
    }
}

[C++]
using namespace System;
using namespace EAGetMail;

Void ParseAttachment()
{
    Mail *oMail = new Mail(S"TryIt");
    oMail->Load( S"c:\\test.eml", false );

    Attachment * atts[]= oMail->Attachments;
    int count = atts->Length;
    String  *tempFolder = S"c:\\temp";
    if( !System::IO::Directory::Exists( tempFolder ))
        System::IO::Directory::CreateDirectory( tempFolder );

    for( int i = 0; i < count; i++ )
    {
        Attachment *att = atts[i];
        //this attachment is in OUTLOOK RTF format, decode it here.
        if( String::Compare( att->Name, "winmail.dat" ) == 0 )
        {
            Attachment *tatts[]= NULL;
            try
            {
                tatts = Mail::ParseTNEF( att->Content, true );
            }
            catch(Exception *ep )
            {
                Console::WriteLine( ep->Message );
                continue;
            }
            int y = tatts->Length;
            for( int x = 0; x < y; x++ )
            {
                Attachment *tatt = tatts[x];
                String *tattname = String::Format( S"{0}\\{1}", tempFolder, tatt->Name );
                tatt->SaveAs( tattname , true );
            }
            continue;
        }
        String *attname = String::Format( S"{0}\\{1}", tempFolder, att->Name );
        att->SaveAs( attname, true );
    }
}

[JScript]
function ParseAttachment()
{
    var oMail:Mail = new Mail("TryIt");
    oMail.Load( "c:\\test.eml", false );

    var atts:Attachment[] = oMail.Attachments;
    var count:int = atts.Length;
    var tempFolder:String = "c:\\temp";
    if( !System.IO.Directory.Exists( tempFolder ))
        System.IO.Directory.CreateDirectory( tempFolder );

    for( var i:int = 0; i < count; i++ )
    {
        var att:Attachment = atts[i];
        //this attachment is in OUTLOOK RTF format, decode it here.
        if( String.Compare( att.Name, "winmail.dat" ) == 0 )
        {
            var tatts:Attachment[] = null;
            try
            {
                tatts = Mail.ParseTNEF( att.Content, true );
            }
            catch(ep:Exception)
            {
                Console.WriteLine( ep.Message );
                continue;
            }
            var y:int = tatts.Length;
            for( var x:int = 0; x < y; x++ )
            {
                var tatt:Attachment = tatts[x];
                var tattname:String = String.Format( "{0}\\{1}", tempFolder, tatt.Name );
                tatt.SaveAs( tattname , true );
            }
            continue;
        }
        var attname:String = String.Format( "{0}\\{1}", tempFolder, att.Name );
        att.SaveAs( attname , true );
    }
}