Reputation: 15
I'm new to scripting and I'm creating an automated task inside openoffice I'm having issue with the crude basic language but I found a excel macro written in vba that does what I need now im trying to convert this to a useable vbscript that can be executed from the command prompt with something like email.vbs mailto subject filetoattach
Dim cdoMsg As New CDO.Message
With cdoMsg
With .Configuration.Fields
.Item(cdoSendUsingMethod).Value = cdoSendUsingPort
.Item(cdoSMTPUseSSL).Value = True
.Item(cdoSMTPServerPort).Value = 465
.Item(cdoSMTPServer).Value = "smtpserver"
.Item(cdoSendUserName).Value = "[email protected]"
.Item(cdoSendPassword).Value = "mypass"
.Item(cdoSMTPAuthenticate).Value = cdoBasic
.Update
End With
.From = "myemail"
.To = "emailto"
.Subject = "Some more really spiffy mail for you!"
.TextBody = "please find attachment"
.AddAttachment App.Path & "attachment"
On Error Resume Next
.Send
End With
If Err.Number <> 0 Then
MsgBox "CDO error " & Hex$(Err.Number) & vbNewLine & Err.Description, _
vbOKOnly Or vbExclamation, _
Caption
Else
MsgBox "Mail sent!", vbOKOnly, Caption
End If
Upvotes: 0
Views: 26395
Reputation: 9933
This is how you can send email with attachment using VBS:
Function sendEmail()
filePath = "C:\Users\shubham\OneDrive\Pictures\Camera Roll\myImage.jpg"
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Your VBS EMAIL"
objMessage.From = "[email protected]"
objMessage.To = "[email protected]"
objMessage.AddAttachment filePath
objMessage.TextBody = "My Text"
Set objConfig = objMessage.Configuration
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "serverXXXXX.web-XYZABC.com"
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "MYPASSWORD_XXXXXXXX"
'Server port (typically 25)
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objConfig.Fields.Update
objMessage.Send
End Function
sendEmail()
Upvotes: 0
Reputation: 15
syntax for launching in windows if using wscript is;
wscript.exe "C:\path\example.vbs" "your email address" "who your sending it to" "your subject" "your body text & vbCRLF & for new line" "c:\path\attachment"
the "" are required because the spaces through off the order and windows send errors if spaces are in the path
dim mailto
dim mailfrom
dim subject
dim body
dim attachment
mailfrom = WScript.Arguments.Item(0)
mailto = WScript.Arguments.Item(1)
subject = WScript.Arguments.Item(2)
body = WScript.Arguments.Item(3)
attachment = WScript.Arguments.Item(4)
Set emailObj = CreateObject("CDO.Message")
emailObj.From = mailfrom
emailObj.To = mailto
emailObj.Subject = subject
emailObj.TextBody = body
emailObj.AddAttachment attachment
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mysmtp.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myUsername"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "myPassword"
emailConfig.Fields.Update
emailObj.Send
If err.number = 0 then Msgbox "email sent"
Upvotes: 0
Reputation:
VBScript has no constants built in so you have to look the constants in VBA's object browser (Alt + F11 - F2 in Excel) and use the number. There is only one datatype in VBScript so no dimming anything as anything (eg Dim x as string
only Dim x
). VBscript only can late bind so no Dim cdoMsg As New CDO.Message
- have to set cdomsg = CreateObject("cdo.message")
.
There is nothing crude about VBScript.
Fields will have default values taken from Outlook Express or Windows Mail but are not set on Windows 10 as it doesn't have either of those two programs.
Here's the bare minimum
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "[email protected]"
emailObj.To = "[email protected]"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
emailObj.AddAttachment "c:\windows\win.ini"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Username"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password"
emailConfig.Fields.Update
emailObj.Send
If err.number = 0 then Msgbox "Done"
Upvotes: 1