创建一个vbs脚本函数,用于发送带附件的电子邮件

时间:2016-02-26 20:38:37

标签: vbscript

我是脚本新手,我在openoffice中创建了一个自动化任务我遇到了粗糙的基本语言问题,但是我找到了一个用vba编写的excel宏,它做了我现在需要的尝试将其转换为一个可用的vbscript,可以从命令提示符执行,例如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 = "wolverinewest@wolverinetruckgroup.com"
        .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

2 个答案:

答案 0 :(得分:1)

VBScript没有内置常量,因此您必须在VBA的对象浏览器中查看常量(Alt + F11 - Excel中的F2)并使用该数字。 VBScript中只有一种数据类型,因此不会像任何东西一样变暗(例如Dim x as string只有Dim x)。 VBscript只能后期绑定,所以没有Dim cdoMsg As New CDO.Message - 必须set cdomsg = CreateObject("cdo.message")

VBScript没有什么粗糙的。

字段将具有从Outlook Express或Windows Mail获取的默认值,但未在Windows 10上设置,因为它没有这两个程序中的任何一个。

这是最低限度

Set emailObj      = CreateObject("CDO.Message")
emailObj.From     = "dcandy@gmail.com"
emailObj.To       = "dcandy@gmail.com"
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"

答案 1 :(得分:0)

如果使用wscript,则在Windows中启动的语法是;

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"

“”是必需的,因为如果空格在路径

中,则通过关闭顺序的空格和窗口会发送错误
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"