Send Email in VB 6.0 - Tutorial

This tutorial introduces how to send email in VB 6 using SMTP. It also demonstrates SSL, S/MIME, Embedded Images, Email Queue, Multiple Threads, Exchange WebDAV and Exchange Web Service (EWS) usage.

Send email in a simple VB6 project using SMTP protocol

To better demonstrate how to send email using SMTP protocol, let’s create a VB 6.0 Standard EXE project at first, then add a CommandButton on the Form, double-click this button. It is like this:

VB 6.0 form project

Installation

EASendMail is a SMTP component which supports all operations of SMTP/ESMTP protocols (RFC 821, RFC 822, RFC 2554). Before you can use the following sample codes, you should download the EASendMail Installer and install it on your machine at first.

Add Reference

To use EASendMail SMTP ActiveX Object in your project, the first step is “Add reference of EASendMail to your project”. Please select menu -> Project -> References -> and select EASendMailObj ActiveX Object, click OK, the reference will be added to current project, and you can start to use it to send email in your project.

add reference in VB 6.0

[VB 6.0 - Send email - Example]

Now add the following codes to the project and change From, To, Server, User and Password to corresponding value. The following example codes demonstrate how to send email using SMTP protocol in VB6 project.

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 = "simple email from VB 6.0 project"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project, do not reply"

    ' 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

If you set everything right, you can get “email was sent successfully”. If you get “failed to send email with the following error:”, then please have a look at the following section.

Common SMTP Transport Error

When you execute above example code, if it returned an error about “Networking connection/Socket” or “No such host”, it is likely that your SMTP server address is not correct. If it threw an exception about “5xx Relay denied”, it is likely that you did not set user authentication. Another common error is “5xx Must issue a STARTTLS command first” or “No supported authentication marshal found!”, that is because your SMTP server requires user authentication under SSL connection. You can set the SSL connection to solve this problem. You can learn more detail in Troubleshooting section.

TLS 1.2

TLS is the successor of SSL, more and more SMTP servers require TLS 1.2 encryption now.

If your operating system is Windows XP/Vista/Windows 7/Windows 2003/2008/2008 R2/2012/2012 R2, and you got connection error with SSL/TLS connection, you need to enable TLS 1.2 protocol in your operating system like this:

Enable TLS 1.2 on Windows XP/Vista/7/10/Windows 2008/2008 R2/2012

Where can I get my SMTP email server address, user and password?

Because each email account provider has different server address, so you should query your SMTP server address from your email account provider. To prevent spreading email from the server, most SMTP servers also require user authentication. User name is your email address or your email address without domain part, it depends on your email provider setting.

Finally, if you have already set your account in your email client such as Outlook or Window Mail, you can query your SMTP server address, user in your email client. For example, you can choose menu -> “Tools” - > - “Accounts” - > “Your email account” - > “Properties” - > “Servers” in Outlook express or Windows Mail to get your SMTP server, user. Using EASendMail to send email does not require you have email client installed on your machine or MAPI, however you can query your exist email accounts in your email client.

Send email in VB 6.0 project

Email Address Syntax and Multiple Recipients

The following example codes demonstrates how to specify display name and email address by different syntax.

' For single email address (From, ReplyTo, ReturnPath), the syntax can be:
' ["][display name]["]<email address>.
' For example:
"Tester, T" <test@adminsystem.com>
Tester <test@adminsystem.com>
<test@adminsystem.com>
test@adminsystem.com

' For mulitple email address (To, CC, Bcc), the syntax can be:
' [single email],[single email]...
' (,;\r\n) can be used to separate multiple email addresses.
' For example:
"Tester, T" <test1@adminsystem.com>, Tester2 <test2@adminsystem.com>,
   <test3@adminsystem.com>, test4@adminsystem.com

[VB 6.0 - Email syntax - Example]

To better understand the email address syntax, please refer to the following codes. It demonstrate how to specify from, to, cc by different email address syntax.

oSmtp.FromAddr = "Tester<test@adminsystem.com>"
oSmtp.FromAddr = "test@adminsystem.com"
oSmtp.FromAddr = "<test@adminsystem.com>"

' Using AddRecipientEx to add To, Cc and Bcc in VB 6.0/Visual Basic 6.0
' Multiple addresses are separated with (,)
' The syntax is like this: "test@adminsystem.com, test1@adminsystem.com"
oSmtp.AddRecipientEx "test1@adminsystem.com, test2@adminsystem.com", 0
oSmtp.AddRecipientEx "Test1<test@adminsystem.com>, Test2<test2@adminsystem.com>", 0

' You can also add carbon copy (CC) or blind carbon copy (BCC) in the email.
oSmtp.AddRecipientEx "CC recipient<cc@adminsystem.com>", 1
oSmtp.AddRecipientEx "Bcc recipient<bcc@adminsystem.com>", 2

From, ReplyTo, Sender and Return-Path

From, Reply-To, Sender and Return-Path are common email headers in email message. You should always set From property at first, it is a MUST to identify the email sender. The following table lists the header and corresponding properties:

Header Property
From Mail.FromAddr
Reply-To Mail.ReplyTo
Sender Mail.Sender
Return-Path Mail.ReturnPath
  • From

    This property indicates the original email sender. This is what you see as the “FROM” in most mail clients.

  • Reply-To

    This property indicates the reply address. Basically, when the user clicks “reply” in mail client, the Reply-To value should be used as the recpient address of the replied email. If you don’t set this property, the Reply address is same as From address.

  • Sender

    This property indicates the who submit/send the email. When the user received the email, the email client displays: From: “sender address” on behalf of “from address”. If you don’t set this property, the Sender address is same as From address. Sender property is common used by mail listing provider. This property also takes effect to DKIM/DomainKeys signature, if Sender is different with From address, then you should sign DKIM/DomainKeys based on Sender domain instead of From address domain.

  • Return-Path

    This property indicates the delivery notification report address. If you don’t set this property, the Return-Path address is same as From address. This property also takes effect to SPF record, if Return-Path is different with From address, then remote SMTP server checkes SPF record of Return-Path instead of From address.

[VB 6.0 - From, ReplyTo, Sender and Return-Path in Email - Example]

The following example codes demonstrate how to specify From, Reply-To, Sender and Return-Path in Email. With the following example codes:

  • If the email couldn’t be delivered to recipient, a non-delivery report will be sent to report@emailarchitect.net.
  • If the user received the email, the email client will display: sender@emailarchitect.net on behalf of from@adminsystem.com.
  • If the user click “reply”, the replied email will be sent to reply@adminsystem.com.
' [VB, VBA - From, ReplyTo, Sender and Return-Path]
Dim oSmtp As New EASendMailObjLib.Mail
oSmtp.LicenseCode = "TryIt"
oSmtp.FromAddr = "from@adminsystem.com"
oSmtp.ReplyTo = "reply@adminsystem.com"
oSmtp.Sender = "sender@emailarchitect.net"
oSmtp.ReturnPath = "report@emailarchitect.net"

'[VBScript, ASP - From, ReplyTo, Sender and Return-Path]
Dim oSmtp
Set oSmtp = Server.CreateObject("EASendMailObj.Mail")
oSmtp.LicenseCode = "TryIt"
oSmtp.FromAddr = "from@adminsystem.com"
oSmtp.ReplyTo = "reply@adminsystem.com"
oSmtp.Sender = "sender@emailarchitect.net"
oSmtp.ReturnPath = "report@emailarchitect.net"

Mail Priority

If you want to set Higher or Lower priority to your email, you can use Priority prority

