Sending mail with CDO through a remote SMTP server

B

Bill22

I want to send emails from VBA using the CDO functionality. I have use
the method described by Ron De Bruin successfully in the past but onl
using my local SMTP server. I now need to do the same with a remot
server, such as Gmail.

I found something potentially suitable in the ever-informative Ron d
Bruin's downloaded example
(http://www.rondebruin.nl/files/CDO_Example_Code.zip) which gives sampl
code for using with Gmail. This works, and emails do get sent and ar
received. However, the sticking point for me is what goes into the Fro
(or reply-to) field in the delivered email. The comments where he say
you can use the .ReplyTo parameter before the .Send seem appropriate
Here they a

' Note: The reply address is not working if you use this Gmail example
' It will use your Gmail address automatic. But you can add this line
' to change the reply address .ReplyTo = "(e-mail address removed)"

Sorry Ron, this doesn't work (at least for me). Here's my simplified an
anonymousised code, which is virtually identical to RdB's code except
had to user port 465:

Code
-------------------

Sub TestingGmail()

Dim iMsg As Object, iConf As Object

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1 ' CDO Source Defaults.
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "(e-mail address removed)"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "(gmailusers password)"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
' .Item("http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress") = "(e-mail address removed)"
.Update
End With

With iMsg
Set .Configuration = iConf
.To = "(e-mail address removed)"
.CC = ""
.BCC = ""
.ReplyTo = "(e-mail address removed)"
.From = """replytoname"" ([email protected])" ' Actual code has '<' and '>', not '(' & ')'
.Subject = "Title"
.TextBody = "Text body"
.Send
End With

Set iMsg = Nothing
Set iConf = Nothing
End Sub

-------------------

(Note that the email address in the .From line is really subtended b
'<' and '>' and not normal brackets - I had to change them as the Foru
thinks they are html tags and removed the text in between as it isn'
recognised.)

The resulting email has the name given in the .From paramete
("replytoname" here) but the associated email address is the Gmai
address given in the CDO parm 'sendusername'. i.e
'(e-mail address removed)' is totally ignored everywhere. Same if it'
set in the senduserreplyemailaddress in the CDO parms (another potentia
solution to this that doesn't work). A 'Reply' to this email will go t
the Gmail address, not to the sender's address. NOT what I want!

I've googled this five ways from Sunday with no solution found and I'
out of ideas. Is there something in my PC's setup that might b
preventing this working? I have Windows 7 Home and Office 2007.

I use Windows Live for my normal email client (Outlook is no
installed), but I'm writing this application for a friend who only use
Hotmail and also has no Outlook. He runs W7 and Office 2010. He may o
may not have Windows Live installed but in any case he does not use i
and it will have no accounts defined. I'm using my own Gmail accoun
(which I only really use on my iPad) as the remote server for all thi
but if it doesn't work for me is it ever going to work for him? I nee
that return address to be his Hotmail address as he *will* be expectin
replies. In this testing phase I'm using my own O2 account in its place
not that it matters a jot what that address is as it's ignored anyway.

Help. Please.
 
B

Bill22

Some of the code got lost in translation. Here it is again, properly i
full:

Code
-------------------
Sub TestingGmail()

Dim iMsg As Object, iConf As Object

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1 ' CDO Source Defaults.
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "(e-mail address removed)"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "(gmailusers password)"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
' .Item("http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress") = "(e-mail address removed)"
.Update
End With

On Error Resume Next
With iMsg
Set .Configuration = iConf
.To = "(e-mail address removed)"
.CC = ""
.BCC = ""
.ReplyTo = "(e-mail address removed)"
.From = """replytoname"" <[email protected]>"
.Subject = "Title"
.TextBody = "Text body"
.Send
End With

On Error GoTo 0

Set iMsg = Nothing
Set iConf = Nothing
End Su
-------------------

Managed to switch off the parsing so it all comes out right now. Sorr
'bout that - must have been a bit confusing with bits missing.

Bil
 
B

Bill22

OK. Fixed it. I have to admit to a certain amount of redness in th
facial area.

I didn't go far enough in the testing of the received email. Th
displayed heading in my Windows Mail Inbox of was of the for
"replytoname ([email protected])" and in my innocence I believed tha
email address to be the Reply To address. I never actually tried to d
a reply to it. When I did that today, more out of frustration tha
serious testing, lo and behold it provided the correct email address i
the To field. i.e. (e-mail address removed) from my code above.

Case closed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top