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.
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.
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:
Add Reference of EASendMail ActiveX Object to Visual Basic 6.0 Project
To use EASendMail SMTP ActiveX Object in your project, the first step is "Add reference of EASendMail to your project". Please choose menu->"Project" -> "References"- > and choose the "EASendMailObj ActiveX Object", click "OK", the reference of EASendMail ActiveX Object will be added to your project, and you can start to use EASendMail to send email in your project.
Private Sub Command1_Click() Dim oSmtp As New EASendMailObjLib.Mail oSmtp.LicenseCode = "TryIt" ' Set your sender email address oSmtp.FromAddr = "firstname.lastname@example.org" ' Add recipient email address oSmtp.AddRecipientEx "email@example.com", 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 = "firstname.lastname@example.org" oSmtp.Password = "testpassword" ' If your smtp server requires SSL connection, please add this line ' oSmtp.SSL_init MsgBox "start to send email ..." If oSmtp.SendMail() = 0 Then MsgBox "email was sent successfully!" Else MsgBox "failed to send email with the following error:" & oSmtp.GetLastErrDescription() End If End Sub
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.
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.
Email Address Syntax and Multiple Recipients
For single email address (From, ReplyTo, ReturnPath), the syntax can be: ["][display name]["]<email address>. For example: "Tester, T" <email@example.com> Tester <firstname.lastname@example.org> <email@example.com> firstname.lastname@example.org 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" <email@example.com>, Tester2 <firstname.lastname@example.org>, <email@example.com>, firstname.lastname@example.org
oSmtp.FromAddr = "Tester<email@example.com>" oSmtp.FromAddr = "firstname.lastname@example.org" oSmtp.FromAddr = "<email@example.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: "firstname.lastname@example.org, email@example.com" oSmtp.AddRecipientEx "firstname.lastname@example.org, email@example.com", 0 oSmtp.AddRecipientEx "Test1<firstname.lastname@example.org>, Test2<email@example.com>", 0 ' You can also add carbon copy (CC) or blind carbon copy (BCC) in the email. oSmtp.AddRecipientEx "CC recipient<firstname.lastname@example.org>", 1 oSmtp.AddRecipientEx "Bcc recipient<email@example.com>", 2
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:
This property indicates the original email sender. This is what you see as the "FROM" in most mail clients.
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.
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.
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, VBA - From, ReplyTo, Sender and Return-Path] Dim oSmtp As New EASendMailObjLib.Mail oSmtp.LicenseCode = "TryIt" oSmtp.FromAddr = "firstname.lastname@example.org" oSmtp.ReplyTo = "email@example.com" oSmtp.Sender = "firstname.lastname@example.org" oSmtp.ReturnPath = "email@example.com" [VBScript, ASP - From, ReplyTo, Sender and Return-Path] Dim oSmtp Set oSmtp = Server.CreateObject("EASendMailObj.Mail") oSmtp.LicenseCode = "TryIt" oSmtp.FromAddr = "firstname.lastname@example.org" oSmtp.ReplyTo = "email@example.com" oSmtp.Sender = "firstname.lastname@example.org" oSmtp.ReturnPath = "email@example.com"
If you want to set Higher or Lower priority to your email, you can use Priority prority
' Set high priority oSmtp.Priority = 1 'High priority
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
Common "Socket/Networking Connection" Error
Using Telnet to detect networking connection to SMTP server
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:
SMTP 25, 587, 465 port
"5xx ... IP address block or on black list or bad reputation" Exception
"5xx user authenticaton" Error
"5xx relay denied" Error
"5xx Must issue a STARTTLS command first"
' If your smtp server requires TLS connection, please add this line oSmtp.SSL_init
"No supported authentication marshal found!"
Other error returned by SMTP server
[VB 6.0 - Using log file to detect SMTP server response - Example]
oSmtp.LogFileName = "d:\smtp.txt"
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 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
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
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.Next: Send email over SSL in VB 6.0 >
If you have any comments or questions about above example codes, please click here to add your comments.