[VB 6.0 - Mail Priority - Example]

' Set high priority
oSmtp.Priority = 1 'High priority

Troubleshooting

When you send email in above simple VB project, if it returned an error, please have a look at the following tips:

“No Such Host” Error

This error means DNS server cannot resolve SMTP server, you should check if you input correct server address. If your server address is correct, you should check if your DNS server setting is correct.

Common “Socket/Networking Connection” Error

This error means there is a problem with networking connection to SMTP server. You can use Windows built-in Telnet command to detect the networking connection.

Using Telnet to detect networking connection to SMTP server

Note

Notice: in Windows 2008/Windows 8 or later version, Telnet Client is not installed by default, you should enable this command in Control Panel -> Programs and Features -> Turn Windows feature on or off -> have Telnet Client checked.

Under DOS command prompt, input “telnet [serveraddress] [port]”:

telnet mail.emailarchitect.net 25
press enter.

If the networking connection to your SMTP server is good, it should return a message like 220 .... If it returns Could not open connection to ..., that means the networking connection to SMTP server is bad, or outbound 25 port is blocked by anti-virus software, firewall or ISP. Please have a look at the following screenshot:

detect SMTP connection using telnet

SMTP 25, 587, 465 port

25 port is the default SMTP server port to receive email. However, some ISP block outbound 25 port to prevent user to send email directly to other SMTP server. Therefore, many email providers also provide an alternative port 587 to receive email from such users. 465 port is the common port used to receive email over implicit SSL connection. If you use telnet to test 465 port, it doesn’t return the “220…”, because it requires SSL hand shake. But if the connection is ok, telnet returns a flash cursor.

“5xx … IP address block or on black list or bad reputation” Exception

This error means SMTP server blocks your IP address or email content. You can try to set user/password in your codes to do user authentication and try it again. If email client set user authentication, most SMTP servers do not check client source IP address in black list.

“5xx user authenticaton” Error

TThis error means user authentication is failed, you should check whether you input correct user/password. Password is always case-sensitive.

“5xx relay denied” Error

For anti-spam policy, most SMTP servers do not accept the email to outbound domain without user authentication. You should set user/password in the codes and try it again.

“5xx Must issue a STARTTLS command first”

This error means SMTP server requires SSL/TLS connection. You should enable SSL/TLS connection like this:

' If your smtp server requires TLS connection, please add this line
oSmtp.ConnectType = 1

“No supported authentication marshal found!”

This error means SMTP server doesn’t support user authentication or it requires user authentication over SSL/TLS connection. You can try to remove user/password in your codes and try it again.

Other error returned by SMTP server

If SMTP server returns an error, it usually returns description about this error. Some descriptions also include a HTTP link, you can go to this linked web page to learn more detail. You can also use the following codes to generate a log file to learn all SMTP session between client and server.

[VB 6.0 - Using log file to detect SMTP server response - Example]

oSmtp.LogFileName = "d:\smtp.txt"

Process Bounced Email (Non-Delivery Report)

If you sent email successfully without error, that means the email has been submitted to the SMTP server. The SMTP server will deliver the email in background, if the email couldn’t be delivered, a Failure Report (NDS) will be sent back to your sender email address.

To retrieve and parse Failure Report (NDS), you should monitor your sender mailbox. I recommend that you use EAGetMail to monitor your sender mailbox using POP3/IMAP4/Exchange WebDAV/Exchange Web Service protocol. After you installed EAGetMail on your machine, there are several full samples named “parse_report.*” for VB6, Delphi, Visual C++ in the installation path.

Email Tracking

Email tracking is used to verify that emails are actually read by recipients. There are two common solutions: Read Receipt and Linked Image Tracking

To learn more detail about Process Bounced Email (Non-Delivery Report) and Email Tracking, please have a look at this topic: Process Bounced Email (Non-Delivery Report) and Email Tracking

Bulk Email Sender Guidelines

If you are a mail listing provider and send bulk emails every day, of course you don’t want your emails are blocked or moved to Junk folder of the recipient mailbox.

To increase the inbox delivery rate of your messages, make sure that all recipients on your distribution lists actually want to receive the mail. Have a look the topic for some tips on how to make sure your messages are welcomed by most email providers:

Bulk Email Sender Guidelines

32bit/x64 ActiveX DLL

Seperate builds of run-time dll for 32 and x64 platform

File Platform
Installation Path\Lib\native\x86\EASendMailObj.dll 32 bit
Installation Path\Lib\native\x64\EASendMailObj.dll 64 bit

Distribution

  • Standard EXE

    For VB6, C++, Delphi or other standard exe application, you can distribute EASendMailObj.dll with your application to target machine without COM-registration and installer. To learn more detail, please have a look at Registration-free COM with Manifest File.

  • Script

    For ASP, VBScript, VBA, MS SQL Stored Procedure, you need to install EASendMail on target machine by EASendMail installer, both 32bit/x64 DLL are installed and registered.

Next Section

In this section, I introduced how to send email in a simple VB6 project using SMTP protocol. At next section I will introduce how to send email over SSL/TLS connection in VB 6.0.

Send Email over SSL/TLS in VB 6.0

In previous section, I introduced how to send email in a simple VB6 project. In this section, I will introduce how to send email over SSL/TLS connection in VB 6.0.

SSL and TLS Introduction

SSL connection encrypts data between the SMTP component and SMTP server to protects user, password and email content in TCP/IP level. Now this technology is commonly used and many SMTP servers are deployed with SSL such as Gmail, Yahoo and Hotmail. There are two ways to deploy SSL on SMTP server:

  • Explicit SSL (TLS)

    Using STARTTLS command to switch SSL channel on normal SMTP port (25 or 587);

  • Implicit SSL

    Deploying SSL on another port (465 or other port, you may query it from your server administrator

EASendMail SMTP component supports both ways. The connection can be specified by Mail.ConnectType property. Please see the following example code.

TLS 1.2

TLS is the successor of SSL, more and more SMTP servers require TLS 1.2 encryption now.

If your operating system is Windows XP/Vista/Windows 7/Windows 2003/2008/2008 R2/2012/2012 R2, and you got connection error with SSL/TLS connection, you need to enable TLS 1.2 protocol in your operating system like this:

Enable TLS 1.2 on Windows XP/Vista/7/10/Windows 2008/2008 R2/2012

[VB 6.0 - Send Email over SSL/TLS Setting - Example]

The following example codes demonstrates how to set SSL/TLS connection.

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

 ' Send email by normal TCP/IP without SSL connection
 oSmtp.ServerAddr = "localhost"
 oSmtp.ServerPort = 25

 ' Send email by SSL connection with STARTTLS command switching
 oSmtp.ServerAddr = "localhost"
 oSmtp.ServerPort = 25
 oSmtp.ConnectType = ConnectSTARTTLS

 ' Send email by SSL connection with direct SSL.
 oSmtp.ServerAddr = "localhost"
 oSmtp.ServerPort = 465
 oSmtp.ConnectType = ConnectDirectSSL

 ' Send email by SSL/TLS connection with auto-detect.
 ' If port is 25 or 587, STARTTLS TLS will be used; otherwise direct SSL will be used.
 oSmtp.ServerAddr = "localhost"
 oSmtp.ServerPort = 465
 oSmtp.ConnectType = ConnectSSLAuto

 oSmtp.ServerAddr = "localhost"
 oSmtp.ServerPort = 25
 oSmtp.ConnectType = ConnectSSLAuto

' Try to send email by SSL/TLS connection, if server doesn't support SSL/TLS, normal TCP is used.
 oSmtp.ServerAddr = "localhost"
 oSmtp.ServerPort = 25
 oSmtp.ConnectType = ConnectTryTLS

 oSmtp.ServerAddr = "localhost"
 oSmtp.ServerPort = 587
 oSmtp.ConnectType = ConnectTryTLS

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 over implicit SSL on 465 port - Example]

