如何从Windows批处理文件发送一个简单的电子邮件? [英] How to send a simple email from a Windows batch file?

查看:245
本文介绍了如何从Windows批处理文件发送一个简单的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行Windows 2003 Service Pack 2中我有一个按需运行的批处理文件。我想有发送的每一个批处理文件运行时的电子邮件。电子邮件很简单,只是表明该批处理文件运行一个句子;它是相同的,每次

I'm running Windows 2003 Service Pack 2. I have a batch file that runs on demand. I want to have an email sent every time the batch file runs. The email is simple, just a sentence indicating that the batch file ran; it is the same every time.

我试图得到这个工作了几件事。我想远程登录,但我无法弄清楚如何重定向一组命令到远程登录的; Windows批处理文件没有Unix风格,这里的文件,并呼吁远程登录<脚本文件其中的脚本文件的包含的命令发送电​​子邮件没有工作。我还发现一对夫妇使用CDO.Message在互联网上的解决方案,但我从来没有使用之前,我一直得到我不明白的错误消息。

I've tried a couple of things to get this done. I thought of telnet, but I can't figure out how to redirect a set of commands into telnet; Windows batch files don't have a Unix-style "here document," and calling "telnet <scriptfile" where scriptfile contains the commands to send an email didn't work. I also found a couple of solutions on the internet using CDO.Message, but I've never used that before and I kept getting error messages that I don't understand.

我如何从一个Windows批处理文件,发送一个简单的电子邮件?

How can I send a simple email from a Windows batch file?

推荐答案

Max是对他说的对与建议跟踪使用Windows脚本的方式做到这一点没有机器上安装任何额外的可执行文件。如果您有IIS SMTP服务设置转发使用智能主机设置,或机器也恰好运行Microsoft Exchange发出的电子邮件他的code会工作。否则,如果不配置,你会发现你的电子邮件在消息队列文件夹只是堆放(\\的Inetpub \\ mailroot \\队列)。所以,除非你可以配置此服务,你也希望能够指定要使用与发送消息的电子邮件服务器。要做到这一点,你可以做这样的事情在您的Windows脚本文件:

Max is on he right track with the suggestion to use Windows Scripting for a way to do it without installing any additional executables on the machine. His code will work if you have the IIS SMTP service setup to forward outbound email using the "smart host" setting, or the machine also happens to be running Microsoft Exchange. Otherwise if this is not configured, you will find your emails just piling up in the message queue folder (\inetpub\mailroot\queue). So, unless you can configure this service, you also want to be able to specify the email server you want to use to send the message with. To do that, you can do something like this in your windows script file:

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

这篇关于如何从Windows批处理文件发送一个简单的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