View Full Version : Converting Code from VBScript to Javascript

09-23-2011, 07:24 PM

I am new javascript and I need your help converting the following code from VBScript to Javascript please.

<script language="vbscript">

function CDO_Mail_Small_Text_2()

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myemailadress@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
End With

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

With iMsg
Set .Configuration = iConf
.To = "jason.kelly@tc.gc.ca"
.CC = ""
.BCC = ""

'.ReplyTo = "Reply@something.nl"
.From = """ARealName"" <ARealName@gmail.com>"
.Subject = "Important message"
.TextBody = strbody
End With

end function


Much thanks and appreciation for all your help and support.



09-23-2011, 07:27 PM
I assume you would like to use javascript to access this script and think that you need to convert this to javascript to make that happen. Well I use a similar email VBscript provided by my hosting company and I can tell you right now that I was able to access it through my javascript code...

09-23-2011, 07:32 PM
Vbscript/asp = Server side language

javascript = Client side language

You cannot convert this code directly into javascript as javascript cannot accomplish this functionality.

You can, however, use an Ajax call to submit the desired data to an asp page in the background with your current code.

09-23-2011, 08:03 PM
Vbscript/asp = Server side language

javascript = Client side language
I use javascript in my asp pages exclusively unless incorporating VBscripts like above. It's a little different because it can access server technologies, but if transitioning from dynamic HTML to serverside Apps (like I did), it's the best way because you don't have to learn a new language for your logic operations.

<%Respons.Write('Try this one on for size')%>
Although I do realize the error of my post. I assumed jason_kelly was using serverside js, but maybe he wants to call this function from a clientside js enhanced page... in which case your right DanInMa, AJAX would be a good solution. @jason_kelly if you are just sending an email (as the script suggests) you just need to send the variables to the vbscript via url variables. You can use AJAX, or any linking method to send them <a href="www.yoursite.com/mailer.asp?name=johnsmith"></a>
and then add this handy dandy javascript to the mailer to recieve them

//...VBscript after that

add a few Regular Expressions for security and your good to go.
@DanInMa I just said in my post that I already do that...

Response.Expires = -1


Function CheckEmail(strTemp)
Dim regEx2
Set regEx2 = New RegExp
regEx2.Pattern = "/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i"
regEx2.Global = True
CheckEmail = regEx2.Replace(strTemp, "")
End Function

Function RemoveBadCharacters(strTemp)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = "[^A-Za-z0-9_ ]"
regEx.Global = True
RemoveBadCharacters = regEx.Replace(strTemp, "")
End Function

Function VBSendMail(subject,name,male,phone,comment)

body="NAME:"+name+"<br /><br />E-MAIL:"+male+"<br /><br />PHONE NUMBER:"+phone+"<br /><br />COMMENT:"+comment


Set objConfig=CreateObject("CDO.Configuration")
objConfig.Fields.Item(sendUrl)=2 ' cdoSendUsingPort

Set objMail=CreateObject("CDO.Message")
Set objMail.Configuration=objConfig
End Function
name= RemoveBadCharacters(Request.Form("name"));
male= CheckEmail(Request.Form("male"));
subject= RemoveBadCharacters(Request.Form("subject"));
phone= RemoveBadCharacters(Request.Form("phone"));
comment= RemoveBadCharacters(Request.Form("comment"));

Mind you in this example my javiscript was sparse, but I was feeling lazy and didn't want to learn the VBscript syntax for that... (Still haven't :o )

09-23-2011, 08:47 PM
that is not javascript code. just becuase you put <%@LANGUAGE="JavaScript"%> at the top doesnt magically make it javascript.

09-23-2011, 08:50 PM
but it is serverside javascript syntax. Look it up, I've been using it for years, built several apps, it's a real thing, and not the least bit VBScript. The language is defined by the syntax. I learned how to do it on MSDN.

09-23-2011, 08:54 PM

09-23-2011, 09:04 PM
You did get the part where I said I was wrong, and where we helped the poster with the relevant information didn't you? Sorry if I ruined your day with my ignorance...

Old Pedant
09-23-2011, 09:23 PM
I strongly suspect that Jason Kelly thinks that he will be able to use JavaScript to send email from a browser, instead of using ASP. He will be sadly disappointed.

However, here is his ASP VBScript code converted to ASP JScript code:

<%@ language="javascript" %>
function CDO_Mail_Small_Text_2()
iMsg = CreateObject("CDO.Message");
iConf = CreateObject("CDO.Configuration");

iConf.Load( -1 ) // CDO Source Defaults...I think this is unneeded
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true;
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1;
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myemailadress@gmail.com";
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword";
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com";

iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;
iConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465;
iConf.Fields.Update(); // notice that this MUST be a function call!

strbody = "Hi there\n\n"
+ "This is line 1\n\n"
+ "This is line 2\n\n"
+ "This is line 3\n\n"
+ "This is line 4";

iMsg.Configuration = iConf;
iMsg.To = "jason.kelly@tc.gc.ca";
iMsg.CC = "";
iMsg.BCC = "";

// iMsg.ReplyTo = "Reply@something.nl";
iMsg.From = '"ARealName" <ARealName@gmail.com>';
iMsg.Subject = "Important message";
iMsg.TextBody = strbody;
iMsg.Send(); // notice that this MUST be a function call!


09-23-2011, 09:25 PM
DanInMa is right - even though it says "JAVASCRIPT", it's actually microsoft's implementation of javascript called jscript which is different from client-side javascript. Javascript was originally designed as a client-side language. MS ripped it off, put in a bunch of proprietary stuff & got sued so they had to rename it "jscript".

09-23-2011, 09:29 PM
I'm totally going to use that, thanx!

BTW @Old Pedant I was kinda hoping you might weigh in on my issue also :o ... http://www.codingforums.com/showthread.php?t=238933

@Raphael I already admitted the err of my ways in my second post and my last post... do we really need to waste anymore time on it? It seems more of a tomato, tamahto kind of issue anyway...

09-23-2011, 09:34 PM
Just for the record, jason_kelly could have made the same distinction that I did and assumed that he needed to convert the vbscript to jscript, in which case my original post might have actually made more sense than DanInMa's (probably not, but I can dream...)

09-23-2011, 09:37 PM
blaze4218, I apologize. I hadn't refreshed the page & I didn't see your posts. Sorry! :)

09-23-2011, 09:41 PM
truce :)

09-24-2011, 04:08 PM
A Great Big Thanks to everyone for everyones help.

Such great help and support on a great forum is always hard to find.

I am glad I found the experts ;)

Cheers and have a great weekend all.