vbscript检查是否发送了电子邮件

时间:2016-03-18 09:15:19

标签: email vbscript sendmail

我创建了一个脚本来发送每周报告,但如果电子邮件成功发送,我不知道如何实现状态消息。目前我只有一条消息显示确认发送消息,但这并没有真正检查是否发送了电子邮件。 这是我的剧本:

Dim fso, path, file, recentDate, recentFile, ToAddress, FromAddress, MessageSubject, MyTime, MessageBody, MessageAttachment, ol, ns, newMail
Set fso = CreateObject("Scripting.FileSystemObject")
Set recentFile = Nothing
For Each file in fso.GetFolder("C:\REPORTData\temp").Files
  If (recentFile is Nothing) Then
    If UCase(fso.GetExtensionName(file.name)) <> "PDF" Then
        Set recentFile = file
    End If
  ElseIf (file.DateLastModified > recentFile.DateLastModified) Then
    If UCase(fso.GetExtensionName(file.name)) <> "PDF" Then
        Set recentFile = file
    End If
  End If
Next
ToAddress = "randomemail@randomemail.com"
MessageSubject = "Emailing: "+recentFile.Name
MessageBody = "Your message is ready to be sent with the following file or link attachments: "+recentFile.Name
MessageAttachment = "C:\REPORTData\temp\"+recentFile.Name
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
CurrentWeek = Right(recentFile.Name, 2)
result = MsgBox ("Send REPORT for week: "+CurrentWeek, vbYesNo + vbQuestion, "REPORT Sender v.0.1")
Select Case result
Case vbYes
    newMail.Send
    Set shell = CreateObject("Wscript.Shell") 
    shell.Popup "Your REPORT for week "+CurrentWeek+" has been sent", 2, "SUCCESS"
Case vbNo
End Select

1 个答案:

答案 0 :(得分:2)

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"
Else
    Msgbox err.number
End if

是如何。