Send Email with Embedded Images in VB 6.0

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

Introduction

To attach an embedded images to email, you should add an attachment to email at first. Then you should assign an unique identifier(contentid) to this attachment. Finally, you need to replace the <img src="your file name" /> to <img src="cid:yourcontentid" />.

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 - Send email with embedded images - Example]

The following example codes demonstrate how to send email with embedded images 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 with embedded images"

    Dim cid As String
    ' Add embedded image and return the unique identifier of the attachment
    cid = oSmtp.AddInline("c:\test.gif")
    If cid = "" Then
        MsgBox "failed add embedded image with error:" & oSmtp.GetLastErrDescription()
        Exit Sub
    End If

    ' Set HTML body format
    oSmtp.BodyFormat = 1
    ' Use the cid as link in the body text
    oSmtp.BodyText = "<html><body>Hello, this is a  embedded <img src=""cid:" & cid & _
            """ > picture.</body><html>"

    ' 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

To attach embedded images/pictures, ImportMailEx and ImportHtml methods are strongly recommended. With these methods, you don’t have to specify the ContentID manually. The html source/file html body can be imported to email with embedded pictures automatically.

[VB 6.0 - Send email with embedded images - ImportHtml - Example]

The following example codes demonstrate how to send email using ImportHtml method with embedded images 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 with embedded images"

    ' Set HTML body format
    oSmtp.BodyFormat = 1
    ' test.gif is in c:\my picture
    ' test.gif will be imported as embedded image in the email
    oSmtp.ImportHtml "<html><body>test <img src=""test.gif""> importhtml</body></html>", _
      "c:\my picture"

    ' 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

You can also refer to the htmlmail.* samples in EASendMail Installer. Those samples demonstrate how to build a HTML email editor and send HTML email with attachment or embedded images/pictures.

VB 6.0 html editor

Next Section

At next section I will introduce how to sign email with digital certificate in VB6.

Appendix

Comments

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