Sending e-mail using smtp

Hi All

Is it possible to send an e-mail using SMTP without using the blat freeware?

Following is my sample code, but it fails at the point where I say .send. The error message that I get is: Transport failed to connect to the server. Can anyone tell me what this means or how to fix it?

Thanks
Lori

' Send by connecting to port 25 of the SMTP server.
Dim iMsg
Dim iConf
Dim Flds
Dim strHTML

Const cdoSendUsingPort = 2

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

Set Flds = iConf.Fields

' Set the CDOSYS configuration fields to use port 25 on the SMTP server.

With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
    'ToDo: Enter name or IP address of remote SMTP server.  EG, IP address below:
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "<172.16.236.36>"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
    .Update
End With

' Build HTML for message body.
strHTML = "<HTML>"
strHTML = strHTML &amp; "<HEAD>"
strHTML = strHTML &amp; "<BODY>"
strHTML = strHTML &amp; "<b> This is the test HTML message body</b></br>"
strHTML = strHTML &amp; "</BODY>"
strHTML = strHTML &amp; "</HTML>"

' Apply the settings to the message.
With iMsg
    Set .Configuration = iConf
    .To = "bachmalm" 'ToDo: Enter a valid email address.
    .From = "bachmalm" 'ToDo: Enter a valid email address.
    .Subject = "This is a test CDOSYS message (Sent via Port 25)"
   ' .HTMLBody = strHTML
    .Send
End With

' Clean up variables.
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

MsgBox "Mail Sent!"

bachman :us: (BOB member since 2002-09-24)

Just wondered if you have SMTP active on your server?
I remember having to do this in IIS using the Internet Services Manager before we could ever do emails. It wasn’t there by default.


scott copeland (BOB member since 2002-08-15)

Hi Scott

What does that mean? How do I find that out?

Thanks
Lori


bachman :us: (BOB member since 2002-09-24)

[quote=“bachman”]

Umm - if you are using IIS, see previous answer - using the Internet Services Manager to check this. If not, I’m sure there are other posts on the subject of SMTP. That’s how we figured it out. I really don’t remember all the details of it. I gave up BCA (emailing reports) at the last license renewal as this stafff of 1.5 did not have time to support BCA :cry:


scott copeland (BOB member since 2002-08-15)

What is Internet Services Manager and how do I access it?

Thanks
Lori


bachman :us: (BOB member since 2002-09-24)

sorry - go to your server, start menu, programs. adminstrative tools, and then Internet Services Manager. You should see a whole bunch of crap you can probably delete. You should see the short name of your server, in my case bsuo. Open that and you see Default Web site and wi is under that. Right-clcik for properties. What you want to see started is Default SMTP.
Now this is IIS 5, IIS 4 was a bit different.


scott copeland (BOB member since 2002-08-15)

Scott

I am trying to run this macro from my pc. I want to get it working on my pc first then I will try it on the server and BCA next.

All I am doing is running a report and then trying to send an e-mail from VBA using SMTP. Am I not doing this correctly? Why would I need to check the server if I trying to run from my pc?

Thanks
Lori


bachman :us: (BOB member since 2002-09-24)

this is a bit beyond me really. I asked because I assumed that SMTP would be running on the webi server.

Transport failed to connect to the server

sorry if I sent you down wrong track but from the error, it sounded like your SMTP server was not set up


scott copeland (BOB member since 2002-08-15)

Should that be 25 instead of 2? The standard SMTP port is 25, as referenced in your comments in your code. Yet you are defining your port as port 2. Could it be that simple? :slight_smile: Hope so… I’ve never tried this, but that jumped out at me this morning.

Dave


Dave Rathbun :us: (BOB member since 2002-06-06)

Hi Dave

I saw that too but if I change it to 25 then the program fails earlier. It fails at the .update instead of at the .send.


bachman :us: (BOB member since 2002-09-24)

The first thing you need to do is confirm that your SMTP service is functioning correctly…

Step 1 In a command prompt window on the PC/SERVER WHERE THE CODE WILL BE EXECUTED, enter

telnet <server name> 25

, where is the name of the remote SMTP server.

Step 2 If there is a response from the remote Exchange server, enter

ehlo

. The remote SMTP server should respond with a list of SMTP commands like this:


220 gs Microsoft ESMTP MAIL Service, Version: 5.0.2195.4905 ready at  Thu, 6 Feb
 2003 14:25:37 +1100
250-gs Hello [192.168.2.63]
250-AUTH GSSAPI NTLM LOGIN
250-AUTH=LOGIN
250-TURN
250-ATRN
250-SIZE 2097152
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250 OK

Step 3 If the telnet test was successful, enter quit to end the telnet session.

If you got no response from the SMTP server by carrying out the steps above, then there is something wrong with your SMTP server service, or you’re trying to bind to the wrong port etc…

If you got a response from the SMTP server by carrying out the steps above, your problem more than likely lies in your code…

Perform the test and let us know how you go…


GraemeSmith :switzerland: (BOB member since 2002-08-16)

I’ve sucessfully implemented remote SMTP in a VBA macro running on the BCA server.

I too had this very issue of transport failure when running the code from my workstation, but I suspected that it was simply a security/firewall rule blocking the transport. We had internal firewalls.

Anyway, we had a dev and production network, so I would write the code at my pc and then test it on the Dev BCA server.

If you have a dev environment, you may want to test your code there. Also, when I get home tonight, I will post the code that I used. Like I said it consistently worked in both Dev and Production.

Rosalind Beasley
rosalind.beasley@westbaysolutions.com
Westbay Solutions Group


Rosalind Beasley (BOB member since 2002-09-11)

May be this could help
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnduwon/html/d5smtp.asp
Link that I use to set up my iis smtp
and look at the “Testing with the Pickup directory”


