EASendMail SMTP Component > Using EASendMail in VB 6.0 to send email
This page has moved. Click Here! to redirect to our new page.
Introduction
EASendMail is a SMTP component which supports all operations of SMTP/ESMTP protocols (RFC 821, RFC 822, RFC 2554). This tutorial covers everything of sending email with EASendMail in VB 6.0.
Installation
You should download the EASendMail SMTP Component Installer and install it on your machine at first.
A simple email sending project
First of all, 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.
Now add the following code in your VB form like this:
'[VB 6.0 Example - Send Simple Email]
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"
'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.
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.
When you execute above example code, if you get error about "Networking connection" or "No such host", it is likely that your SMTP server address is not correct. If you get an error like "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.
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
Mail Address Syntax in EASendMail SMTP Component:
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> or 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
To better understand the email address syntax in EASendMail, please refer to the following codes.
'[VB 6.0 Example - Email 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
Send email over SSL Connection
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. There are two ways to deploy SSL on SMTP server: 1. Using STARTTLS command to switch SSL channel on normal SMTP port (25); 2. Deploying SSL on another port (465 or other port, you may query it from your server administrator) directly. EASendMail SMTP component supports both ways.
The following samples demonstrate how to send email over SSL connection with Gmail, Yahoo and Hotmail account.
Send email by Gmail account with SSL connection
'[VB 6.0 Example - Sending Email by Gmail Account with SSL Connection]
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.
oSmtp.UserName = "gmailid@gmail.com"
oSmtp.Password = "yourpassword"
''Send email over SSL/TLS connection.
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
Send email by Yahoo account with SSL connection
'[VB 6.0 Example - Send Email by Yahoo account with SSL Connection]
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"
'Yahoo user authentication should use your
'Yahoo email address as the user name.
oSmtp.UserName = "myid@yahoo.com"
oSmtp.Password = "yourpassword"
'Because yahoo deploys SMTP server on 465 port with direct SSL connection.
'So we should change the port to 465.
oSmtp.ServerPort = 465
''Send email over SSL/TLS connection.
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
Send email by MSN/hotmail account with SSL connection
'[VB 6.0 Example - Send Email by MSN/Hotmail Account with SSL Connection]
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.live.com"
'Hotmail user authentication should use your
'hotmail email address as the user name.
oSmtp.UserName = "liveid@hotmail.com"
oSmtp.Password = "yourpassword"
''Send email over SSL/TLS connection.
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
Reply-To, Return-Path and Mail Priority
If you want to set another email address to get the replied email rather than your From address, you can use ReplyTo property.
If you want to set another email address to get the delivery report rather than your From address, you can use ReturnPath property.
If you want to set Higher or Lower priority to your email, you can use Priority prority
Send email directly without SMTP server(MX DNS lookup)
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.
'[VB 6.0 Example - Send Email Directly without SMTP Server]
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.
Send email with Html body
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. The following sample demonstrates how to use BodyFormat to send a HTML email.
'[VB 6.0 Example - Send HTML Email]
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 email body
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"
'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
After you received the email by your email client, the body text is like this:
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.xx samples in EASendMail SMTP Component installer. Those samples demonstrate how to build a HTML email editor and send HTML email with attachment or embedded pictures/images.
Send email with Attachment
To send an email with attachment, we need to use AddAttachment method. This method can attach a file to the email message from local disk or a remote URL.
'[VB 6.0 Example - Send Email with Attachment]
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 email body
oSmtp.BodyText = "<font size=5>This is</font> <font color=red><b>a test</b></font>"
'adds attachment from local disk
If oSmtp.AddAttachment( "c:\test.doc" ) <> 0 Then
MsgBox "Failed to add attachment with error:" & oSmtp.GetLastErrDescription()
End If
'adds 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"
'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
Send email with Embedded Picture/Image
To attach an embedded picture 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" />.
'[VB 6.0 Example - Send Email with Embedded Image/Picture]
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"
'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
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.
Send email with Digital Signature(S/MIME)
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".
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.
'[VB 6.0 Example - Send Email with Digital Signature (S/MIME)]
Const CRYPT_MACHINE_KEYSET = 32
Const CRYPT_USER_KEYSET = 4096
Const CERT_SYSTEM_STORE_CURRENT_USER = 65536
Const CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072
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"
'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"
'If your smtp server requires SSL connection, please add this line
'oSmtp.SSL_init
'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
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
Send email with Email Encryption(S/MIME)
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.
'[VB 6.0 Example - Encrypt email with certificate (S/MIME)]
Const CRYPT_MACHINE_KEYSET = 32
Const CRYPT_USER_KEYSET = 4096
Const CERT_SYSTEM_STORE_CURRENT_USER = 65536
Const CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072
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"
'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"
'If your smtp server requires SSL connection, please add this line
'oSmtp.SSL_init
'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
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:
Send email with 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.
Send email with Event Handler
After SendMail method is invoked, if you want to know the progress of the email sending, you should use Event Handler. Event handler only supports asynchronous mode
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 sample codes demonstrate how to send email in asynchronous mode.
'[VB 6.0 Example - Send Email with Asynchronous Mode + Event Handler]
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"
'If your smtp server requires SSL connection, please add this line
'oSmtp.SSL_init
'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
Send email with Multiple Threads(Mass Mail)
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.
The following sample codes demonstrate how to send mass emails with FastSender object in VB6.0
'[VB 6.0 Example - Send Mass Emails with Multiple Threads(Mass Mail)]
Option Explicit
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"
'If your smtp server requires SSL connection, please add this line
'oSmtp.SSL_init
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
Total Sample Projects
After you downloaded the EASendMail SMTP Component Installer and install it on your machine, there are many samples in the installation path.
| asp | Send email from ASP (VBScript, JScript) - ActiveX/COM |
| asp_queue | Send email from ASP to EASendMail Service. (VBScript, JScript) - ActiveX/COM |
| asp_queue_database | Send email from ASP to EASendMail Service, EASendMail service will select recipients from database. (VBScript, JScript) - ActiveX/COM |
| asp_net | Send email from ASP.NET. (C#, VB, JScript.NET) |
| asp_net_batch | Send bulk emails with multiple threads from ASP.NET. (C#, VB) |
| asp_net_queue | Send email from ASP.NET to EASendMail Service. (C#, VB, JScript.NET) |
| asp_net_queue_database | Send email from ASP.NET to EASendMail Service, EASendMail service will select recipients from database. (C#, VB, JScript.NET) |
| simple.vb6 | Send text/plain email from Visual Basic 6.0. This sample also demonstrates digital signature, email encryption. (VB6) - ActiveX/COM |
| simple.vcNative | Send text/plain email from Visual C++. This sample also demonstrates digital signature, email encryption. (Visual C++) - ActiveX/COM |
| simple.vb | Send text/plain email from Visual Basic.NET. This sample also demonstrates digital signature, email encryption. |
| simple.csharp | Send text/plain email from C#. This sample also demonstrates digital signature, email encryption. |
| simple.vc | Send text/plain email from managed c++. This sample also demonstrates digital signature, email encryption. |
| htmlmail.vb6 | Send text/html email from Visual Basic 6.0. This sample also demonstrates embedded pictures, digital signature, email encryption. (VB6) - ActiveX/COM |
| htmlmail.vcNative | Send text/html email from Visual C++. This sample also demonstrates embedded pictures, digital signature, email encryption. (Visual C++) - ActiveX/COM |
| htmlmail.vb | Send text/html email from Visual Basic.NET. This sample also demonstrates embedded pictures, digital signature, email encryption. |
| htmlmail.csharp | Send text/html email from C#. This sample also demonstrates embedded pictures, digital signature, email encryption. |
| mass.vb6 | Send email by FastSender with multiple threadings. This sample also demonstrates email address validating. (VB6) - ActiveX/COM |
| mass.vb | Send email by BeginSendMail with multiple threadings. This sample also demonstrates email address validating. |
| mass.csharp | Send email by BeginSendMail with multiple threadings. This sample also demonstrates email address testing. |
| samples_vs2008\pocketpc.mobile.cs | Send email from PocketPC/Windows Mobile System. |
| samples_vs2008\pocketpc.mobile.vb | Send email from PocketPC/Windows Mobile System. |
See Also
Send Email in Visual Basic.NET - Toturial
Send Email in C#/CSharp.NET - Toturial
Send Email in Managed C++ - Toturial
Send Email in Visual C++ - Toturial
Free Email Support
Not enough? Please contact our technical support team.
Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered User)
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 Hotmail or Yahoo email account is recommended.
|