1

below is a script to send mail but i want to run the script from context menu "right click" and use file on which i have right clicked as attachment

if i had to run cmd command i can use %1 as right clicked filename but how to do same with this vbscript???

Set objMail = CreateObject("CDO.Message")
Set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields
 
'Set various parameters and properties of CDO object
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing Jump ") = 2 'cdoSendUsingPort
'your smtp server domain or IP address goes here such as smtp.yourdomain.com
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver Jump ") = "smtp.mail.yahoo.com"
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport Jump ") = 25 'default port for email
'uncomment next three lines if you need to use SMTP Authorization
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername Jump ") = "your-username"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword Jump ") = "your-password"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate Jump ") = 1 'cdoBasic
objFlds.Update
objMail.Configuration = objConf
objMail.From = "fromEmailAddress@yourdomain.com"
objMail.To = "toEmailAddress@yourdomain.com"
objMail.Subject = "Put your email's subject line here"
objMail.TextBody = "Your email body content goes here"

Attach = WScript.Arguments(0)

objMail.AddAttachment Attach 'Don't use = after AddAttachment, just provide the path
objMail.Send
 
'Set all objects to nothing after sending the email
Set objFlds = Nothing
Set objConf = Nothing
Set objMail = Nothing

Registry Location :
Computer\HKEY_CLASSES_ROOT*\shell\Mail
Computer\HKEY_CLASSES_ROOT*\shell\Mail\command
c:\Windows\System32\mail.vbs "%1"

Email Sent MsgBox is displayed. But Mail is not being sent. Can someone try and help.

Tathastu Pandya
  • 326
  • 1
  • 8
  • `%1` works in any shortcut but you’ll need to call the script through cscript.exe to be able to access `WScript.Arguments`. – user692942 Jul 19 '22 at 07:22
  • i tried Attach = WScript.Arguments(1), then added registry cmd /c pathtovbs %1, but it throws error subscript out of range. can you help with that – Tathastu Pandya Jul 19 '22 at 15:33
  • The first argument will be at position `0` as `Arguments` is a 0-based ordinal array. At the moment you are trying to access an index that doesn't exist, hence the "Subscript out of range" error. See [`WshArguments` Object](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/ss1ysb2a(v=vs.84)). Also, note how [the answer](https://stackoverflow.com/a/2806731/692942) in the duplicate question references `WScript.Arguments(0)`. – user692942 Jul 19 '22 at 15:41
  • its not the case Attach variable should point to location of file passed as %1 in registry 0 will the script itself – Tathastu Pandya Jul 19 '22 at 15:59
  • Sorry, but I'm struggling to follow that sentence. The `%1` is passed as an argument into the VBScript and as it's the only argument passed in it should be accessed using `WScript.Arguments(0)`. So if your registry entry uses `cscript.exe test.vbs "%1"` for example `%1` will contain (when accessed via the context menu) the path to the file you have right clicked on and it will be the first argument passed into the VBScript (`test.vbs`). So to access it in the script use `WScript.Arguments(0)`. – user692942 Jul 19 '22 at 16:23
  • Does this answer your question? [Creating Simple Custom Context Menu Commands - How can a VB Script be made to run that uses the file path/name that was right clicked?](https://stackoverflow.com/q/64305794/692942) – user692942 Jul 19 '22 at 20:00
  • seems like WScript.Arguments(0) is resolved to filename. everything is fine evrn msgbox email sent is seen but still mail is not being send after some time wscript returns error transport has lost its connection to server – Tathastu Pandya Jul 20 '22 at 05:27
  • The config schema values have “Jump" affixed to the end of them, that is not a valid schema string. Also does yahoo allow you to relay email through port 25? – user692942 Jul 20 '22 at 07:24
  • but if i write the same script in vbsedit and provide path to file in Attach variable manually then it works. just not working with right click – Tathastu Pandya Jul 20 '22 at 07:26
  • Does this answer your question? [Sending mail through Yahoo fails](https://stackoverflow.com/a/33938567/692942) – user692942 Jul 20 '22 at 07:27
  • i was using the similiar script just variables were different and added attachment – Tathastu Pandya Jul 20 '22 at 07:28
  • The schema configuration string `http://schemas.microsoft.com/cdo/configuration/sendusing Jump` isn’t a valid [configuration value](https://learn.microsoft.com/en-us/previous-versions/exchange-server/exchange-10/ms526288(v%3dexchg.10)), I can’t understand how that would ever work. – user692942 Jul 20 '22 at 07:30
  • schemas are all correct if i execute script without attachment then it works. But with attachment it fails – Tathastu Pandya Jul 22 '22 at 07:29
  • Can you [edit] the question and update the code the reflect what you have tried (correcting the schema etc.)? – user692942 Jul 22 '22 at 07:49

0 Answers0