AddAttachment Method


Attach a file or URL as attachment to current email.

[Syntax]
Visual C++: HRESULT AddAttachment( BSTR FileName, long* pVal )
Visual Basic: AddAttachment( FileName As String ) As Long

Parameters

FileName
A full file name or URL.

Return Value

Return zero if successful, or return non-zero if failed.

Remarks

strFile must be a valid local file with full path or a valid URL.
To attach embedded images or inline images, Mail.ImportHtml and Mail.ImportMailEx methods are strongly recommended.

Example

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

[VB6, VBA - Send Email with File Attachment]

Private Sub btnSendMail_Click() 

    Dim oSmtp As New EASendMailObjLib.Mail 
    oSmtp.LicenseCode = "TryIt" 

    ' Set your sender email address
    oSmtp.FromAddr = "test@emailarchitect.net" 

    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0 

    ' Set email subject
    oSmtp.Subject = "test HTML email from VB 6.0 project with attachment" 

    ' Set HTML body format
    oSmtp.BodyFormat = 1 

    ' Set HTML body text
    oSmtp.BodyText = "<font size=5>This is</font> <font color=red><b>a test</b></font>" 

    ' Add attachment from local disk
    If oSmtp.AddAttachment( "c:\test.doc" ) <> 0 Then 
        MsgBox "Failed to add attachment with error:" & oSmtp.GetLastErrDescription() 
    End If 

    ' Add attachment from remote website
    If oSmtp.AddAttachment( "https://www.emailarchitect.net/webapp/img/logo.jpg" ) <> 0 Then 
        MsgBox "Failed to add attachment with error:" & oSmtp.GetLastErrDescription() 
    End If 

    ' Your SMTP server address
    oSmtp.ServerAddr = "smtp.emailarchitect.net" 

    ' User and password for ESMTP authentication
    oSmtp.UserName = "test@emailarchitect.net" 
    oSmtp.Password = "testpassword" 

    ' If your smtp server requires SSL connection, please add this line
    ' oSmtp.SSL_init

    MsgBox "start to send email ..." 

    If oSmtp.SendMail() = 0 Then 
        MsgBox "email was sent successfully!" 
    Else 
        MsgBox "failed to send email with the following error:" & oSmtp.GetLastErrDescription() 
    End If 

End Sub 

[ASP, VBScript - Send Email with File Attachment]

    Dim oSmtp
    Set oSmtp = Server.CreateObject("EASendMailObj.Mail")
    oSmtp.LicenseCode = "TryIt" 

    ' Set your sender email address
    oSmtp.FromAddr = "test@emailarchitect.net" 

    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0 

    ' Set email subject
    oSmtp.Subject = "test HTML email from VB 6.0 project with attachment" 

    ' Set HTML body format
    oSmtp.BodyFormat = 1 

    ' Set HTML body text
    oSmtp.BodyText = "<font size=5>This is</font> <font color=red><b>a test</b></font>" 

    ' Add attachment from local disk
    If oSmtp.AddAttachment( "c:\test.doc" ) <> 0 Then 
        Response.Write "Failed to add attachment with error:" & oSmtp.GetLastErrDescription() 
    End If 

    ' Add attachment from remote website
    If oSmtp.AddAttachment( "https://www.emailarchitect.net/webapp/img/logo.jpg" ) <> 0 Then 
        Response.Write "Failed to add attachment with error:" & oSmtp.GetLastErrDescription() 
    End If 

    ' Your SMTP server address
    oSmtp.ServerAddr = "smtp.emailarchitect.net" 

    ' User and password for ESMTP authentication
    oSmtp.UserName = "test@emailarchitect.net" 
    oSmtp.Password = "testpassword" 

    ' If your smtp server requires SSL connection, please add this line
    ' oSmtp.SSL_init

    Response.Write "start to send email ..." 

    If oSmtp.SendMail() = 0 Then 
        Response.Write "email was sent successfully!" 
    Else 
        Response.Write "failed to send email with the following error:" & oSmtp.GetLastErrDescription() 
    End If 

[Delphi - Send Email with File Attachment]

Unit Unit1; 

Interface 

Uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail Unit 

Type 
  TForm1 = Class(TForm) 
    Button1: TButton; 
    Procedure Button1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  End; 

Var 
  Form1: TForm1; 

Implementation 

{$R *.dfm} 

Procedure TForm1.Button1Click(Sender: TObject); 
Var 
  oSmtp : TMail; 
