Send Email with CDO

fenhow

Registered User.
Local time
Today, 13:10
Joined
Jul 21, 2004
Messages
599
Hi, I am wondering if it is possible to send an email using CDO where I can add a field from my form to the Subject or textbody of the email message?
This is the code I am currently using, it works great but I want to add some information to the email regarding the record from which the email was sent.

Public Function send_email_PO()

Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "blablabla"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "*****"
.Update
End With
' build email parts
With cdomsg
.To = "email@here" .From = "email@here"
.Subject = "Record Updated"
.TextBody = "This is an automatically generated email, please do not reply because no one will see it and you will be left wondering why you are not getting a response. ***"
.Send
End With
Set cdomsg = Nothing
End Function
 
Solved
.Subject = "SOME TEXT HERE " & " " & [Forms]![name of form]![name of field]
 
Fenhow,
Is this still working for you? I'm trying to email directly through my access app like this in gmail, but I'm getting an error, and gmail is telling me this is not a "modern" application so it won't accept it.

I'm running access 2010. Did you need to add any references? I've added CDO for 2000 exchange, but maybe I need another?
 
I'm getting an error, and gmail is telling me this is not a "modern" application so it won't accept it.
Can you post the complete error message?
Maybe try to use smtpserverport 465.

Did you need to add any references? I've added CDO for 2000 exchange, but maybe I need another?

If you use CreateObject("CDO.message"), you do not need any references for this.
 
Ok, I've got it straightened out now. I needed to go into my gmail settings and allow less secure apps.

I also had some trouble with the code above.

For it to work for me, I needed to change it to as shown below:

Code:
        msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
        msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
        msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUserName
        msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strPassWord
        msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        msg.Configuration.Fields.Update
 

Users who are viewing this thread

Back
Top Bottom