The following example codes demonstrate how to send email over SSL connection on 465 port.

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 = "simple email from VB 6.0 project"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project, do not reply"

    ' 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"

    ' Set SSL 465 port
    oSmtp.ServerPort = 465

    ' Set direct SSL connection
    oSmtp.ConnectType = ConnectSSLAuto

    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

[VB 6.0 - Send email over TLS (Explicit SSL) on 25 or 587 port - Example]

The following example codes demonstrate how to send email over TLS (STARTTLS command, Explicit SSL) connection on 25 port.

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 = "simple email from VB 6.0 project"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project, do not reply"

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

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

    ' Set 25 port, if your server uses 587 port, please change 25 to 587
    oSmtp.ServerPort = 25

    ' Set TLS connection
    oSmtp.ConnectType = ConnectSSLAuto

    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

SMTP Server SSL Certificate

To send email over SSL/TLS connection, you don’t need to install a certificate on your machine. The data is encrypted by server certificate public/private key pair.

SMTP Setting for Gmail, Yahoo, Hotmail and Office 365

Because most popluar email providers support or require SSL/TLS connection, so I will introduce specific setting for Gmail, Yahoo, Hotmail and Office 365 in the coming sections.

Next Section

At next section I will introduce how to send email using Gmail account in VB6.

Send Email using Gmail in VB 6.0

In previous section, I introduced how to send email over SSL/TLS connection. In this section, I will introduce how to send email using Gmail account in VB 6.0.

Introduction

Gmail SMTP server address is smtp.gmail.com. It requires implicit SSL or explicit SSL (TLS) connection, and you should use your Gmail email address as the user name for ESMTP authentication.

Server Port SSL/TLS
smtp.gmail.com 25, 587 TLS
smtp.gmail.com 465 SSL

Gmail App Password

To help keep your account secure, starting May 30, 2022, ​​Google will no longer support the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.

Therefore, you should sign in using App Passwords. An App Password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. App Passwords can only be used with accounts that have 2-Step Verification turned on. You need to use App Password instead of the user password for user authentication.

Another solution is Gmail OAUH, please see Gmail SMTP OAUTH section.

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 using Gmail account over implicit SSL on 465 port - Example]

The following example codes demonstrate how to send email using Gmail account over SSL 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 Gmail email address
    oSmtp.FromAddr = "gmailid@gmail.com"
    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0

    ' Set email subject
    oSmtp.Subject = "test email from gmail account"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project with gmail"

    ' Gmail SMTP server address
    oSmtp.ServerAddr = "smtp.gmail.com"

    ' set direct SSL 465 port,
    oSmtp.ServerPort = 465

    ' detect SSL/TLS automatically
    oSmtp.ConnectType = ConnectSSLAuto

    ' Gmail user authentication should use your
    ' Gmail email address as the user name.
    ' For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
    oSmtp.UserName = "gmailid@gmail.com"

    ' Create app password in Google account
    ' https://support.google.com/accounts/answer/185833?hl=en
    oSmtp.Password = "your app password"

    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

[VB 6.0 - Send email using Gmail account over explicit SSL (TLS) on 25 or 587 port]

The following example codes demonstrate how to send email using Gmail account over TLS 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 Gmail email address
    oSmtp.FromAddr = "gmailid@gmail.com"
    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0

    ' Set email subject
    oSmtp.Subject = "test email from gmail account"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project with gmail"

    ' Gmail SMTP server address
    oSmtp.ServerAddr = "smtp.gmail.com"

    ' Gmail user authentication should use your
    ' Gmail email address as the user name.
    ' For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
    oSmtp.UserName = "gmailid@gmail.com"

    ' Create app password in Google account
    ' https://support.google.com/accounts/answer/185833?hl=en
    oSmtp.Password = "your app password"

    ' set 587 port, if you want to use 25 port, please change 587 to 25
    oSmtp.ServerPort = 587

    ' detect SSL/TLS automatically
    oSmtp.ConnectType = ConnectSSLAuto

    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

Gmail SMTP OAUTH

The Gmail IMAP and SMTP servers have been extended to support authorization via the industry-standard OAuth 2.0 protocol. Using OAUTH protocol, user can do authentication by Gmail Web OAuth instead of inputting user and password directly in application.

Google will disable traditional user authentication in the future, switching to Google OAuth is strongly recommended now.

TLS 1.2

TLS is the successor of SSL, more and more SMTP servers require TLS 1.2 encryption now.

If your operating system is Windows XP/Vista/Windows 7/Windows 2003/2008/2008 R2/2012/2012 R2, and you got connection error with SSL/TLS connection, you need to enable TLS 1.2 protocol in your operating system like this:

Enable TLS 1.2 on Windows XP/Vista/7/10/Windows 2008/2008 R2/2012

Next Section

At next section I will introduce how to send email using Yahoo account in VB6.

Send Email using Yahoo in VB 6.0

In previous section, I introduced how to send email using Gmail account. In this section, I will introduce how to send email using Yahoo account in VB 6.0.

Introduction

Yahoo SMTP server address is smtp.mail.yahoo.com. It supports both Normal/Implicit SSL/Explicit SSL (TLS) connection to do user authentication, and you should use your Yahoo email address as the user name for ESMTP authentication. For example: your email is myid@yahoo.com, and then the user name should be myid@yahoo.com.

If you want to use implicit SSL connection with Yahoo SMTP server, you must set the port to 465.

Server Port SSL/TLS
smtp.mail.yahoo.com 25, 587 TLS
smtp.mail.yahoo.com 465 SSL

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.

Important

If you got authentication error, you need to enable Allowing less secure apps in your Yahoo account. Or you can generate App Passwords and use this app password instead of your user password.

Although Yahoo supports OAUTH, but it doesn’t provide mail permission, so OAUTH is not a solution for Yahoo mail.

[VB 6.0 - Send email using Yahoo over Implicit SSL on 465 port - Example]

The following example codes demonstrate how to send email using Yahoo account in VB over SSL 465 port.

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 Yahoo email address
    oSmtp.FromAddr = "myid@yahoo.com"
    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0

    ' Set email subject
    oSmtp.Subject = "test email from Yahoo mail account"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project with Yahoo email"

    ' Yahoo SMTP server address
    oSmtp.ServerAddr = "smtp.mail.yahoo.com"

    ' For example: your email is "myid@yahoo.com", then the user should be "myid@yahoo.com"
    oSmtp.UserName = "myid@yahoo.com"
    oSmtp.Password = "yourpassword"

    ' Set port to 465.
    oSmtp.ServerPort = 465

    ' detect SSL/TLS automatically
    oSmtp.ConnectType = ConnectSSLAuto

    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

[VB 6.0 - Send Email using Yahoo over Explicit SSL (TLS) on 25 or 587 Port - Example]

The following example codes demonstrate how to send email using Yahoo account in VB6 over TLS 587 port.

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 Yahoo email address
    oSmtp.FromAddr = "myid@yahoo.com"
    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0

    ' Set email subject
    oSmtp.Subject = "test email from Yahoo mail account"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project with Yahoo email"

    ' Yahoo SMTP server address
    oSmtp.ServerAddr = "smtp.mail.yahoo.com"

    ' For example: your email is "myid@yahoo.com", then the user should be "myid@yahoo.com"
    oSmtp.UserName = "myid@yahoo.com"
    oSmtp.Password = "yourpassword"

    ' Set port to 587, if you want to use 587 port, please change 587 to 25
    oSmtp.ServerPort = 587

    ' detect SSL/TLS automatically
    oSmtp.ConnectType = ConnectSSLAuto

    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