Dan (BOB member since 2002-08-18)

Can you explain to me what this code means?
a namespace prefix (such as “http://schemas.microsoft.com/cdo/configuration/”)

What do I need to change this to in order for this to work in my environment?

How did you overcome your firewall issues?

We are in the process of setting up a testing environment.

Thanks
Lori


bachman :us: (BOB member since 2002-09-24)

Lori,

I just ran this code a few minutes ago and it worked like a charm.

If you want, I can send you my smtp server name so that you can test this code from your PC against my SMTP server on the web. You will need to change the values for the sReplyToAddress, sFromAddress,sAttachment, and sSMTPServer variables. This will confirm whether or not it is a firewall or SMTP configuration issue or not. I suspect it is, since my code is not much different than yours.

regards,
Rosalind

Sub RemoteSMTPSend()
'~’ Function Name: RemoteSMTPSend()
’ Purpose: Send Email with attachment via remote SMTP Server
’ Returns: N/A
’ Author: Rosalind Beasley
’ Created: 11/28/2001
’ Update History:
'
'Reference to ADO2.5 Library
'Late Binding to CDO 2000 Library

'Send the message using the network (SMTP over the network).
Const cdoSendUsingPort = 2
'2= send using network, 1= send using local smtp server

’ The following are the configuration enum values
Const cdoSendUsingMethod = “http://schemas.microsoft.com/cdo/configuration/sendusing
Const cdoSMTPServer = “http://schemas.microsoft.com/cdo/configuration/smtpserver
Const cdoSMTPServerPort = “http://schemas.microsoft.com/cdo/configuration/smtpserverport
Const cdoSMTPConnectionTimeout = “http://schemas.microsoft.com/cdo/configuration/SMTPConnectionTimeout
Const cdoSMTPAccountName = “http://schemas.microsoft.com/cdo/configuration/SMTPAccountName
Const cdoSendUserReplyEmailAddress = “http://schemas.microsoft.com/cdo/configuration/SendUserReplyEmailAddress

Dim i As Integer
Dim sHtmlBody As String
Dim iConf As Object
Dim iMsg As Object
Dim flds As ADODB.Fields
Dim sReplyToAddress As String ’ Reply to address used in Mail Message
Dim sFromAddress As String ’ From Address used in Mail Message
Dim sToAddress As String ’ To Address used in Mail Message
Dim sSubject As String ’ Subject of the Mail Message
Dim sAttachment As String ’ Path and File Name of Attachment file
Dim sSmtpServer As String ’ SMTP Server Host Name
Dim iPortNumber As Integer ’ Port Number used by SMTP Server
Dim iTimeOut As Integer ’ Timeout for SMTP server transport
Dim bAppInteractive As Boolean ’ Aplication.Interactive value
Dim bAppBreakOnVbaError As Boolean ’ Application.BreakONVBAError value
Dim serrorlog As String ’ Name of Process Error Log File
Dim semaillog As String ’ Name of Email Process Log

On Error GoTo Error_Handler

sAccountName = “BCA”
sReplyToAddress = “user@server.com
sFromAddress = “user@server.com
sToAddress = “user@server.com
sSubject = “CDO for Windows 2000”
sAttachment = “c:\Attachment.txt”
'— Email Server Settings —
sSmtpServer = “mail.server.com
iPortNumber = 25
iTimeOut = 30

'— VBA Debug Settings —

bAppInteractive = False
bBreakOnVBAError = False

'— File Location Settings —

serrorlog = “c:\log\smtperror.log”
semaillog = “c:\log\smtpemail.log”

'Make sure the application does not enter any interactive mode
'Application.BreakOnVBAError = bAppBreakOnVbaError
'Application.Interactive = bAppInteractive

'Late binding to the CDO Library
Set iConf = CreateObject(“CDO.Configuration”)
Set iMsg = CreateObject(“CDO.Message”)

sHtmlBody = " "
sHtmlBody = sHtmlBody & " "
sHtmlBody = sHtmlBody & " "
sHtmlBody = sHtmlBody & " "
sHtmlBody = sHtmlBody & " "
sHtmlBody = sHtmlBody & " "
sHtmlBody = sHtmlBody & "

This is the body of the message
"
sHtmlBody = sHtmlBody & " "
sHtmlBody = sHtmlBody & " "

Set flds = iConf.Fields

'set the configuration for Network Send
With flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = sSmtpServer
.Item(cdoSMTPServerPort) = iPortNumber
.Item(cdoSMTPConnectionTimeout) = iTimeOut
.Item(cdoSMTPAccountName) = sAccountName
.Item(cdoSendUserReplyEmailAddress) = sReplyToAddress
.Update
End With

'Send email

Set iMsg = CreateObject("CDO.Message")
With iMsg
    .Configuration = iConf
    .from = sFromAddress
    .To = sToAddress
    .Subject = sSubject
    .HTMLBody = sHtmlBody
    .AddAttachment sAttachment
    .ReplyTo = sReplyToAddress
    .Send
End With
Set iMsg = Nothing

GoTo CleanUp

Error_Handler:
'write errors to logfile DFR.log
Open serrorlog For Append As #1
Print #1, Err.Number; Err.Description, Date, Time
Close #1

CleanUp:
'cleanup
Set flds = Nothing
Set iMsg = Nothing
Set iConf = Nothing
Exit Sub

End Sub


Rosalind Beasley (BOB member since 2002-09-11)

I forgot to mention that the code in the previous post requires a reference to ADO 2.5.

See previous post for Remote SMTP code. (CDO for windows 2000)

regards,
Rosalind Beasley
rosalind.beasley@westbaysolutions.com
Westbay Solutions Group


Rosalind Beasley (BOB member since 2002-09-11)