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

Posted on 2003-11-25
Medium Priority
Last Modified: 2013-11-13
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 http://www.activxperts.com/activemail/ 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.
Question by:tomwilliford
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1

Accepted Solution

ToolMan earned 1200 total points
ID: 9817534
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 http://www.dato.at/download/freeware/sendmail.zip) 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

Author Comment

ID: 9817683
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)

Expert Comment

ID: 9818120

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,


Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

LVL 16

Expert Comment

ID: 9818749
Use this FREE component instead:

Author Comment

ID: 9818772

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.

Assisted Solution

MaxPol earned 800 total points
ID: 9819356
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: maxpol@expert-exchange.com" & vbcrlf) ' here you can place even a fake e-mail address.
Wait (250)
Winsock1.SendData ("rcpt to: tomwilliford@somewhere.com" & 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


LVL 16

Expert Comment

ID: 9819435
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.

Author Comment

ID: 9819604
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.

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question