Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending an e-mail through exchange without SMTP or Outlook

Status
Not open for further replies.

frumpus

Programmer
Aug 1, 2005
113
US
I know there are a lot of examples available for sending e-mail from vbscript but they all seem to involve using an SMTP relay or creating an Outlook.Application object.

I can't use either of these methods. I simply want to us a CDO.Message object with an Exchange server on my domain.

I have a book that makes this sound like the simplest thing in the world, but it doesn't seem to contain all the code needed to connect to the Exchange server, so I get an error.

This is the example code I am trying to get working.

Code:
Set objEmail = CreateObject("CDO.Message")

With objEmail
 .From = "myemail@mydomain.com"
 .To = "myemail@mydomain.com"
 .Subject = "Automagical Email"
 .Textbody = "You have received email from a mystical wizard! You gain 37 exp. You find: soiled loincloth"
 .Send
End With

Set objEmail = Nothing

After several seconds pass, the .Send line generates this error.

The transport failed to connect to the server.
Code: 80040213
Source: CDO.Message.1

Any clues as to my missing code would be appreciated.
 
Here try this


Send Email without Installing the SMTP Service

Description
Demonstration script that uses CDO to send email from a computer where the SMTP Service has not been installed. Replace the name smarthost with the name of your SMTP server.

Set objEmail = CreateObject("CDO.Message")

