Accepted answer

windows includes an object called collaborative data objects or cdo. this object allows you to send emails using any smtp server assuming that other prerequisites are met (firewall open, isp not blocking ports, account is configured on the smtp server, smtp server allows relaying, etc).

most of the examples i've found use late binding, which is preferred. in my testing on xp it appeared that the correct library reference, if you prefer to use early binding, is "microsoft cdo for windows 2000 library".

it's important to know that any time you send email you will have to send it through (or out of) some kind of email server. this means you will have to authenticate with that email server and also usually means that you need to send the email out using a "from" email address that exists on that very email server.

here's some code using late binding:

const cdosendusingpickup = 1
const cdosendusingport = 2
const cdoanonymous = 0
' use basic (clear-text) authentication.
const cdobasic = 1
' use ntlm authentication
const cdontlm = 2 'ntlm

public sub sendemail()
    dim imsg as object
    dim iconf as object
    dim flds as object
    dim schema as string

    set imsg = createobject("cdo.message")
    set iconf = createobject("cdo.configuration")
    set flds = iconf.fields

    ' send one copy with smtp server (with autentication)
    schema = ""
    flds.item(schema & "sendusing") = cdosendusingport
    flds.item(schema & "smtpserver") = ""
    flds.item(schema & "smtpserverport") = 25
    flds.item(schema & "smtpauthenticate") = cdobasic
    flds.item(schema & "sendusername") = ""
    flds.item(schema & "sendpassword") = "password"
    flds.item(schema & "smtpusessl") = false

    with imsg
        .to = ""
        .from = ""
        .subject = "test send"
        .htmlbody = "test"
        '.sender = "sender"
        '.organization = "my company"
        '.replyto = ""
        set .configuration = iconf
    end with

    set iconf = nothing
    set imsg = nothing
    set flds = nothing
end sub


at my company i used a other solution. i have created a c# class library with com classes / objects. com classes can be implemented in your access application and this way you can use all the advantages of c# (mailing for example) and still use it (calling it) in access.

the only disadvantage is that you have to register your class library (dll) at all the computers who use your access application. i have done that with a simple power-shell script which executes at the start of the access application.

a good start for a com based library is here:

if you would like some more information about it then i am always happy to help you.


the following ms-access vba code works for you do indicate smtpusessl=true, but you do not specify the port, otherwise you get error 5.7.57.

sub smpttest2()
set emailobj = createobject("cdo.message")

emailobj.from = "" = ""
emailobj.subject = "test cdo"
emailobj.textbody = "test cdo"
'emailobj.addattachment "c:\windows\win.ini"

set emailconfig = emailobj.configuration

emailconfig.fields("") = 2
emailconfig.fields("") = 1
emailconfig.fields("") = ""
'exclude the following line    
'emailconfig.fields("") = 587
emailconfig.fields("") = true
emailconfig.fields("") = ""
emailconfig.fields("") = "mypassword"


if err.number = 0 then msgbox "done"
end sub


i cannot add this to the comments because i do not have enough reputation, so please don't axe me.

"seems like this method lets you spoof about anything on my server. just noticed that there's an addattachment method. could that work with just a relative path to say, an excel sheet? "

it works for me (access 2010, exchange 2010):

.addattachment ("url here")


this works for me in ms access 2010 / windows 7

smailserver = "myispsmtp" 'not just any old smtp
smailfromaddress = "me"
smailtoaddress = "me"

set objmessage = createobject("cdo.message")
stoaddress = smailtoaddress
ssubject = "subject"
sbody = "mailbody"

objmessage.subject = ssubject
objmessage.from = smailfromaddress = stoaddress
' = sccaddress
objmessage.textbody = sbody
'objmessage.addattachment smailattachment
objmessage.configuration.fields.item("") = 2
objmessage.configuration.fields.item("") = smailserver
objmessage.configuration.fields.item("") = 25

more info:

Related Query

More Query from same tag