TLS 1.2

TLS is the successor of SSL, more and more SMTP servers require TLS 1.2 encryption now.

If your operating system is Windows XP/Vista/Windows 7/Windows 2003/2008/2008 R2/2012/2012 R2, and you got connection error with SSL/TLS connection, you need to enable TLS 1.2 protocol in your operating system like this:

Enable TLS 1.2 on Windows XP/Vista/7/10/Windows 2008/2008 R2/2012

Next Section At next section I will introduce how to send email using Hotmail/Live/Outlook/Office 365 account in VB6.

Send Email using Hotmail/Live/Outlook/Office 365 in VB 6.0

In previous section, I introduced how to send email using Yahoo account. In this section, I will introduce how to send email using Hotmail/Live/Outlook/Office 365 in VB 6.0.

Introduction

Hotmail/Live/Outlook.com SMTP server address is smtp.office365.com. It requires explicit SSL (TLS) connection to do user authentication, and you should use your Hotmail/Live/Outlook.com email address as the user name for ESMTP authentication. For example: your email is liveid@hotmail.com, and then the user name should be liveid@hotmail.com.

Server Port SSL/TLS
smtp.office365.com 25, 587 TLS

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 using Hotmail/Live/Outlook over Explicit SSL (TLS) on 25 or 587 Port - Example]

The following example codes demonstrate how to send email using Hotmail/Live/Outlook in VB6 over TLS 25 or 587 port.

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 Hotmail email address
    oSmtp.FromAddr = "liveid@hotmail.com"
    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0

    ' Set email subject
    oSmtp.Subject = "test email from hotmail account"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project with hotmail"

    ' Hotmail SMTP server address
    oSmtp.ServerAddr = "smtp.office365.com"

    ' Hotmail user authentication should use your
    ' Hotmail email address as the user name.
    oSmtp.UserName = "liveid@hotmail.com"

    ' If you got authentication error, try to create an app password instead of your user password.
    ' https://support.microsoft.com/en-us/account-billing/using-app-passwords-with-apps-that-don-t-support-two-step-verification-5896ed9b-4263-e681-128a-a6f2979a7944
    oSmtp.Password = "your password or app password"

    ' Set port to 587, if you want to use 25 port, please change 587 to 25
    oSmtp.ServerPort = 587

    ' detect SSL/TLS connection automatically
    oSmtp.ConnectType = ConnectSSLAuto

    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

Hotmail SMTP OAUTH

If your account enabled two-factor authentication, you cannot login your account by normal user authentication, you should use SMTP OAUTH or App Password.

Microsoft Live SMTP servers (Hotmail, Oultook personal account) have been extended to support authorization via the industry-standard OAuth 2.0 protocol. Using OAUTH protocol, user can do authentication by Microsoft Web OAuth instead of inputting user and password directly in application.

Microsoft will disable traditional user authentication in the future, switching to Microsoft OAuth (Modern Authentication) is strongly recommended now.

Or you can generate App Passwords and use this app password instead of your user password.

Send Email using Office 365

Office 365 SMTP server uses 587 port and explicit SSL (TLS) connection.

Server Port SSL/TLS
smtp.office365.com 25, 587 (recommended) TLS

App Password and SmtpClientAuthenticationDisabled

If your account enabled two-factor authentication, you cannot login your account by normal user authentication, you should create an App Passwords and use this App Password instead of the user password.

You should also check if authenticated client SMTP submission (SMTP AUTH) is enabled:

Enable or disable authenticated client SMTP submission (SMTP AUTH) in Exchange Online.

[VB 6.0 - Send email using Office 365 over explicit SSL (TLS) on 587 port - Example]

The following example codes demonstrate how to send email using Office 365 in VB6 over TLS 587 port.

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"

    ' Your office 365 email address
    oSmtp.FromAddr = "myid@mydomain"
    ' Add recipient email address
    oSmtp.AddRecipientEx "support@emailarchitect.net", 0

    ' Set email subject
    oSmtp.Subject = "test email from office 365 account"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project using office 365"

    ' Your Office 365 SMTP server address,
    ' You should get it from outlook web access.
    oSmtp.ServerAddr = "smtp.office365.com"

    ' user authentication should use your
    ' email address as the user name.
    oSmtp.UserName = "myid@mydomain"

    ' If you got authentication error, try to create an app password instead of your user password.
    ' https://support.microsoft.com/en-us/account-billing/using-app-passwords-with-apps-that-don-t-support-two-step-verification-5896ed9b-4263-e681-128a-a6f2979a7944
    oSmtp.Password = "your password or app password"

    ' Set port to 587
    oSmtp.ServerPort = 587

    ' detect SSL/TLS connection automatically
    oSmtp.ConnectType = ConnectSSLAuto

    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

Office365 SMTP/EWS/Ms Graph API OAUTH

If your account enabled two-factor authentication, you cannot login your account by normal user authentication, you should use SMTP/EWS/Ms Graph API OAUTH or App Password.

Microsoft Office365 SMTP/EWS/Ms Graph API servers have been extended to support authorization via the industry-standard OAuth 2.0 protocol. Using OAUTH protocol, user can do authentication by Microsoft Web OAuth instead of inputting user and password directly in application.

Microsoft will disable traditional user authentication in the future, switching to Microsoft OAuth (Modern Authentication) is strongly recommended now.

Or you can generate App Passwords and use this app password instead of your user password.

Next Section

At next section I will introduce how to send email without specified SMTP server.

Send Email directly without SMTP server(MX DNS lookup) in VB 6.0

In previous section, I introduced how to send email using Hotmail/Live/Office 365 account. In this section, I will introduce how to use DNS lookup to send email without specified SMTP server in VB 6.0.

Introduction

In general, we send email via specified SMTP server. How does the specified SMTP server know what address this email should be sent to? The answer is… it queries MX record of recipient’s domain via DNS lookup. It then forwards this email to the SMTP server queried from DNS server. If recipient’s server doesn’t work fine, sender’s SMTP server will send a failure-delivery report to the sender telling it failed to send out the email.

How does EASendMail SMTP component work with “Send email directly”? Firstly, it queries MX record for recipient address from DNS, then sends email to recipient’s email server directly. In short, if no SMTP server is specified in the code, EASendMail will send email to recipient directly. Since querying DNS server consumes CPU time and networking resource, the performance of “Send email directly” is lower than sending email with specified SMTP server. Moreover, nowadays more and more SMTP servers block email sent from dynamic IP address, so we don’t recommend you to use “Direct Send Email” except you have a static IP address or you encounter problem with your ISP SMTP server.

Every recipient may have different SMTP server, if there are multiple recipients in one message and you want to send email directly, you should send the email to the recipients one by one.

To implement this feature, you just need to put nothing to SMTP server address.

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 without specified SMTP server (MX record DNS lookup) - Example]

The following example codes demonstrate how to send email using DNS lookup without SMTP server 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 = "Direct send email from VB 6.0 project"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB6.0 project directly"

    ' Set SMTP server address to ""
    oSmtp.ServerAddr = ""

    ' Do not set user authentication
    ' Do not set SSL connection

    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

With above code, if you get error like “5xx IP address rejected”, that means your IP address is blocked by the recipient’s SMTP server. You have to specify a SMTP server with user authentication to relay your email.

