我正在运行Windows 2003 Service Pack2。我有一个按需运行的批处理文件。我希望每次运行批处理文件时都发送一封电子邮件。电子邮件很简单,只有一句话表明批处理文件已运行;每次都是一样的。

我已经尝试了一些方法来完成此任务。我想到了telnet,但是我不知道如何将一组命令重定向到telnet。 Windows批处理文件没有Unix风格的“此处文档”,因此调用"telnet <scriptfile"(其中scriptfile包含发送电子邮件的命令)无效。我还使用CDO.Message在Internet上找到了一些解决方案,但是我以前从未使用过,并且不断收到我不理解的错误消息。

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

最佳答案

Max的建议是正确的,建议使用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

关于windows - 如何从Windows批处理文件发送简单的电子邮件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9038926/

10-16 14:59