Sending mail through a remote SMTP server

deadseasquirrels

Board Regular
Joined
Dec 30, 2004
Messages
232
I've been trying to send emails through an SMTP server in various ways. I've been able to send email using CDO going directly through an SMTP server on my same local network (not requiring a username and password). But now I am trying to send email from my home with the SMTP server I am using that is hosted by some commercial company. I used the code below, which is sponsored by Paul Sadowski:
Code:
Sub sendMailRemote()
    'Sending a text email using authentication against a  remote SMTP server

    Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
    Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
    Const cdoAnonymous = 0 'Do not authenticate
    Const cdoBasic = 1 'basic (clear-text) authentication
    Const cdoNTLM = 2 'NTLM
    
    Dim objMessage As Object

    Set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = "Example CDO Message"
    objMessage.Sender = """Me"" <me@my.com>"
    objMessage.To = "me@hotmail.com"
    objMessage.TextBody = "This is some sample message text.." & vbCrLf & "It was sent using SMTP authentication."

    '==This section provides the configuration information for the remote SMTP server.

    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

    'Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.myserver.com"

    'Type of authentication, NONE, Basic (Base64 encoded), NTLM
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic

    'Your UserID on the SMTP server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "deadesasquirrels"

    'Your password on the SMTP server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "squirrels"

    'Server port (typically 25)
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

    'Use SSL for the connection (False or True)
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

    'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

    objMessage.Configuration.Fields.Update

    '==End remote SMTP server configuration section==

    objMessage.Send

End Sub

So this code doesn't work. It says "Transport failed to connect to server" which is pretty general. But the thing is I do use the SMTP server from outlook, so port 25 is not blocked, I'm positive I am using the correct username and password (again, because outlook works). Also I know the SMTP server requires authentication...so I'm not sure what else is wrong. If Mr. Sadowski is on this forum maybe you can reply as well. Anybody have any experience with this?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I am trying to get this CDO working at work and I was hoping that someone could tell me how to know what port my Outlook 2003 is working on?
 
Upvote 0
I found a great free remote smtp service at www.bluebottle.com - Works perfectly with Excel/Outlook Express. You get 50 email allowance per day and more if you elect to pay $9 or $25 a year for a service upgrade.
 
Upvote 0
Hi,

I know this thread is over six years old, but I have only just come across it in an attempt at doing exactly what it says on the title. Thanks to staffie007 - I'm using essentially identical code with gmail and it works. (Bottom of page 2.)

However I was wondering if he/she ever got a solution to the question posed - is it possible to set a different reply-to address instead of the sender's address in the From field?

I checked out the cdo configuration parameters on the MS site and lo and behold there is one called "senduserreplyemailaddress". That seemed just the ticket, so I put it in:

"http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress") = "a different email address"

Totally ignored. The email address in the 'sendusername' parameter gets used.

I hope staffie007 is still around and able to say how they got on with this - or if anyone else can help.

I'm running Excel 2007 under Win7 Home.
 
Upvote 0
I found something in the ever-informative Ron de Bruin's downloaded examples (http://www.rondebruin.nl/files/CDO_Example_Code.zip) which gives sample code for using with Gmail. The killer bit is at the end where he says you can use the .ReplyTo parameter before the .Send. Here is his explanation :

' 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 = "Reply@something.nl"

Bingo! (again, I thought). Failure (again). Sorry Ron, doesn't work. Here's my simplified and anonymousised code, which is virtually identical to RdB's code except I 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") = "gmailuser@gmail.com"
        .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") = stUserEmail
        .Update
    End With

    With iMsg
        Set .Configuration = iConf
        .To = "recipient@somedomain.com"
        .CC = ""
        .BCC = ""
        .ReplyTo = "replytoname@anydomain.com"
        .From = """replytoname"" (replytoname@anydomain.com)"        ' 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 are really subtended by '<' and '>' and not normal brackets - I had to change them as the Forum thinks they are html tags and removed the text in between as it isn't recognised.)

The resulting email has the name given in the .From parameter ("replytoname" here) but the associated email address is STILL the Gmail address given in sendusername. i.e. replytoname@anydomain.com is totally ignored everywhere. Same if it's set in the senduserreplyemailaddress parameter earlier. A 'Reply' to this email will go to the Gmail address. NOT what I want!

I'm out of ideas now. Is there something in my PC's setup that might be preventing this working? I use Windows Live for my normal email client (Outlook is not installed), but I'm writing this for a friend who only uses Hotmail and also has no Outlook. He runs W7 and Office 2010. He may or may not have Windows Live installed but in any case he does not use it and it will have no accounts defined. I'm using my own Gmail account (which I only really use on my iPad) as the remote server for all this but if it doesn't work for me is it ever going to work for him? I need that return address to be his Hotmail address as he *will* be expecting 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.
 
Last edited:
Upvote 0
OK. Fixed it. I have to admit to a certain amount of redness in the facial area.

I didn't go far enough in the testing of the received email. The displayed heading in my Windows Mail Inbox of was of the form "replytoname (gmailuser@gmail.com)" and in my innocence I believed that email address to be the Reply To address. I never actually tried to do a reply to it. When I did that today, more out of frustration than serious testing, lo and behold it provided the correct email address in the To field. i.e. replytoname@anydomain.com from my code above.

Case closed.
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,696
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top