Send Email from a VB script with no email client installed and not using CDO, using SMTP

What I'm trying to do is send email from a VB script (from a web page on our internal network, for example). I'm evaluating some software from called ActiveEmail SMTP Email Toolkit 2.1. It's basically a control that handles the script and uses our SMTP server to send email. Everything works with this control installed but there are 2 problems.

1) It's not free. That control costs money and while it doesn't cost much, I'm absolutely positive that the same thing can be done for free in VB with the right script.

2) You have to install software to make it work. This is the bigger of the 2 problems. I'm on a corporate network with several hundred PC's and don't really want yet another piece of software on my computers or images. Also, every machine in the facility would have to have this software installed before we could go live with an email script and with as little time as we have, that could take forever.

I work at the largest satellite plant in a large manufacturing company so we have our own (quite nice dual P4 xeon with 2 GB of RAM) Exchange 5.5 server (upgrading to Exchange 2003 this FY) but we don't have control of the way the servers are configured, set up etc. We can make local changes but anything that would have to be changed at a corporate level is probably out.

Basically, I'm looking for some VB code that will emulate everything that control does, as it works just like I want it to and it also works from machines that have never had an email client installed.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi there,
i had the same "problem"  as you had, and come up with the following, --> there is a freeware tool called sendmail (downloadable from you can use that in a batch or vbscript to send you mail
The only requirement is that you need to specify a relay host to send the mail.

hope it helps

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tomwillifordAuthor Commented:
Well I have a relay host that works with that utility, and you don't have to install anything... If nothing better pans out then I COULD use this but I'd really like to stick to a script. That still requires the distribution of software and a clunky solution (write the message to a text file and then send from the text file)

the solution is quite simple, at least in VB.

You should create a winsock object with something like

Set Winsock = server.createobject ("winsock") ' I'm not sure about this, you should check
Then, you could do like this:

Winsock.localport = 0
Winsock.remotehost = [smpt host ip] ' here you can place any mail server address where you can communicate
Winsock.remoteport = 25 ' this is the default SMTP port

on error resume next
Winsock.close ' just in case it is open for any reason

You could implement a wait where you do a winsock.GetData ReadData, vbString
Then verify that ReadData is 220. (means OK)

Winsock.SendData ("HELO [whatever name you want send]" & vbcrlf) ' the name is not required by most systems
Now you should wait for some time in order to be sure the server received the data
In this case 250 means OK

Winsock.SendData ("mail from: [whatever mail address you want]" & vbcrlf) ' here you can place even a fake e-mail address.
Wait for a  250 (means OK)

Winsock.SendData ("rcpt to: [a real mail address]" & vbcrlf) ' this is the to, cc, bcc mail address (see below)
you have to send this line for each recipient.
Wait for a  250 (means OK)

now we sill send the real data
Winsock.SendData ("data" & vbcrlf) ' write as it is
Wait for 354 (means ready to get data)

here you could send watever you want, but with the following header:

From: [sender e-mail address]
Date: [see below date format]
x -Mailer: [whatever name you want to give. You app name could be used]
To: [to recipient mail address]
CC: [cc recipient mail address]
BCC: [bcc recipient mail address]
Subject: [inser a subject]
[write here your text]

end the whole message with a
vbcrlf & "." & vbcrlf

The date and time should be formatted as follow:
Format(Date, "Ddd") & ", " & Format(Date, "dd Mmm YYYY") & " " & Format(Time, "hh:mm:ss") & "" & " +0100"

So your message has been sent.

winsock.close will close the connection

Hope this help,


Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Richie_SimonettiIT OperationsCommented:
Use this FREE component instead:
tomwillifordAuthor Commented:

What you posted looks like it's going down the right path, but (there's always a but) between my poor VB skills and your VB shorthand it isn't working. If you had a sample of some actual code that works I'd be willing to call that the solution. I'v tried to get it to work but I have so much commented now there is more comment than code and it's still not working. Help?


