如何从Windowsbatch file发送简单的电子邮件?

我正在运行Windows 2003 Service Pack 2.我有一个按需运行的batch file。 我希望每次运行batch file时都会收到一封电子邮件。 电子邮件很简单,只是一个表示batch file运行的句子; 每次都是一样的

我已经尝试了几件事情来完成。 我想到了telnet,但我不知道如何将一组命令redirect到telnet; Windowsbatch file没有Unix风格的“here文档”,并且在其中scriptfile包含用于发送电子邮件的命令的情况下调用"telnet <scriptfile"不起作用。 我还使用CDO.Message在互联网上find了一些解决scheme,但是我从来没有使用过,我一直在收到我不明白的错误消息。

我怎样才能从Windowsbatch file发送简单的电子邮件?

马克斯是正确的轨道与建议使用Windows脚本的方式来做到这一点,而不需要在机器上安装任何额外的可执行文件。 如果您使用“智能主机”设置将IIS SMTP服务设置为转发出站电子邮件,或者该计算机正好运行Microsoft Exchange,则他的代码将可以正常工作。 否则,如果没有配置,你会发现你的邮件堆积在邮件队列文件夹(\ inetpub \ mailroot \ queue)中。 因此,除非您可以配置此服务,否则您还希望能够指定要用来发送消息的电子邮件服务器。 要做到这一点,你可以在你的Windows脚本文件中做这样的事情:

 Set objMail = CreateObject("CDO.Message") Set objConf = CreateObject("CDO.Configuration") Set objFlds = objConf.Fields objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.your-site-url.com" 'your smtp server domain or IP address goes here objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'default port for email 'uncomment next three lines if you need to use SMTP Authorization 'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username" 'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password" 'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic objFlds.Update objMail.Configuration = objConf objMail.FromName = "Your Name" objMail.From = "your@address.com" objMail.To = "destination@address.com" objMail.Subject = "Email Subject Text" objMail.TextBody = "The message of the email..." objMail.Send Set objFlds = Nothing Set objConf = Nothing Set objMail = Nothing 

我已经使用了多年的Blat( http://www.blat.net/ )。 这是一个简单的命令行工具,可以从命令行发送电子邮件。 它是免费的,开源的。

您可以使用命令,如“Blat myfile.txt – 到fee@fi.com – 服务器smtp.domain.com -port 6000”

这里是一些其他软件,你可以尝试从命令行发送电子邮件(我从来没有使用过):
http://caspian.dotconf.net/menu/Software/SendEmail/
http://www.petri.co.il/sendmail.htm
http://www.petri.co.il/software/mailsend105.zip
http://retired.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm

在这里( http://www.petri.co.il/send_mail_from_script.htm ),你可以找到其他各种从VBS脚本发送电子邮件的方式,加上一些提到的软件

以下VBScript代码是从该页面中获取的

 Set objEmail = CreateObject("CDO.Message") objEmail.From = "me@mydomain.com" objEmail.To = "you@yourdomain.com" objEmail.Subject = "server is down!" objEmail.Textbody = "server100 is no longer accessible over the network." objEmail.Send 

将该文件保存为something.vbs

 Set Msg = CreateObject("CDO.Message") With Msg .To = "you@yourdomain.com" .From = "me@mydomain.com" .Subject = "Hello" .TextBody = "Just wanted to say hi." .Send End With 

将该文件保存为something2.vbs

我认为这些VBS脚本使用Windows默认邮件服务器(如果存在)。 我没有测试这些脚本…

如果PowerShell可用,则Send-MailMessage命令行程序是一个单行命令,可以轻松地从批处理文件中调用该命令来处理电子邮件通知。 以下是您将在批处理文件中包含以调用PowerShell脚本的行的示例( %xVariable%是您可能希望从批处理文件传递给PowerShell脚本的变量):

– [批量文件] –

 :: ...your code here... C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -command C:\MyScripts\EmailScript.ps1 %xVariable% 

以下是您可能包含在PowerShell脚本中的一个示例(如果您从批处理文件中传递%xVariable% ,则必须在脚本中包含PARAM行作为第一个非标记行:

– [POWERSHELL SCRIPT] –

 Param([String]$xVariable) # ...your code here... $smtp = "smtp.[emaildomain].com" $to = "[Send to email address]" $from = "[From email address]" $subject = "[Subject]" $body = "[Text you want to include----the <br> is a line feed: <br> <br>]" $body += "[This could be a second line of text]" + "<br> " $attachment="[file name if you would like to include an attachment]" send-MailMessage -Smtpserver $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Attachment $attachment -Priority high 

如果您不能按照Max的建议在您的服务器上安装Blat(或任何其他实用程序),那么您的服务器可能已经安装了可以发送电子邮件的软件。

我知道Oracle和Sqlserver都有能力发送邮件。 您可能必须与您的DBA合作才能启用该功能并/或获得使用该功能的权限。 当然,我可以看到这可能会带来一系列问题和繁文</s>节。 假设您可以访问该功能,那么将批处理文件登录到数据库并发送邮件相当简单。

批处理文件可以通过CSCRIPT轻松运行VBScript。 快速谷歌搜索发现许多链接显示如何发送电子邮件与VBScript。 我碰巧看到的第一个是http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/enterprise/mail/ 它看起来很直接。