Send Email(E-Mail) from VB6 using CDO

This article is written by Pon Saravanan  on 04-Jul-07 Last modified on :21-Apr-10

VB6 Send Email Using SMTP and CDO

In enterprise application sending email from applications is a inevitable task. Particularly for notifications emails are one of the best medium to communicate.Normally the SMTP server will be in seperate location and reachable from application server or web servers. In this scenerio we need to specify to CDO which SMTP server we are going to use. Then only the relay will be succesful.

This is a simple code to send email from applications. Now this function supports CC, BCC and attachments.


Even though there are several ways in vb6 send email . The following function is very easiest and supporting most of the task that are needed.
The function will return the status about sent. Since this function designed for applications, the related settings can be fetched from settings file rather than changing the code and compiling.


Basically we are going to use the following objects for sending  and configuring the cdo object
•  CDO.Message
• CDO.Configuration


This object is used to set the various properties like following

• To
• From
• Subject
• TextBody
• Attachment
• CC

Finally we can call the Send method.



This object is used to set the configurations like
• Send using
• SMTP server
• SMTP server port

To resolve "The transport failed to connect to the server"

Please change the respective SMTP server in the "change your SMTP server here" section. Or else you will get the "The transport failed to connect to the server." error message.

VBknowledgebase uses this function which needs smtp Authentication. In order for this function to work, You must have a valid smtp server with a valid user name and password.

It happened to me that my user account was locked once since i have tried several times with bad password. Then the setting wont work for a while. So it is better if you have any doubts use any utilities which may help to test the test email before using this function. So make sure you have valid information regarding the SMTP server before using this function.

VB6 send email Source Code


Private Sub Command1_Click()
    Dim colAtt As New Collection
    colAtt.Add "c:\boot.ini"
    colAtt.Add "c:\boot.bak"
    MsgBox SendEmail("", "test boot", "Get Boot.ini", "", "ccname2@server.com4", colAtt)
End Sub
'*  Purpose :   To Send eMail
'*  Inputs  :   strRecipient(String)    Recipient comma seperated
'*              strSubject(String)      Subject
'*              strBody                  Body
'*              colAttachments          Collection of attachments
'*                                      file paths.
'*  Returns :   Boolean about the sent status
Public Function SendEmail(ByVal strSender As String, _
                        ByVal strRecipient As String, _
                        ByVal strSubject As String, _
                        ByVal strBody As String, _
                        Optional ByVal strCc As String, _
                        Optional ByVal strBcc As String, _
                        Optional ByVal colAttachments As Collection _
                         ) As Boolean
    Dim cdoMsg As New CDO.Message
    Dim cdoConf As New CDO.Configuration
    Dim Flds
    Dim attachment
    Dim strHTML
    On Error GoTo ErrTrap
    Const cdoSendUsingPort = 2
    'Set cdoMsg =  CreateObject("CDO.Message")
    'Set cdoConf = CreateObject("CDO.Configuration")
    Set Flds = cdoConf.Fields
    With Flds
        .Item("") = 2 '
        .Item("") = "change your smtp server here"
        .Item("") = 25
    End With
    ' Apply the settings to the message.
    With cdoMsg
        Set .Configuration = cdoConf
        .To = strRecipient
        .From = strSender
        .Subject = strSubject
        .TextBody = strBody
        If Not colAttachments Is Nothing Then
            For Each attachment In colAttachments
                .AddAttachment attachment
        End If
        If strCc <> "" Then .CC = strCc
        If strBcc <> "" Then .BCC = strBcc
    End With
    Set cdoMsg = Nothing
    Set cdoConf = Nothing
    Set Flds = Nothing
    SendEmail = True
    Exit Function
Err.Raise Err.Number, "", "Error from Functions.SendEmail" & Err.Description
    SendEmail = False
End Function
function needs reference to microsoft cdo library

