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]

Const ConnectNormal = 0
Const ConnectSSLAuto = 1
Const ConnectSTARTTLS = 2
Const ConnectDirectSSL = 3
Const ConnectTryTLS = 4

Private Sub btnSendMail_Click() 

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

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

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

    ' ConnectTryTLS means if server supports SSL/TLS connection, SSL/TLS is used automatically
    oSmtp.ConnectType = ConnectTryTLS

    ' 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 

    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] Const ConnectNormal = 0 Const ConnectSSLAuto = 1 Const ConnectSTARTTLS = 2 Const ConnectDirectSSL = 3 Const ConnectTryTLS = 4 Dim oSmtp Set oSmtp = Server.CreateObject("EASendMailObj.Mail") oSmtp.LicenseCode = "TryIt" ' Your SMTP server address oSmtp.ServerAddr = "smtp.emailarchitect.net" ' User and password for ESMTP authentication oSmtp.UserName = "test@emailarchitect.net" oSmtp.Password = "testpassword" ' ConnectTryTLS means if server supports SSL/TLS connection, SSL/TLS is used automatically oSmtp.ConnectType = ConnectTryTLS ' 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 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; const ConnectNormal = 0; ConnectSSLAuto = 1; ConnectSTARTTLS = 2; ConnectDirectSSL = 3; ConnectTryTLS = 4; Var Form1: TForm1; Implementation {$R *.dfm} Procedure TForm1.Button1Click(Sender: TObject); Var oSmtp : TMail; Begin oSmtp := TMail.Create(Application); oSmtp.LicenseCode := 'TryIt'; // Your SMTP server address oSmtp.ServerAddr := 'smtp.emailarchitect.net'; // User and password for ESMTP authentication, oSmtp.UserName := 'test@emailarchitect.net'; oSmtp.Password := 'testpassword'; // ConnectTryTLS means if server supports SSL/TLS connection, SSL/TLS is used automatically oSmtp.ConnectType := ConnectTryTLS; // 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()); 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 <tchar.h> #include <Windows.h> #include "EASendMailObj.tlh" using namespace EASendMailObjLib; const int ConnectNormal = 0; const int ConnectSSLAuto = 1; const int ConnectSTARTTLS = 2; const int ConnectDirectSSL = 3; const int ConnectTryTLS = 4; int _tmain(int argc, _TCHAR* argv[]) { ::CoInitialize(NULL); IMailPtr oSmtp = NULL; oSmtp.CreateInstance(__uuidof(EASendMailObjLib::Mail)); oSmtp->LicenseCode = _T("TryIt"); // 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("test"); // ConnectTryTLS means if server supports SSL/TLS connection, SSL/TLS is used automatically oSmtp->ConnectType = ConnectTryTLS; // 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(_T("https://www.emailarchitect.net/webapp/img/logo.jpg")) != 0) { _tprintf(_T("Failed to add attachment with error: %s\r\n"), (const TCHAR*)oSmtp->GetLastErrDescription()); } _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()); } 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