PDA

View Full Version : Remote SMTP server with VBScript


CobolExpert
04-05-05, 11:23
I am trying to get my script to e-mail me an error code and description if it errors out. I have an exchange mail server in house so I figured I would just use the following code to send myself an e-mail. I tried running the code by itself and get nothing... no e-mail, nothing in the bad mail folder, and nothing in message tracking to or from the addresses I listed. I am getting no scripting error messages and my port is the default (25). Any ideas?? :confused:


Dim objMessage

'Sending a text email using a remote server

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.Sender = "me@myaddress.com"
objMessage.To = "me@myaddress.com"
objMessage.TextBody = Err.number & " - " & Err.Description

'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.

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") = "myserver.mydomain"

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

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send


Thanks!!



I found some old posts back in 2003 discussing this but nothing newer so please excuse me if I missed a post that already explained this stuff..

Kodo
04-05-05, 11:47
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "myserver.mydomain"

Marked in red.. where is your mailserver IP address or host name?

CobolExpert
04-05-05, 11:52
I took it out, didn't really feel the need to provide everyone with my internal mail server name and domain and/or ip address. ;)

I've tried it with just the server name and the FQDN but both gave me the same result (nadda).

Kodo
04-05-05, 11:56
perhaps your email server is blocking remote smtp?
check your event viewer on your ex server to see if there were any errors?

CobolExpert
04-05-05, 13:52
Ya, nothing in the event viewer.

I know I have locked down the server pretty tight but I thought I was allowing internal clients to relay. Maybe not... If nothings wrong in the script, it must be an exchange thing.

CobolExpert
04-06-05, 09:38
I am allowing my internal range to relay... hmmm... any ideas on what it could be? If the script runs with no errors (just as I pasted below but with my information entered in) but the exchange server acts like it never gets it... then.. hmmm... how interesting.

Kodo
04-06-05, 09:46
maybe you need to authenticate

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

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.Sender = """Me"" <me@my.com>"
objMessage.To = "test@test.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") = "mail.your.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") = "youruserid"

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

'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") = False

'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

CobolExpert
04-07-05, 09:57
Found this link - http://www.slipstick.com/files/Q324037.doc

I followed that and used one of the codes they had listed (shown below) and it works fine. All I did was put in my server name and the to and from address. Wuz up wit dat... I'm not a big coder so I can't really see a difference between the two other then how they are coding it (the second code has two sets ahead of time - message and config while the original code had one set for the message and then used the longer strings down below for the config lines... Does anyone else see anything that would make a difference? It doesn't really matter for me now that its working but I was suprised since the code I was using was so readily available (on here and other sites).


Dim iMsg
Dim iConf
Dim Flds

Const cdoSendUsingPort = 2
Const strSmartHost = "MyServerName"

'Create the message object
Set iMsg = CreateObject("CDO.Message")

'Create the configuration object
Set iConf = iMsg.Configuration

'Set the fields of the configuration object to send using SMTP via port 25.
With iConf.Fields
.item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
.item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSmartHost
.Update
End With

'Set the message to,from,subject,body properties.

With iMsg
.To = "me@myaddress.com"
.From = "me@myaddress.com"
.Subject = "Test message using CDOEx and cdoSendUsingPort, sent on: " & now()
.TextBody = "This is a test using CDOEx"
.Send
End With

set iMsg = Nothing

Kodo
04-07-05, 10:05
I don't know why that would make a difference but VB can be funky that way sometimes.

CobolExpert
04-07-05, 10:10
FYI for anyone else who can't get the original code to work. I added this to the new code so it has the same functionality. You'll of course have to define the variables.


With iConf.Fields
'Send mail by using SMTP over the network
.item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
'Use predefined mail host to send mail to
.item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = sMailHost
'Do not use SSL for the connection
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
'Use predefined mail port to connect to
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = iMailPort
'Use a connection timeout of 60 seconds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
'Connect to mail server anonymously
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous
'Username to connect to mail server with (if authentication is not anonymous)
'.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "DOMAIN\USERNAME"
'Password to connect to mail server with (if authentication is not anonymous)
'.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "PASSWORD"

.Update
End With


Thanks for the help Kodo

Kodo
04-07-05, 10:19
You're welcome. Sorry I couldn't come up with a resolution but Hey, I learned something out of it too!