VB Script 调用Outlook发送邮件

(0 comments)

有些环境下,Exchange不支持SMTP Relay,VBS也可以调用Outlook来收发邮件。为了防止可能的病毒传播,VBS程序在发送邮件前,会弹出安全提示框,这个提示框在5秒内不能点"是",只有在用户点"是"以后,邮件才会发送。

发送邮件参数包括收件人,标题,内容等信息,并且可以发送附件,并限制附件最大尺寸。

rcpt="liaojl@liaojl.com, root@liaojl.com"
subject="Send out by Outlook"
body="Hello world!"
attachfile=null
MaxSize = 5000000   ' 附件大小不超过5M

Call sendmailByOutlook(rcpt, subject, body, attachments)

函数定义的代码如下:

Sub SendmailByOutlook(rcpt, subject, body, attachment)
  
  If CreateObject("Scripting.FileSystemObject").GetFile(attachment).size > maxsize 
  then
    attachment = ""
    Wscript.Echo "文件大小超过最大尺寸限制,备份文件将不会发送到邮件"
  End If
  
  Set olapp = CreateObject("outlook.application")
  Set newmail = olapp.CreateItem(olMailItem)
    With newmail
      .To = rcpt                      'mailitem的收件人属性
      .Importance = olImportanceHigh  '将邮件的重要性设置为高
      .subject = subject              'mailitem的主题属性
      .Body = body                    'mailitem的正文属性
      
      '如果需要包含附件,就给邮件添上附件
      If Len(attachment) <> 0 Then .Attachments.Add attachment   
      .Send                           '发送
    End With

  Set newmail = nothing
  Set olapp =nothing
  
End Sub
Currently unrated

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required