Begin 
  oSmtp := TMail.Create(Application); 
  oSmtp.LicenseCode := 'TryIt'; 

  // Set your sender email address
  oSmtp.FromAddr := 'test@emailarchitect.net'; 

  // Add recipient email address
  oSmtp.AddRecipientEx( 'support@emailarchitect.net', 0); 

  // Set email subject
  oSmtp.Subject := 'test HTML email from Delphi project'; 

  // Set HTML body format
  oSmtp.BodyFormat := 1; 

  // Set HTML body text
  oSmtp.BodyText := '<font size=5>This is</font> <font color=red><b>a test</b></font>'; 

  // Add attachment from local disk
  If oSmtp.AddAttachment('c:\test.doc') <> 0 Then 
    ShowMessage( 'Failed to add attachment with error: ' + 
    oSmtp.GetLastErrDescription()); 

  // Add attachment from remote website
  If oSmtp.AddAttachment('https://www.emailarchitect.net/webapp/img/logo.jpg') <> 0 Then 
    ShowMessage( 'Failed to add attachment with error: ' + 
    oSmtp.GetLastErrDescription()); 

  // Your SMTP server address
  oSmtp.ServerAddr := 'smtp.emailarchitect.net'; 

  // User and password for ESMTP authentication
  oSmtp.UserName := 'test@emailarchitect.net'; 
  oSmtp.Password := 'testpassword'; 

  // If your SMTP server requires SSL connection, please add this line
  // oSmtp.SSL_init();

  ShowMessage( 'start to send email ...' ); 

  If oSmtp.SendMail() = 0 Then 
    ShowMessage( 'email was sent successfully!' ) 
  Else 
    ShowMessage( 'failed to send email with the following error: ' 
    + oSmtp.GetLastErrDescription()); 

End; 

End. 

[Visual C++ - Send Email with File Attachment]

#include "stdafx.h" 

#include "easendmailobj.tlh" 
using namespace EASendMailObjLib; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    ::CoInitialize( NULL ); 

    IMailPtr oSmtp = NULL; 
    oSmtp.CreateInstance( __uuidof(EASendMailObjLib::Mail)); 
    oSmtp->LicenseCode = _T("TryIt"); 

    // Set your sender email address
    oSmtp->FromAddr = _T("test@emailarchitect.net"); 

    // Add recipient email address
    oSmtp->AddRecipientEx( _T("support@emailarchitect.net"), 0 ); 

    // Set email subject
    oSmtp->Subject = _T("HTML email from Visual C++ project with attachment"); 

    // Set HTML body format
    oSmtp->BodyFormat = 1; 

    // Set HTML body text
    oSmtp->BodyText = _T("<font size=5>This is</font> <font color=red><b>a test</b></font>"); 

    // Add attachment from local disk
    if(oSmtp->AddAttachment( _T("c:\\test.doc")) != 0) 
    { 
        _tprintf( _T("Failed to add attachment with error: %s\r\n"), 
             (const TCHAR*)oSmtp->GetLastErrDescription()); 
    } 

    // Add attachment from remote website
    if(oSmtp->AddAttachment( "https://www.emailarchitect.net/webapp/img/logo.jpg" ) != 0 ) 
    { 
        _tprintf( _T("Failed to add attachment with error: %s\r\n"), 
             (const TCHAR*)oSmtp->GetLastErrDescription()); 
    } 

    // Your SMTP server address
    oSmtp->ServerAddr = _T("smtp.emailarchitect.net"); 

    // User and password for ESMTP authentication
    oSmtp->UserName = _T("test@emailarchitect.net"); 
    oSmtp->Password = _T("testpassword"); 

    // If your SMTP server requires SSL connection, please add this line
    // oSmtp->SSL_init();

    _tprintf(_T("Start to send HTML email ...\r\n" )); 

    if( oSmtp->SendMail() == 0 ) 
    { 
        _tprintf( _T("email was sent successfully!\r\n")); 
    } 
    else 
    { 
        _tprintf( _T("failed to send email with the following error: %s\r\n"), 
            (const TCHAR*)oSmtp->GetLastErrDescription()); 
    } 

    if( oSmtp != NULL ) 
        oSmtp.Release(); 

    return 0; 
} 

See Also

AddAttachment1 Method
AddInline Method
ImportHtml
ImportMailEx

Online Examples

VB6- Send Email with Attachment
VB6 - Send Email with Embedded Images
Delphi - Send Email with Attachment
Delphi - Send Email with Embedded Images
Visual C++ - Send Email with Attachment
Visual C++ - Send Email with Embedded Images