SmtpMail.AddAttachment Method


Attaches a file to the e-mail message.

[Visual Basic]
Public Sub AddAttachment( _
    fileName As String _
) As Attachment

Public Sub AddAttachment( _
    fileName As String, _
    content() As Byte _
) As Attachment
[C#]
public Attachment AddAttachment(
    string fileName
);

public Attachment AddAttachment(
    string fileName,
    byte[] content
);
[C++]
public: Attachment* AddAttachment(
    String* fileName
);

public: Attachment* AddAttachment(
    String* fileName,
    unsigned char content __gc[],
);
[JScript]
public function AddAttachment( 
    fileName : String
) : Attachment;

public function AddAttachment( 
    fileName : String,
    content : Byte[]
) : Attachment;

Parameters

fileName
A full file name or URL.
content
The binary data of the file.

Return Value

The Attachment class instance.

Remarks

To attach embedded pictures, SmtpMail.ImportHtmlBody and SmtpMail.ImportHtml methods are strongly recommended.

Example

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

[Visual Basic]
Imports EASendMail

Public Sub AttachFile()
    Try
        Dim oMail As SmtpMail = New SmtpMail("TryIt")
        Dim oAttachment As Attachment = oMail.AddAttachment( "c:\test.gif" )
        
        'adds attachment from remote host
        'Dim oAttachment As Attachment = oMail.AddAttachment( "http://www.adminsystem.com/test.gif" )
        
        'changes attachment name to another name
        'oAttachment.Name = "mytest.gif"
        
        'specifies the attachment as an embedded picture
        'Dim contentID As String = "test001@host"
        'oAttachment.ContentID = contentID
        'oMail.HtmlBody = "<html><body>this is a <img src=""cid:" & contentID & """> embedded picture.</body></html>"
        
        'save as an eml file, you can use outlook express to open and check it.
        oMail.SaveAs( "c:\test.eml", True )
        
    Catch exp As System.Exception
        MessageBox.Show(exp.Message)
    End Try
End Sub

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

public void AttachFile()
{
    try
    {	  
        SmtpMail oMail = new SmtpMail("TryIt");
        Attachment oAttachment = oMail.AddAttachment( "c:\\test.gif" );
        
        //adds attachment from remote website
        //Attachment oAttachment = oMail.AddAttachment( "http://www.adminsystem.com/test.gif" );
        
        //changes attachment name to another name
        //oAttachment.Name = "mytest.gif";
        
        //specifies the attachment as an embedded picture
        //string contentID = "test001@host";
        //oAttachment.ContentID = contentID;
        //oMail.HtmlBody = "<html><body>this is a <img src=\"cid:" + contentID + "\"> embedded picture.</body></html>";
        
        //save as an eml file, you can use outlook express to open and check it.
        oMail.SaveAs( "c:\\test.eml", true );   
    }
    catch( System.Exception exp )
    {
        MessageBox.Show( exp.Message );           
    }
}

[C++]
using namespace System;
using namespace System::Collections;
using namespace EASendMail; 

System::Void AttachFile()
{
    try
    {
        SmtpMail *oMail = new SmtpMail(S"TryIt");
        Attachment *oAttachment = oMail->AddAttachment( S"c:\\test.gif" );
        
        //adds attachment from remote website
        //Attachment *oAttachment = oMail->AddAttachment( S"http://www.adminsystem.com/test.gif" );
        
        //changes attachment name to another name
        //oAttachment->Name = S"mytest.gif";
        
        //specifies the attachment as an embedded picture
        //String* contentID = S"test001@host";
        //oAttachment->ContentID = contentID;
        //oMail->HtmlBody = String::Format( S"<html><body>this is a <img src=\"cid:{0}\"> embedded picture.</body></html>",
            contentID );
        
        //save as an eml file, you can use outlook express to open and check it.
        oMail->SaveAs( S"c:\\test.eml", true );
    }
    catch( System::Exception *exp )
    {
        MessageBox::Show( exp->Message );            
    }
}

[JScript]
public function AttachFile()
{  
    try
    {   
        var oMail:SmtpMail =  new SmtpMail("TryIt");
         Attachment oAttachment = oMail.AddAttachment( "c:\\test.gif" );
        
        //adds attachment from remote website 
        //Attachment oAttachment = oMail.AddAttachment( "http://www.adminsystem.com/test.gif" );
        
        //changes attachment name to another name
        //oAttachment.Name = "mytest.gif";
        
        //specifies the attachment as an embedded picture
        //var contentID : String = "test001@host";
        //oAttachment.ContentID = contentID;
        //oMail.HtmlBody = "<html><body>this is a <img src=\"cid:" + contentID + "\"> embedded picture.</body></html>";
        
        //save as an eml file, you can use outlook express to open and check it.
        oMail.SaveAs( "c:\\test.eml" );        
    }
    catch( exp:System.Exception )
    {
        MesageBox.Show( exp.Message );           
    }   
    
}