objEmail.From = "admin1@fabrikam.com"
objEmail.To = "admin2@fabrikam.com"
objEmail.Subject = "Server down"
objEmail.Textbody = "Server1 is no longer accessible over the network."
objEmail.Configuration.Fields.Item _
(" = 2
objEmail.Configuration.Fields.Item _
(" = _
"smarthost"
objEmail.Configuration.Fields.Item _
(" = 25
objEmail.Configuration.Fields.Update
objEmail.Send
 
I had already tried that. It still requires an SMTP server so no go.

I have made some progress, setting config to 3 instead of 2 seems to configure it to use exchange and not SMTP, but this requires that I provide it with a mailbox URL for the senders box and I'm having some trouble getting that URL correct. This is the code I am working with currently.

Code:
Set objEmail = CreateObject("CDO.Message")
Set Config = CreateObject("CDO.Configuration")
Set Config = objEmail.Configuration

sMailboxURL = "file://server/first storage group/mailbox store/user/"

Config("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 3
Config("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/mailboxurl")[/URL] = sMailboxURL
Config.Fields.Update

With objEmail
 .From = "user@mydomain.com"
 .To = "recip@mydomain.com"
 .Subject = "Automagical Email"
 .Textbody = "You have received email from a mystical wizard! You gain 37 exp. You find: soiled loincloth"
 .Send
End With

Set objEmail = Nothing
Set COnfig = Nothing

The error I get with this is:

Object or data matchine the name, range, or selection criteria was not found within the scope of this operation
Code 80040E19

The mailbox URL is supposed to accept 'file://' or ' formats. When I try using http I get an error that says the class is not defined. Either way, the error is generated by the .Send line.

For reference, here is the code sample from msdn2 that I am working from.

Code:
Sub SendMailUsingExchange(sMailboxURL, sTo, sFrom, sSubject, sText)
	 ' Assuming type information has been imported into the script host.
    	 ' For example, <reference object="CDO.Message"/> in .wsf script file.

	 Dim Msg  		' As CDO.Message
	 Dim Config		' As CDO.Configuration

	 ' Create message and configuration objects.
    Set Msg = CreateObject("CDO.Message")
	 Set Config = CreateObject("CDO.Configuration")

	 ' Get the message configuration object.
    Set Config = Msg.Configuration

	 ' Set the sendusing field to 'cdoSendUsingExchange'.
    Config("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 3

	 ' Set the mailboxurl field to the specified mailbox URL.
    Config("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/mailboxurl")[/URL] = sMailboxURL

	 ' Update the fields.
    Config.Fields.Update

	 ' Set the To, From, Subject, and TextBody fields on the message.
    Msg.To = sTo
    Msg.From = sFrom
    Msg.Subject = sSubject
    Msg.TextBody = sText

	 ' Send the message.
    Msg.Send

	 wscript.echo "Calling SendMailWithConnection is successful!"

	 ' Clean up.
	 Set Msg = Nothing
	 Set Config = Nothing
End Sub
 
Also, regarding sMailboxURL, msdn says:

"mailboxurl The URL to the user's Exchange mailbox. This URL can be in either the File: or HTTP: URL scheme format. If this property is not set, CDO looks up the user's mailbox URL using Microsoft Active Directory® and the user principal name (UPN) in the sendusername property."

but in reality it can't seem to look it up on its own.

the example it gives is:

sMailboxURL = "file://./backofficestorage/subdomain.example.com/mbx/user/
 
Here's a generic sub that I have used over and over again. Simply put in the name of your exchange server, the sender, the recipient, and the message. This was cobbled together from several sources including MS.

It has optional support for plain text or HTML messages, and adding attachments.

It will not do a DNS lookup for an MX record and send wherever.

Code:
[green]'==========================================================================
'
' COMMENT: Generic subroutine that sends mail to an external SMTP server.
' Message can be HTML or Plain Text.
'
' SOURCES:
' [URL unfurl="true"]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_cdo_configuration_coclass.asp[/URL]
' [URL unfurl="true"]http://support.microsoft.com/default.aspx?scid=kb;en-us;Q286431[/URL]
'==========================================================================
[/green]
Dim strServer, strSender, strRecipient, strSubject, strMessage

strServer = "exchserver.yourdomain.tld"
strSender = "test@test.com" 
strRecipient = "yourname@yourdomain.tld"
strSubject = "Test"
[green]' strMessage = "This is a test."[/green]
strMessage = "<HTML><BODY><b>This is a test.</b></br></BODY></HTML>"

SendEmail strServer, strSender, strRecipient, strSubject, strMessage

Sub SendEmail(server, sndr, rcpt, subj, msg)
	Dim iMsg, iConf, Flds
[green]	' Set the visual basic constants as they do not exist within VBScript.
	' Do not set your smtp server information here.[/green]
	Const cdoSendUsingMethod = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing"[/URL]
	Const cdoSendUsingPort = 2
	Const cdoSMTPServer = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver"[/URL]
[green]	' The following field names are not needed, but can be enabled
' 	Const cdoSMTPServerPort = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport"[/URL]
' 	Const cdoSendEmailAddress = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendemailaddress"[/URL]
' 	Const cdoSendUserReplyEmailAddress = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress"[/URL]
' 	Const cdoSMTPAuthenticate = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"[/URL]
' 	Const cdoBasic = 1
' 	Const cdoSendUserName = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusername"[/URL]
' 	Const cdoSendPassword = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendpassword"[/URL]
' 	Const cdoSMTPConnectionTimeout = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"[/URL]

	' Create the CDO connections.[/green]
	Set iMsg = CreateObject("CDO.Message")
	Set iConf = CreateObject("CDO.Configuration")
	Set Flds = iConf.Fields
	
[green]	' SMTP server configuration.[/green]
	With Flds
		.Item(cdoSendUsingMethod) = cdoSendUsingPort
[green]		' Set the SMTP server address here.[/green]
		.Item(cdoSMTPServer) = server
[green]		' Optional Fields
' 		.Item(cdoSMTPServerPort) = 25
' 		.Item(cdoSendEmailAddress) = """MySelf"" <myself@example.com>"
' 		.Item(cdoSendUserReplyEmailAddress) = """Another"" <another@example.com>"
' 		.Item(cdoSMTPAuthenticate) = cdoBasic
' 		.Item(cdoSendUserName) = "domain\username"
' 		.Item(cdoSendPassword) = "password"
' 		.Item(cdoSMTPConnectionTimeout) = 10[/green]
		.Update
	End With
	
[green]	' Set the message properties.[/green]
	With iMsg
		Set .Configuration = iConf
		.To = rcpt
[green]' 		.CC = rcpt[/green]
		.From = sndr
		.Subject = subj
	End With

	If InStr(UCase(msg), "<HTML>") Then
		iMsg.HTMLBody = msg
	Else
		iMsg.TextBody = msg
	End If
	
[green]	' An attachment can be included.
	'iMsg.AddAttachment Attachment
	
	' Send the message.[/green]
	iMsg.Send
End Sub

I have had issues with host based firewalls that try to block the outbound connection on port 25, so beware.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
That is also using SMTP. I can't do it. Our Exchange server is not set up to relay SMTP and the admins won't budge on that. They don't want people routing spam through us and getting our server blacklisted.
 
I have made some progress, setting config to 3 instead of 2 seems to configure it to use exchange and not SMTP, but this requires that I provide it with a mailbox URL for the senders box and I'm having some trouble getting that URL correct. This is the code I am working with currently.

Looking at MSDN, it's hard to say what the mailbox "URL" is... I would suggest using the "cdoSendUserName" instead and CDO will look up the user in AD.

[URL unfurl="true"]http://msdn2.microsoft.com/en-us/library/ms876415.aspx[/url]

Some other notes that come to mind...

If you want to send mail to the internet, you will have to install IIS or some other 3rd party mailer on the system running the script or relay off another server (Since CDO does not support doing an MX record lookup before sending).

If you are sending only to internal recipients, then the method above should work fine. Exchange automatically accepts mail destined to domains that it owns.

The problem that you may be running into is that the Exchange server is configured to only accept SMTP connections from some upstream Spam filter or front-end server. By configuring the authentication features of CDO you should be able to override this behavior. Or you can ask your mail admins to allow you to send the mail to their Spam Filter so that it can be relayed into the mail system normally.


PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Update:

I got the sMailboxURL ironed out to use the correct path only to discover that McAfee was not allowing the script to run properly. This was a result of pulling the Schema from microsoft's site (meaning wscript.exe was trying to run something out of the temp folder, causing McAfee to block it.)

Basically, the code I posted above works fine, so long as you disable 'Access Protection' in McAfee and have sufficient privileges on the Exchange server. So my problems aren't all solved, but at least I know the code works.
 
Exactly! And in your case " sufficient privileges" should just mean being able to authenticate with "Send As" privileges for the Exchange account.

Look forward to reading your progress.


 
Final report:

I found a much safer, completely different method using our Oracle Alpha server. Turns out we had an SMTP function running on it that allows me to do what I want without disabling any of my anti-virus functionality or wrangling with the Exchange server so I took the easy out and everything is fine.

Thanks all for the replies and I did learn some useful stuff along the way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top