That requires additional files, I was trying to avoid that. Thanks for the link, though.
In order to create the object, could you please try this three methods:

Set Winsock1 = Server.createobject("MSWinSock.WinSock")

Set Winsock1 = Server.CreateObject("MSWinsock.Winsock", "wsock_");

Set Winsock1 = Server.CreateObject("MSWinsock.Winsock.1")

One of this three should work.

After the code is the one I already sent you, but if you want an example, here it is:

' The following is a function that Wait for a return code or for a certain amount of time based on a loop (not real time)
' even if the return is not the right one, it is ok. Sometime you loose the message back.
' anyway the important is to wait
Public Function Wait(TimeWait As Integer) As String
Dim x As Long
preverr = Err
preverrdesc = Err.Description
On Error Resume Next
For x = 0 To TimeWait * 20
    Winsock1.GetData ReadData, vbString
    If Val(Left(ReadData, 3)) = TimeWait Then
        Exit For
    End If
Err = preverr
Err.Description = preverrdesc
Wait = ReadData

End Function

Sub Main ()
' create the socket object (i.e. the following, try th others if this do not work)
Set Winsock1 = Server.CreateObject("MSWinsock.Winsock.1")

Winsock1.localport = 0
Winsock1.remotehost = "MYSMTPMAILSERVER" ' here you can place any mail server address where you can communicate (to use IP, user remotehostIP)
Winsock1.remoteport = 25 ' this is the default SMTP port

on error resume next
Winsock1.close ' just in case it is open for any reason

Wait (220)

Winsock1.SendData ("HELO MaxPol" & vbcrlf) ' the name is not required by most systems
Now you should wait for some time in order to be sure the server received the data
Wait (250)
Winsock1.SendData ("mail from:" & vbcrlf) ' here you can place even a fake e-mail address.
Wait (250)
Winsock1.SendData ("rcpt to:" & vbcrlf) ' this is the to, cc, bcc mail address (see below)
'you have to send this line for each recipient.
Wait (250)
'now we sill send the real data
Winsock1.SendData ("data" & vbcrlf) ' write as it is
'here you could send watever you want, but with the following header:
Dim DataText as string
Dim DateTimeF as string
DateTimeF=Format(Date, "Ddd") & ", " & Format(Date, "dd Mmm YYYY") & " " & Format(Time, "hh:mm:ss") & "" & " +0100"

DataText="From: MaxPol" & vbcrlf & "Date: " & DateTimeF & vbcrlf
DataText=DataText & "x -Mailer: MaxPol" & vbcrlf
DataText=DataText & "To: TomWilliFord" & vbcrlf
DataText=DataText & "CC: MaxPol" & vbcrlf
DataText=DataText & "BCC: MaxPol" & vbcrlf
DataText=DataText & "Subject: Auto send mail" & vbcrlf
DataText=DataText & "Hi, this message has been sent to you by MaxPol & TomWilliford." & vbcrlf & vbcrlf
DataText=DataText & "Have a nice day." & vbcrlf
DataText=DataText & vbcrlf & "." & vbcrlf

Winsock1.sedndata DataText

' and your mail is sent!!!

End Sub


Richie_SimonettiIT OperationsCommented:
it doesn't requires aditional files (anyway, a third party (ActiveEmail SMTP ) is aditional files too)
Just add the code in your own app. No use dll at all.
tomwillifordAuthor Commented:
Just wanted to explain my points "logic". ToolMan's solution is the one I'm going to use. I can distribute the file needed easily enough and I have the VB skills needed to use that. I would have preferred to use MaxPol's solution (as it met both of my requirements) but I lack the needed VB knowlege to effectly implement/support code of that complexity. I'm new to VB and still learning and don't have the time needed right now to do it right by learning everything in the code. Since this needs doing before Turkey Day, I have to make some progress... I appreciate everyone's help on this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.