Remarks

In my solid experience, I don’t suggest that you send email directly.

  • If your IP address is dynamic, most SMTP servers reject your connection due to anti-spam policy. We always suggest that your send email by a SMTP server that has a static internet IP address. When you relay email by your SMTP server, because you do user authentication at first before you send email to your SMTP server, so your SMTP server doesn&#8217;t reject your connection even your IP address is dynamic. Finally your SMTP server sends email to remote SMTP server. Because your SMTP server has a static IP, the email won&#8217;t be rejected by remote SMTP server.

    Send email using DNS lookup in VB6
  • If you encountered a temporal SMTP error (4xx), you should retry to send email later. That means you have to write the code to handle retry. So if you have a static IP address, I suggest that you use EASendMail Component + EASendMail Service, EASendMail service can send email directly or send email with specified SMTP server in background and handle delivery retry automatically.

    To learn more detail about EASendMail Serivce, please have a look at Work with EASendMail Service (Email Queuing).

Next Section

At next section I will introduce how to compose HTML email and send HTML email in VB6.

Send HTML Email in VB 6.0

In previous section, I introduced how to send email without specified SMTP server. In this section, I will introduce how to compose and send HTML email in VB 6.0.

Introduction

If you want to specify the font, color or insert pictures in your email, you should use Html email format instead of Plain text email format.

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 HTML email - Example]

The following example codes demonstrate how to send email in HTML body format 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"

    ' 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>"

    ' 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

After you received the email by your email client, the body text is like this:

send html email in vb6

Import Html to email directly

Of course, you don’t have to write the HTML source body text in your application manually. You can build a html file with HTML tools and use ImportMailEx method to import the html file directly.

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 attach file attachment to email message.

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.

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.

Send Email with Digital Signature in VB 6.0 - S/MIME with SHA1, SHA256 and SHA512

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

Introduction

S/MIME (Secure/Multipurpose Internet Mail Extensions) is a standard for public key encryption and signing of MIME data.

Digital signature prevents email content is faked or changed in transport level. Encrypting email protects email content from exposure to inappropriate recipients. Both digital signature and email encrypting depend on digital certificate.

If you have an email digital signature certificate installed on your machine, you can find it in “Control Panel” -> “Internet Options” -> “Content” -> “Certificates” -> “Personal”.

VB 6.0 email ceritificate

Then you can use your email certificate to sign the email by the following code. If you don’t have a certificate for your email address, you MUST get a digital certificate for personal email protection from third-party certificate authorities such as www.verisign.com.

If you need a free certificate for your email address, you can go to http://www.comodo.com/home/email-security/free-email-certificate.php to apply for one year free email certificate.

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 digital signature (S/MIME) - Example]

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

Note

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

Const CRYPT_MACHINE_KEYSET = 32
Const CRYPT_USER_KEYSET = 4096
Const CERT_SYSTEM_STORE_CURRENT_USER = 65536
Const CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072

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 email from VB 6.0 with digital signature"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 with digital signature"

    ' Add digital signature
    If Not oSmtp.SignerCert.FindSubject("test@emailarchitect.net", _
        CERT_SYSTEM_STORE_CURRENT_USER, "my") Then
        MsgBox oSmtp.SignerCert.GetLastError()
        Exit Sub
    End If

    If Not oSmtp.SignerCert.HasPrivateKey Then
        MsgBox "Signer certificate has not private key, " & _
             " this certificate can not be used to sign email!"
        Exit Sub
    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

Signature Algorithm

You can use SignatureHashAlgorithm property to set MD5, SHA1, SHA256, SHA384 or SHA512 signature algorithm. SHA256 is recommended.

RSASSA-PSS Signature for EDIFACT

If you need to use RSASSA-PSS signature scheme based on EDIFACT rule, you need an additional ActiveX Object for EASendMail, please have a look at this topic:

RSASSA-PSS + RSA-OAEP Encryption with SHA256

Next Section

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

Encrypt Email in VB 6.0 - S/MIME with RC2, 3DES and RSAES-OAEP

In previous section, I introduced how to send email with digital signature. In this section, I will introduce how to encrypt email with digital certificate in VB 6.0.

Introduction

After the recipient received your email with digital signature, the recipient can get your digital certificate public key from your digital signature. Then the recipient can encrypt an email with your public key and send it to you. Only you can decrypt this email with your private key. That is how S/MIME can protect your email content. If you don’t expose your digital certificate private key to others, none can read your email which is encrypted by your public key.

If you received an email with digital signature, your email client usually stores the public key of the sender in “Control Panel” -> “Internet Options” -> “Content” -> “Certificates” -> “Other People”.

Then you can use the following code to encrypt email and send it to your recipient.

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 - Email Encryption (S/MIME) - Example]

The following example codes demonstrate how to encrypt email with digital certificate in VB6.

Note

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

Const CRYPT_MACHINE_KEYSET = 32
Const CRYPT_USER_KEYSET = 4096
Const CERT_SYSTEM_STORE_CURRENT_USER = 65536
Const CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072

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 encrypted email from VB 6.0 project"
    ' Set email body
    oSmtp.BodyText = "this is a test encrypted email sent from VB 6.0 project"

    ' Add digital signature
    If Not oSmtp.SignerCert.FindSubject("test@emailarchitect.net", _
        CERT_SYSTEM_STORE_CURRENT_USER, "my") Then
        MsgBox oSmtp.SignerCert.GetLastError()
        Exit Sub
    End If

    If Not oSmtp.SignerCert.HasPrivateKey Then
        MsgBox "Signer certificate has not private key, " & _
             " this certificate can not be used to sign email!"
        Exit Sub
    End If

    ' Find the encrypting certificate for every recipients
    Dim oEncryptCert As New EASendMailObjLib.Certificate
    If Not oEncryptCert.FindSubject("support@emailarchitect.net", _
                            CERT_SYSTEM_STORE_CURRENT_USER, "AddressBook") Then
        If Not oEncryptCert.FindSubject("support@emailarchitect.net", _
                                        CERT_SYSTEM_STORE_CURRENT_USER, "my") Then
            MsgBox oEncryptCert.GetLastError()
            Exit Sub
        End If
    End If

    ' Add encrypting certificate
    oSmtp.RecipientsCerts.Add oEncryptCert

    ' 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

If you received digital signed and encrypted email by Windows Mail(Outlook Express), it looks like this:

VB 6.0 sign and encrypt email - s/mime

Encryption Algorithm

You can use EncryptionAlgorithm property to set RC2, RC4, 3DES, AES128, AES192 or AES256 encryption algorithm. RSAES-OAEP (AES128, AES192 and AES256) is recommended.

RSA-OAEP Encryption with SHA256 HASH

If you need to use RSA-OAEP encryption with sha256 scheme based on EDIFACT rule, please have a look at this topic:

RSASSA-PSS + RSA-OAEP Encryption with SHA256

Next Section

At next section I will introduce how to send email with event handler in asynchronous mode.

Send Email Asynchronously in VB 6.0

In previous section, I introduced how to encrypt email with digital certificate. In this section, I will introduce how to use event handler and send email asynchronously in VB 6.0.

Introduction

Asynchronous mode

In synchronous mode, once SendMail method is called, it returns to application after the method is complete. Therefore, if the runtime (it depends on the networking connection and the email size) is long, your application cannot do anything before this method ends, which results “my application is blocked or halted”. In contrast, in asynchronous mode, as SendMail method works in background, this methods return to application immediately no matter the running method is complete or not.

Event handler

