Remote SMTP server with VBScript

Discussion in 'Software' started by CobolExpert, Apr 5, 2005.

  1. CobolExpert

    CobolExpert Private E-2

    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:

    Code:
    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..
     
  2. Kodo

    Kodo SNATCHSQUATCH

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

    CobolExpert Private E-2

    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).
     
  4. Kodo

    Kodo SNATCHSQUATCH

    perhaps your email server is blocking remote smtp?
    check your event viewer on your ex server to see if there were any errors?
     
  5. CobolExpert

    CobolExpert Private E-2

    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.
     
  6. CobolExpert

    CobolExpert Private E-2

    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.
     
  7. Kodo

    Kodo SNATCHSQUATCH

    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
     
  8. CobolExpert

    CobolExpert Private E-2

    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).

    Code:
    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
    
     
  9. Kodo

    Kodo SNATCHSQUATCH

    I don't know why that would make a difference but VB can be funky that way sometimes.
     
  10. CobolExpert

    CobolExpert Private E-2

    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.

    Code:
    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
     
  11. Kodo

    Kodo SNATCHSQUATCH

    You're welcome. Sorry I couldn't come up with a resolution but Hey, I learned something out of it too!
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds