2011-08-12 98 views
9

我已经编写了一个每晚运行的自动化测试,并且我希望在测试完成后每天晚上通过电子邮件发送结果。用VBScript发送电子邮件而不运行Outlook

为了做到这一点,我试图把下面以我的批处理文件的末尾:

Set MyApp = CreateObject("Outlook.Application") 
Set MyItem = MyApp.CreateItem(0) 
With MyItem 
    .To = "[email protected]" 
    .Subject = "Subject" 
    .ReadReceiptRequested = False 
    .HTMLBody = "resport" 
End With 
MyItem.Send 

然而,这是导致电子邮件无法发送,因为我的Outlook不开,因为测试在后台运行,我无法访问UI。

无论如何发送此电子邮件没有实际运行在机器上的前景。

谢谢!

+0

http://stackoverflow.com/questions/6588621/how-to-send-email-to-a-distribution-list-with -vbscript-in-an-asp – 2011-08-12 15:53:54

回答

19

运行后,可以使用CDO.Message对象在VBScript中发送电子邮件没有Outlook相当全功能的SMTP客户端。你需要知道你的SMTP服务器的地址,使用这个:

Set MyEmail=CreateObject("CDO.Message") 

MyEmail.Subject="Subject" 
MyEmail.From="[email protected]" 
MyEmail.To="[email protected]" 
MyEmail.TextBody="Testing one two three." 

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2 

'SMTP Server 
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.server.com" 

'SMTP Port 
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 

MyEmail.Configuration.Fields.Update 
MyEmail.Send 

set MyEmail=nothing 

如果您的SMTP服务器需要用户名,然后密码在MyEmail.Configuration.Fields.Update线之上粘贴这些行:

'SMTP Auth (For Windows Auth set this to 2) 
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1 
'Username 
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="username" 
'Password 
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")="password" 

更多信息在使用CDO发送电子邮件与VBScript可以在下面的链接找到: http://www.paulsadowski.com/wsh/cdo.htm

+0

非常感谢!这有助于一吨!我要求服务器的名称,并希望在星期一之前有。然后我会选择这个作为接受的答案。 – user856354

+0

如果我希望邮件中包含用户名和机器名称,例如'userxyz登录到machinexyz',该怎么办?其实我有Windows 7客户端,我想通过发送邮件来跟踪登录。所以我将这个代码作为登录事件任务来调用。我有管理AD的Windows Server 2008。那么我应该在哪里配置[this](http://superuser.com/questions/660703/getting-mail-on-windows-logon-with-username-of-logged-in-user)规则 - 在win 7客户端上或赢得服务器。这有什么区别吗?也有可能使用'vbs'来实现这一点? – Mahesha999

0

是的。 Blat或任何其他自带的SMTP邮件程序。 BLAT是从命令行

Blat is here

相关问题