score:13

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 = "http://schemas.microsoft.com/cdo/configuration/"
    flds.item(schema & "sendusing") = cdosendusingport
    flds.item(schema & "smtpserver") = "mail.myserver.com"
    flds.item(schema & "smtpserverport") = 25
    flds.item(schema & "smtpauthenticate") = cdobasic
    flds.item(schema & "sendusername") = "email@email.com"
    flds.item(schema & "sendpassword") = "password"
    flds.item(schema & "smtpusessl") = false
    flds.update

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

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

score:0

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: https://www.codeproject.com/articles/7859/building-com-objects-in-c

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

score:1

the following ms-access vba code works for smtp.office365.com. 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 = "name@myaddress.com"
emailobj.to = "name@youraddress.com"
emailobj.subject = "test cdo"
emailobj.textbody = "test cdo"
'emailobj.addattachment "c:\windows\win.ini"

set emailconfig = emailobj.configuration


emailconfig.fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailconfig.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailconfig.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
'exclude the following line    
'emailconfig.fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
emailconfig.fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailconfig.fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "name@myaddress.com"
emailconfig.fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
emailconfig.fields.update

emailobj.send

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

score:2

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

https://msdn.microsoft.com/en-us/library/ms526453(v=exchg.10).aspx https://msdn.microsoft.com/en-us/library/ms526983(v=exchg.10).aspx

score:12

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
objmessage.to = stoaddress
'objmessage.cc = sccaddress
objmessage.textbody = sbody
'objmessage.addattachment smailattachment
objmessage.configuration.fields.item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objmessage.configuration.fields.item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smailserver
objmessage.configuration.fields.item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objmessage.configuration.fields.update
objmessage.send

more info: http://msdn.microsoft.com/en-us/library/ms526318(v=exchg.10).aspx


Related Query

More Query from same tag