In previous examples, after SendMail method is invoked, if you want to know the progress of the email sending, you should use Event Handler to monitor the progress of email sending.

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 event handler in asynchronous mode - Example]

To demonstrate how to use asynchronous mode and event handler, let’s add a Label control in the form at first, the name of the label is “Label1”.

The following example codes demonstrate how to send email with event handler in asynchronous mode.

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 WithEvents oSmtp As EASendMailObjLib.Mail
Private m_bError As Boolean
Private m_bFinished As Boolean

Private Sub oSmtp_OnAuthenticated()
    Label1.Caption = "Authenticated"
End Sub

Private Sub oSmtp_OnClosed()
    If Not m_bError Then
        Label1.Caption = "email was sent successfully!"
    End If
    m_bFinished = True
End Sub

Private Sub oSmtp_OnConnected()
    Label1.Caption = "Connected"
End Sub

Private Sub oSmtp_OnError(ByVal lError As Long, ByVal ErrDescription As String)
    Label1.Caption = "failed to send email with error: " & ErrDescription
    m_bError = True
    m_bFinished = True
End Sub

Private Sub oSmtp_OnSending(ByVal lSent As Long, ByVal lTotal As Long)
    Label1.Caption = "Sending " & lSent & "/" & lTotal
End Sub

Private Sub Command1_Click()

    If oSmtp Is Nothing Then
        Set oSmtp = New EASendMailObjLib.Mail
        oSmtp.LicenseCode = "TryIt"
    End If

    m_bError = False
    m_bFinished = False

   ' 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 email from VB 6.0 in asynchronous mode"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project with asynchronous mode"

    ' 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

    ' Set asynchronous mode
    oSmtp.Asynchronous = 1

    Command1.Enabled = False
    Label1.Caption = "start to send email ..."

    oSmtp.SendMail

    Do While Not m_bFinished
        ' Wait for the email sending, you can do other thing here
        DoEvents
    Loop

    MsgBox Label1.Caption
    Command1.Enabled = True
End Sub

Next Section

At next section I will introduce how to send mass email with multiple threads in VB6.

Send Email with Multiple Threads(Mass Mail) in VB 6.0

In previous section, I introduced how to use asynchronous mode. In this section, I will introduce how to send mass email with multiple threads in VB 6.0.

Introduction

Visual Basic 6.0 doesn’t support multiple threads. However, FastSender object has an inner threading pool based on MaxThreads count. Firstly, Send method submits email to FastSender mail queue. Secondly threading pool retrieves email from mail queue and sends it out. Finally OnSent event informs that the email was sent successfully or unsuccessfully.

No. of worker threads in the threading pool of FastSender object is automatically adjusted based on the actual usage. The maximum no. of worker threads is up to the value of MaxThread property specified.

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 Example - Send mass email with multiple threads]

The following example codes demonstrate how to send mass email with multiple threads.

Note

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

Option Explicit

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

Private WithEvents m_oFastSender As EASendMailObjLib.FastSender
Private oSmtp As EASendMailObjLib.Mail

Private Sub Command1_Click()

    Dim recipientAddr(3) As String
    Dim i As Integer

    If m_oFastSender Is Nothing Or oSmtp Is Nothing Then
        Set m_oFastSender = New EASendMailObjLib.FastSender
        Set oSmtp = New EASendMailObjLib.Mail
        oSmtp.LicenseCode = "TryIt"
        ' Set the maximum no. of worker threads
        m_oFastSender.MaxThreads = 10
    End If

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

    ' 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

    recipientAddr(0) = "test@adminsystem.com"
    recipientAddr(1) = "test1@adminsystem.com"
    recipientAddr(2) = "test2@adminsystem.com"

    For i = 0 To 2
        oSmtp.ClearRecipient
        oSmtp.AddRecipientEx recipientAddr(i), 0
        oSmtp.Subject = "mass email test subject"
        oSmtp.BodyText = "mass email test body sent from vb"
        Call m_oFastSender.Send(oSmtp, i, "any")
    Next

End Sub

Private Sub m_oFastSender_OnSent(ByVal lRet As Long, _
                                 ByVal ErrDesc As String, _
                                 ByVal nKey As Long, _
                                 ByVal tParam As String,  _
                                 ByVal Sender As String, _
                                 ByVal Recipients As String)
    If lRet = 0 Then
        MsgBox nKey & " email was sent successfully"
    Else
        MsgBox nKey & ": failed to send email: " & ErrDesc
    End If
End Sub

Next Section

At next section I will introduce how to send email using EASendMail Service Queue in ASP.

Send Email with Queue in ASP

In previous section, I introduced how to send mass emails with multiple threads. In this section, I will introduce how to send email using EASendMail Service Queue in ASP.

Introduction

EASendMail Service is a light and fast email delivery service which works with EASendMail SMTP Component to enable your application to send mass emails in background queue service.

Along with its ability to picking recipients from database in background and sending email in specified datetime, it eases your task in developing featured email application such as newsletter application. We strongly recommend you to use EASendMail Service with your ASP/Web Application.

send email using queue in ASP/VBScript

Important

To work with EASendMail Service, please download EASendMail and EASendMail Service at first, and then install both on your machine. If you are using web hosting service and you don’t have permission to install service on that server, EASendMail service is not suitable for you.

With EASendMail email queue feature, you do not have to code for multiple threadings. EASendMail Service can send email in background with multiple threadings automatically. You just need to adjust the maximum worker threads in EASendMail Service Manager to increase the performance. Please click here to learn more detail about EASendMail Service.

If your networking connection to your SMTP server is not very fast, EASendMail Service is absolutely solution for you. You just need to submit the email to EASendMail service queue, it is very fast because EASendMail service uses shared memory to accept email from EASendMail component, and then the service will send email in background service. It is very important to improve the response time for ASP web application.

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.

[ASP Example - Send email with Queue]

The following example codes demonstrate how to send email using EASendMail Service Queue.

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

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 = "simple email from ASP project"
' Set email body
oSmtp.BodyText = "this is a test email sent from ASP queue project, do not reply"

' 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

Response.Write "start to send email ..."
' You just need to change SendMail method to SendMailToQueue method in
' your ASP.NET web application, then EASendMail uses queue to send email.
' SendMailToQueue can be used in windows application as well.

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

Next Section

At next section I will introduce how to send mass emails with EASendMail Service Database Queue in ASP.

Send Mass Emails using Database Queue in ASP

In previous section, I introduced how to send email EASendMail Service Queue. In this section, I will introduce how to send mass emails with advanced database queue in ASP.

Introduction

Although EASendMail service provides a faster way to send email in background, but there are thousands of emails in a task, the SendMailToQueue will be invoked for thousands times, obviously it is not effect way.

Therefore, EASendMail service provides a more effective way to send mass emails. In short, you just need to submit your database connection and record set once, EASendMail service will pick up the record set in background and send email to each record one by one. It is very useful to send mass emails in ASP web application.

send email using database queue in ASP/VBScript

To better understand the database queue, we need to create three tables in your SQL database like this:

