Send Email with Attachment in VB 6.0

In previous section, I introduced how to send HTML email. In this section, I will introduce how to send email with attachment in VB 6.0.

Introduction

To send an email with file attachment, we need to use AddAttachment method. This method can attach a file to the email message from local disk or a remote URL.

Note

Remarks: All of samples in this section are based on first section: Send email in a simple VB 6.0 project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[VB 6.0 - Add attachment from local disk or remote URL - Example]

The following example codes demonstrate how to send email with attachment in VB6.

Note

To get the full sample projects, please refer to Samples section.

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

Private Sub Command1_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( "http://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, if your server doesn't require
    ' User authentication, please remove the following codes.
    oSmtp.UserName = "test@emailarchitect.net"
    oSmtp.Password = "testpassword"

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

    ' If your server uses 587 port
    ' oSmtp.ServerPort = 587

    ' If your server uses 25/587/465 port with SSL/TLS
    ' oSmtp.ConnectType = ConnectSSLAuto
    ' oSmtp.ServerPort = 25 ' 25 or 587 or 465

    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

Next Section

At next section I will introduce how to add embedded images/pictures to email message.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.