VBS send mail

2 methods:

Sending Mail with OutLook

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Function SendMail()
Dim objOutlook, Mail, PopupLocate

' Create Email object
Set objOutlook=CreateObject("Outlook.Application")
Set objMail=objOutlook.CreateItem(0)

' Setting receiver
objMail.to= "yongfeiuall@163.com"
objMail.cc= "yongfeiuall@163.com"

' Setting title & content & attached
objMail.Subject="自动化测试结果"
objMail.Body="自动化测试结果"
objMail.Attachments.Add("e:\test.htm")

' Mail send
SystemUtil.Run "..\QTP\OutlookSecurityPopup.vbs"
objMail.Send

' 注销Email对象
objOutlook.Quit
Set objMail = Nothing
Set objOutlook = Nothing
End Function

OutlookSecurityPopup

1
2
3
4
5
6
7
8
9
10
11
12
13
Set fso = CreateObject("WScript.Shell")
if fso.AppActivate("Microsoft Office Outlook") = FALSE then
wscript.Sleep 10000
end if

if fso.AppActivate("Microsoft Office Outlook") = true then
wscript.Sleep 1000
fso.AppActivate("Microsoft Office Outlook")
fso.SendKeys "{tab}"
fso.SendKeys "{tab}"
wscript.sleep 1000
fso.SendKeys "{ENTER}"
end if

Sending mail without a security popup

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Const SMTPSERVER = "xxxxxx"
Const MailFrom = "xxxxxx" '发件人邮箱
Const MailPWD = "xxxxxx" '发件人邮箱密码
Const SendTo = "yongfeiuall@163.com" '收件人邮箱
Const Body = "<a href=""http://www.izheyi.com"" >Click Here</a>"

Dim objMail, UseHTML

'Create Email object
Set objMail = CreateObject("CDO.Message")

'Setting sender & receiver
objMail.From = MailFrom
objMail.To = SendTo
'objMail.CC = CC

'Setting title & content & attached
objMail.Subject = "Subject"
objMail.AddAttachment(ExcelReport)
objMail.HTMLBody = "<H5>Below is the example of email with Hyperlink</H5><br>" + Body

'Define mail configuration
Const schema = "http://schemas.microsoft.com/cdo/configuration/"
With objMail.Configuration.Fields
.Item(schema & "sendusing") = 2
.Item(schema & "smtpserver") = SMTPSERVER
.Item(schema & "smtpauthenticate") = 1
.Item(schema & "sendusername") = MailFrom
.Item(schema & "sendpassword") = MailPWD
.Item(schema & "smtpconnectiontimeout") = 60
.Update
End With

'Send mail
objMail.Send
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!