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

查看:21
本文介绍了如何从 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.

我尝试了一些方法来完成这项工作.我想到了telnet,但是我不知道如何将一组命令重定向到telnet;Windows 批处理文件没有 Unix 风格的此处文档",并且调用 "telnet <scriptfile" 其中 scriptfile 包含发送电子邮件的命令没有工作.我还在互联网上找到了一些使用 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,那么他的代码将起作用.否则,如果没有配置,您会发现您的电子邮件只是堆积在消息队列文件夹 (inetpubmailrootqueue) 中.因此,除非您可以配置此服务,否则您还希望能够指定用于发送邮件的电子邮件服务器.为此,您可以在 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 (inetpubmailrootqueue). 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天全站免登陆