CREATE TABLE [dbo].[rcpts](
    [uid] [bigint] IDENTITY(1,1) NOT NULL,
    [name] [nvarchar](50) NULL,
    [email] [nvarchar](128) NOT NULL,
 CONSTRAINT [PK_rcpts] PRIMARY KEY CLUSTERED
(
    [uid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
    IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[errorlog](
    [uid] [bigint] IDENTITY(1,1) NOT NULL,
    [email] [nvarchar](128) NULL,
    [server] [nvarchar](50) NULL,
    [errorcode] [nvarchar](50) NULL,
    [errordescription] [nvarchar](255) NULL,
 CONSTRAINT [PK_errorlog] PRIMARY KEY CLUSTERED
(
    [uid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
    IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[sentlog](
    [uid] [bigint] IDENTITY(1,1) NOT NULL,
    [server] [nvarchar](50) NULL,
    [email] [nvarchar](128) NULL,
 CONSTRAINT [PK_sentlog] PRIMARY KEY CLUSTERED
(
    [uid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
    IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Then insert two records in table ‘rcpts’ like this:

SQL table sample

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.

[ASP Example - Send mass emails with EASendMail Service Database Queue]

The following example codes demonstrate how to send mass emails using email queue + database in VBScript/ASP.

Note

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

<%
' The following example codes demonstrate sending email message using email queue + database
' To get full sample projects, please download and install EASendMail on your machine.
' To run it correctly, please change SMTP server, user, password, sender, recipient value to yours

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"

' Set your sender email address
oSmtp.FromAddr = "test@emailarchitect.net"
' Set email subject
oSmtp.subject = "simple email from ASP queue"

' If you want to EASendMail service send the email after 10 minutes, please use the following code.
' oSmtp.Date = DateAdd("n", 10, Now())

' change it to your sql server address, database, user and password
' The server/instance name syntax used in the server option is the same for all SQL Server connection strings.
' e.g.: Server=serveraddress\instancename;
' EASendMail will use the following connection to connect to the database,
' the syntax is same as ADO connection object.
oSmtp.AddHeader "X-Data-Connection", _
 "Driver={SQL Native Client};" & _
 "Server=serveraddress;Database=database;Uid=user;Pwd=password;"

' For more connection string
' MS SQL Server 2000
'"Driver={SQL Server};Server=localhost;Database=pubs;Uid=sa;Pwd=asdasd;"

' MS SQL Server 2005
'"Driver={SQL Server Native Client};Server=localhost;Database=pubs;Uid=sa;Pwd=asdasd;"
' MS SQL Server 2005 Native Provider
'"Provider=SQLNCLI;Server=serveraddress;Database=database;Uid=user;Pwd=password;"

' MS SQL Server 2008
'"Driver={SQL Server Native Client 10.0};Server=localhost;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
' MS SQL Server 2008 Native Provider
'"Provider=SQLNCLI10;Server=serveraddress;Database=database;Uid=user;Pwd=password;"

' MS SQL Server 2012
'"Driver={SQL Server Native Client 11.0};Server=localhost;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
' MS SQL Server 2012 Native Provider
'"Provider=SQLNCLI11;Server=serveraddress;Database=database;Uid=user;Pwd=password;"

' EASendMail will select the fields by the following sql statement
' before sending email,
' then pick the recipient address from specified field.
oSmtp.AddHeader "X-Sql-Select", "SELECT uid, name, email FROM Recipients"

' Pick "name" field as the recipient name and "email" field as the recipient address.
' You can also use {$var_srecord:fieldname} to pick any field in X-Sql-Select statement
' and put it to subject, bodytext, then EASendMail will replace it automatially.
oSmtp.DisplayTo = """{$var_srecord:name}"" <{$var_srecord:email}>"
oSmtp.AddHeader "X-Rcpt-To", "{$var_srecord:email}"

' EASendMail service will execute the following sql statement on
' every email was sent successfully.
oSmtp.AddHeader "X-Sql-OnSentSuccess", _
"INSERT INTO sentlog (server, email) VALUES('{$var_server}', '{$var_rcptaddr}')"

' EASendMail service will execute the following sql statement on
' every email could not be sent.
oSmtp.AddHeader "X-Sql-OnSentError", _
"INSERT INTO errorlog(email, server, errorcode, errordescription ) " & _
"VALUES('{$var_rcptaddr}', '{$var_server}', '{$var_errcode}', '{$var_errdesc}')"

Dim bodytext As String
bodytext = "Hi {$var_srecord:name}, " & Chr(13) & Chr(10)
bodytext = bodytext & "Send email with queue." & Chr(13) & Chr(10) & Chr(13) & Chr(10)
bodytext = bodytext & "From:Tester" & Chr(13) & Chr(10)
bodytext = bodytext & "To:{$var_srecord:email}" & Chr(13) & Chr(10) & Chr(13) & Chr(10)
bodytext = bodytext & "Your id in database is {$var_srecord:uid}." & Chr(13) & Chr(10)
oSmtp.bodytext = bodytext

' {$var_srecord:uid} {$var_srecord:name} {$var_srecord:email} in
' body text will be replaced by EASendMail automatically.

' 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

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

With above codes, no matter how many records in your table, SendMailToQueue is only invoked once and EASendMail service will pick up records in background and send the email based on each record to recipient one by one. It also inserts the results back to “errorlog” and “sentlog” tables automatically. You can open EASendMail Service Manager to monitor the queue and view Journal -> System Error to check if there is error with your database connection and SQL statement.

Database Server Driver

In X-Data-Connection header, you should specify a database driver to connect database. You can open “Control Panel” -> “Administrative Tools” - > “ODBC Data Sources” - “Drivers” to check current installed database drivers.

odbc drivers

Common SQL Driver Download

If SQL Server is installed on a remote server, and you don’t have SQL driver installed on local machine, then you need to download and install corresponding driver on local machine.

MS Access Database x64 Driver Download

Microsoft Access Database Engine 2010 Redistributable

See Also

Send Email in SQL Server Stored Procedure - Tutorial

Next Section

At next section I will introduce how to send email using Exchange Web Service (EWS) in VB6.

Send Email using Exchange Web Service - EWS in VB 6.0

In previous section, I introduced how to send mass email with EASendMail Service database queue. In this section, I will introduce how to send email using Exchange Web Service (EWS) in VB 6.0.

Introduction

Exchange Web Services (EWS), an alternative to the MAPI protocol, is a documented SOAP based protocol introduced with Exchange Server 2007. We can use HTTP or HTTPS protocol to send email with Exchange Web Services (EWS) instead of SMTP protocol.

With EASendMail SMTP Component, you do not have to build your EWS SOAP XML request and parse the response. It wraps the SOAP XML and HTTP request automatically. You just need to change the Protocol property, and then EASendMail uses Web Service protocol to send email. Your server SHOULD be Exchange 2007 or later version; otherwise you cannot use Exchange Web Service protocol.

  • SMTP protocol

    Standard SMTP protocol based on TCP/IP, all email servers support this protocol, Exchange Server also supports SMTP protocol. Using SMTP protocol is always recommended.

  • Exchange WebDAV

    Exchange WebDAV is a set of methods based on the HTTP protocol to manage users, messages in Microsoft Exchange Server. We can use HTTP or HTTP/HTTPS protocol to send email with Exchange WebDAV instead of SMTP protocol. But since Exchange 2007, WebDAV service is disabled by default, so I only suggest that you use WebDAV protocol in Exchange 2000/2003.

  • Exchange Web Service (EWS)

    Exchange Web Services (EWS), an alternative to the MAPI protocol, is a documented SOAP based protocol introduced with Exchange Server 2007. We can use HTTP or HTTPS protocol to send email with Exchange Web Services (EWS) instead of SMTP protocol. I only suggest that you use EWS protocol in Exchange 2007/2010/2013/2016/2019 or later version. Office365 also supports EWS very well.

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 Example - Send email with Exchange Web Service - EWS]

The following example codes demonstrate how to send email using Exchange Web Service (EWS) 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 = "simple email from VB 6.0 project"
    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project, do not reply"

    ' Your Exchange server address
    oSmtp.ServerAddr = "exch.emailarchitect.net"

    ' Set Exchange Web Service Protocol - EWS - Exchange 2007/2010/2013/2016/2019/Office365
    oSmtp.Protocol = 1

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

    ' By default, Exchange Web Service requires SSL connection.
    oSmtp.ConnectType = ConnectSSLAuto

    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

Exchange Server 2007 EWS Issue

If you send email to Exchange Server 2007 using EWS. You may get an exception: “Client does not have permissions to send as this sender”. You should add this line in your code:

' Set sender email address, please change it to yours
oSmtp.FromAddr = "test@emailarchitect.net"
oSmtp.Anonymous = 1

That is because Exchange Server 2007 doesn’t allow From header in the email message, the From header will be added by Exchange Server automatically.

Manage Send As Permissions in Exchange 2007/2010/2013

If you send email using SMTP protocol and it threw an exception: “Client does not have permissions to send as this sender” with Exchange 2007/2010/2013/2016/2019/Office365, please see the following topic:

Next Section

At next section I will introduce how to send email using Exchange WebDAV in VB6.

Send Email using Exchange WebDAV in VB 6.0

In previous section, I introduced how to send email using Exchange Web Service - EWS. In this section, I will introduce how to send email using Exchange WebDAV in VB 6.0.

Introduction

Exchange WebDAV is a set of methods based on the HTTP protocol to manage users, messages in Microsoft Exchange Server. We can use HTTP or HTTPS protocol to send email with Exchange WebDAV instead of SMTP protocol.

With EASendMail SMTP Component, you do not have to build your WebDAV request and parse the response. It wraps the WebDAV HTTP request automatically. You just need to change the Protocol property, and then EASendMail uses WebDAV protocol to send email. Your server SHOULD be Exchange 2000 or 2003 version; otherwise you cannot use Exchange WebDAV protocol. Although Exchange 2007 still supports WebDAV protocol, but the default status of WebDAV in Exchange 2007 is disabled, so you should use Exchange Web Service protocol with Exchange 2007 or later version.

  • SMTP protocol

    Standard SMTP protocol based on TCP/IP, all email servers support this protocol, Exchange Server also supports SMTP protocol. Using SMTP protocol is always recommended.

  • Exchange WebDAV

    Exchange WebDAV is a set of methods based on the HTTP protocol to manage users, messages in Microsoft Exchange Server. We can use HTTP or HTTP/HTTPS protocol to send email with Exchange WebDAV instead of SMTP protocol. But since Exchange 2007, WebDAV service is disabled by default, so I only suggest that you use WebDAV protocol in Exchange 2000/2003.

  • Exchange Web Service (EWS)

    Exchange Web Services (EWS), an alternative to the MAPI protocol, is a documented SOAP based protocol introduced with Exchange Server 2007. We can use HTTP or HTTPS protocol to send email with Exchange Web Services (EWS) instead of SMTP protocol. I only suggest that you use EWS protocol in Exchange 2007/2010/2013/2016 or later version. Office365 also supports EWS very well.

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 Example - Send email using Exchange WebDAV]

The following example codes demonstrate how to send email with Exchange WebDAV 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 = "simple email from VB 6.0 project"

    ' Set email body
    oSmtp.BodyText = "this is a test email sent from VB 6.0 project, do not reply"

    ' Your Exchange server address
    oSmtp.ServerAddr = "exch.emailarchitect.net"

    ' Set Exchange WebDAV Protocol - Exchange 2000/2003
    oSmtp.Protocol = 2

    ' User and password for Exchange WebDAV authentication
    oSmtp.UserName = "test"
    oSmtp.Password = "testpassword"

    ' If your WebDAV requires SSL connection, please add this line
    ' oSmtp.ConnectType = ConnectSSLAuto

    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

Total sample projects in EASendMail SMTP Component installation package.

VB 6.0 - Sample Projects for SMTP, SSL, TLS, Embedded Images, S/MIME, EWS, Email Queue

After you downloaded the EASendMail SMTP Component Installer and install it on your machine, there are many samples in the installation path.

All the samples locate at EASendMail Installation Folder. Most of sample projects demonstrate file attachment, embedded images, S/MIME, user authentication, SSL/TLS connection and Dns lookup.

ActiveX Object Sample Projects

ASP Classic

VBScript, JScript\Simple Send a simple email from ASP Classic.
VBScript, JScript\SimpleQueue Send email from ASP Classic to EASendMail Service.
VBScript\GmailOauth Send email using Gmail OAUTH/XOAUTH2.
VBScript, JScript\AdvancedQueueWithDatabase Send email from ASP Classic to EASendMail Service, background service will select recipients from database and write result back to database.

Delphi

Simple Send a simple email from Delphi 7.
HtmlMail Send text/html email using Web Browser Control Editor
Mass Send mass emails using thread pool.
Oauth Send email using Gmail/Office365/Hotmail OAUTH/XOAUTH2.

MS SQL Server

SQL Send email from MS SQL Server stored procedure.

Script

VBScript/JScript/WScript Send a simple email from VBScript/JScript/WScript.

VB6

Simple Send a simple email from VB 6.0.
HtmlMail Send text/html email using Web Browser Control Editor
Mass Send mass emails using thread pool.
Oauth Send email using Gmail/Office365/Hotmail OAUTH/XOAUTH2.

VC++

Simple Send a simple email from VC++.
HtmlMail Send text/html email using Web Browser Control Editor
Mass Send mass emails using thread pool.
Oauth Send email using Gmail/Office365/Hotmail OAUTH/XOAUTH2.

.NET Framework Sample Projects

ASP.NET Form

C#, VB, JScript\Simple Send a simple email from ASP.NET form.
C#, VB, JScript\SimpleQueue Send email from ASP.NET to EASendMail Service.
C#, VB\GmailOauth Send email using Gmail OAUTH/XOAUTH2.
C#, VB, JScript\AdvancedQueueWithDatabase Send email from ASP.NET to EASendMail Service, background service will select recipients from database and write result back to database.

ASP.NET MVC

C#, VB\WebProject1\SimpleController Send a simple email from ASP.NET MVC by Form Post/Ajax Post.
C#, VB\WebProject1\GmailOauthController Send email using Gmail OAUTH/XOAUTH2.
C#, VB\WebProject1\MassController Send mass emails using background thread pool.
C#, VB\WebProject1\DbRecipientsController Send mass emails using background thread pool, select recipients from database and write result back to database.

.NET Desktop (Windows Form)

C#, VB\Simple Send a simple email from .NET Windows Form.
C#, VB\HtmlMail Send text/html email using Web Browser Control Editor
C#, VB\Mass Send mass emails using thread pool.
C#, VB\Oauth Send email using Gmail/Office365/Hotmail OAUTH/XOAUTH2.

Microsoft Store App (UAP)

C#, VB\Simple Send text/plain or html email from Microsoft Store App (UAP).
C#, VB\Mass Send mass emails using thread pool from Microsoft Store App (UAP).
C#, VB\GmailOauth Send email using Gmail OAUTH/XOAUTH2.

Windows CE/PocketPC

C#, VB\pocketpc.mobile Send a simple email from .NET Compact Framework.

PowerShell

PowerShell Tutorial and Sample

Free Email Support

Not enough? Please contact our technical support team.

Support@EmailArchitect.NET

Remarks

We usually reply emails in 24hours. The reason for getting no response is likely that your smtp server bounced our reply. In this case, please try to use another email address to contact us. Your Gmail, Hotmail email account is recommended.

Appendix

Comments

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