This article discuss about VB6 send email.
For version please refer Send Email from Asp.Net

  • pons
    Please change your mail server in the "change your mail server here" section.
    or else you will get the "The transport failed to connect to the server."
    if you are getting different error please let me know so that
    i can assist you.thanks
    Pon saravanan
    10/8/2007 11:02:22 AM

    it didnt work. i have change my mail server but i got this error message: 4/27/2008 6:50:30 PM

    it didnt work.
    i got this message as well The message could
    not be sent to the SMTP server.
    The transport code was 0x800cc6a.
    The server response was 451 please try again later.
    Does that mean the SMTP requires authentication before it can send mail?
    you can contact me if you need to ask me any further questions,
    my email address is
    4/27/2008 6:54:43 PM

    it didnt work it shows the following err message:

    Error from Functions.SendEmailThe message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available

    pl help my email id is [email removed by spam filters]
    6/25/2008 7:04:39 AM

  • pons
    Hi seenuvasan
    i have checked the function, It works perfectly. But the problem is with the settings. If your settings are not correct, the mail will not be sent. Please check the SMTP server connection. and try to test it with some tools to send messsage. It may actively refuse the commands from you

    6/25/2008 7:20:38 AM

    any sample gmail smtpserver 8/4/2008 1:52:31 AM

    I changed it to "" & port to "465" but I also get "The transport failed to connect to the server." error! 5/28/2009 9:30:31 AM

    nice 9/1/2009 4:40:45 AM

    We were using the similar code and it was working fine. But we are now getting the following error.

    Error # -2147220973
    Source: CDO.Message.1
    Description: The transport failed to connect to the server.

    what may be the cause for this and possible resolution? We tried using ip as well as DNS just to make sure that its not an issue with DNS resolution. But it is throwing error in both cases. Please help us out.

    Thanks in advance
    12/8/2009 11:45:33 PM

  • pons
    1. Make sure that the SMTP Server address is correct,

    2. Please check whether any firewall is in between your application and SMTP server .

    3. Make sure that the port number of the SMTP Server is also correct in 'smtpserverport' configuration

    Sample code will be(already in article body)
    Set Flds = cdoConf.Fields

    With Flds
    .Item("") = 2 '
    .Item("") = "change your smtp server here"
    .Item("") = 25

    are you using .NET 2.0? then use the System.Net.Mail classes
    12/9/2009 1:39:12 AM

    Thanks pons for your reply. We are using the same fields as you mentioned above with correct SMTP server (to which we can ping from command prompt) We are using VB6. 12/9/2009 4:13:23 AM

  • pons
    If you are using an SMTP server which needs authentication
    try additonal items for authentications
    .Item("")= 1
    .Item("") = "username"
    .Item("") = "password"
    I hope it works
    12/9/2009 4:55:17 AM


    it's works only servers.
    have checked the function, It works perfectly on serverside. But the problem is with the node ''transport failed to connect to the server''.

    pls help.

    thanks in advance,
    1/30/2010 2:15:00 AM

    Hi venkat
    You should be able to resolve as mentioned in the article at section **To resolve "The transport failed to connect to the server"**

    If you still unable to reach your SMTP server, try adding the following as mentioned in previous comment
    .Item("")= 1
    .Item("") = "username"
    .Item("") = "password"
    1/30/2010 7:30:47 AM

    When I'm sending mail from "me" to "me", it is working fine. However when the destination is a different address, I get the error "server rejected one or more recipients. Response : 553 sorry, domain isn't allowed thru this MTA without auhtentication #5.7.1". When tried authentication as given by you, I get following message:"The message could not be sent to SMTP server. Transport error code 0x80040217. Server response not available"

    Please help me.
    3/2/2010 11:17:41 AM

  • pons
    Please check any firewall between your app/web server and the SMTP server. If there are any then try configuring the firewall to allow the Ipaddress,Port of the app/web server.

    Hope that helps
    3/2/2010 5:53:35 PM

    Can i send email using CDO?
    in which sender can display his email_id....
    3/29/2010 7:44:00 AM


    I am unable to understand your question, can you please elaborate a bit further.

    3/29/2010 6:56:13 PM

    How can I get the smt server name for a particular domain name? 3/29/2010 10:38:41 PM

  • pons
    You may check with your administrator who is managing the domain. 3/30/2010 12:09:23 AM

    it works fine on the computer where i have developed VB6 Project but got error on the other Pcs.
    error "transport failed to connect to the server" or some times error occurs like "Server response not available"
    can u diagnosis the problem.
    Thanx in Advance
    6/17/2010 12:15:21 AM

  • pons
    Sure, But if you have some time to read this article and the comments followed by. I guess you can fix the issue. If not please describe with enough information for me to dianose.

    6/17/2010 12:31:17 AM


    This program works fine for me several months, but today its not working. showing the error "The transport failed to connect to the server". Why is it so ? my smtp server is
    6/22/2010 1:54:45 AM

  • pons
    May be smtp server has disallowed you. You contact them and findout what is happening. Please check any firewall between your application server and SMTP server and check your application server is able to access the SMTP server. 6/22/2010 1:59:01 AM

    I have same problem: "The transport failed to connect to the server"

    My code is similar to yours. OS: Windows XP Professional.

    My code runs perfectly in developer machine (Windows XP Professional with VB6) but in production machine with same network and smtp configuration I get that error .

    Any others machines (developer machines) this code runs without problem.

    I installed VB6 Runtimes (sp5 and sp6) in production machine, but I get same error. Maybe this error is in CDOSys/OS installation?

    Newer Microsoft Outlook install CDO, I tested with Microsoft Outlook 2003 without success. Any sugestion?

    Sorry, my english is bad.
    10/1/2010 6:33:02 AM

    I dont think the exception could be due to the CDO Libraries. But i strongly believe that the Production server is unable to communicate with the SMTP Server. Check for any firewall between them. Please read through the comments for various possiblities on this issue 10/1/2010 6:40:41 AM

    Well, there are no firewall between production machine and smtp server, only a transparent proxy server without any restrictions.

    Production machine and development machine is in same network without any security system enabled (no firewall, this network is a simple adsl connection for test purposes).
    Windows Firewall is disabled too. No antivirus in production machine.

    I think you're right. I tested connection in production machine via telnet and I got error (time out). In my development machine I can connect (after long wait time) via telnet. This looks like a bad Internet connection that causes time out errors.

    Thanks very much for help.

    10/1/2010 7:07:28 AM

    failed to connect to the server 10/14/2010 8:13:55 AM

    ok 10/18/2010 4:53:50 AM

    good 12/18/2010 4:43:03 PM

    The destination do received the email. But without text body data and nosubject.
    Do you have any idea
    5/10/2011 1:55:22 PM

    thanks for the code .
    can you help me

    what valuse i should fill the following field:

    MsgBox SendEmail(",", "test boot", "Get Boot.ini", "", "ccname2@server.com4", colAtt)

    waiting for reply
    6/17/2011 10:42:42 PM

    i used a Gmail address and got a error message as bellow:

    Error from Functions.SendEmail:
    The server rejected the sender address. The server response was: 530 5.7.0 Must issue a STARTTLS command first. b4sm6102983pba.59

    Can anyone help me!!? You can replay me at kt_maker[at]mail[dot]com


    8/8/2011 5:02:45 AM

    This code works well if you have a valid SMTP server.
    Thank you very much.
    11/25/2011 3:27:15 AM

    If you don't send your email user name and password you can only send mails to and from yourself. However, when you pass these parameters you can send mail anywhere.
    Do it like this:
    With Flds
    .Item("") = 2
    .Item("") = "[your server]"
    .Item("") = [port number]
    .Item(cdoSMTPAuthenticate) = cdoBasic
    .Item(cdoSendUserName) = "[your email user name]"
    .Item(cdoSendPassword) = "[your email password]"
    End With

    The port number is not necessarily 25.
    For example, my server uses port 587 (for security reasons)
    Hope this helps...
    11/25/2011 7:04:31 AM

    The code is missing the double quotes between Name1 and ccName so shows a missing argument. 2/24/2012 5:53:44 AM

    how i can i send embedded image in the email body?

    My email is

    11/23/2012 7:03:27 PM

    Hi Pon Saravanan,

    I'm using vb6.0 and CDO's for sending email.[Windows7]
    The below code working good for gmail, yahoo, nate, hotmail
    It's not working for naver,

    I'm getting below error:
    Run-time error '2147220975(80040211)':
    The message could not be sent to the SMTP server.
    the transport error code was 0x80040217.
    the server response was not available.

    naver : SendEmail("", "465", "2", "", "pwd", "subj-naver")
    Daum : SendEmail("", "465", "2", "", "pwd", "subj-daum")


    Sub SendEmail(strServer As String, _
    strPort As String, _
    strSendUsing As String, _
    strUsername As String, _
    strPwd As String, _
    strSubject As String)

    Dim NewMail As CDO.Message

    Set NewMail = New CDO.Message

    NewMail.Configuration.Fields.Item _
    ("") = True

    NewMail.Configuration.Fields.Item _
    ("") = 1

    NewMail.Configuration.Fields.Item _
    ("") = strServer

    NewMail.Configuration.Fields.Item _
    ("") = strPort '465

    NewMail.Configuration.Fields.Item _
    ("") = strSendUsing '2

    NewMail.Configuration.Fields.Item _
    ("") = strUsername

    NewMail.Configuration.Fields.Item _
    ("") = strPwd


    With NewMail
    .Subject = "Test Mail from" & strSubject
    .From = strUsername
    .To = ""
    .CC = ""
    .BCC = ""
    .BodyPart.Charset = "utf-8"
    .TextBody = "Body"
    End With

    MsgBox ("Mail has been Sent")

    Set NewMail = Nothing

    End Sub

    How can I make it? Please help me.

    Thanks in advance,
    2/18/2013 3:23:17 AM

Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    

Spam Bot Trap


Select Theme