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

use*_*810 29 windows email batch-file

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

我已经尝试了几件事来完成这件事.我想到了telnet,但我无法弄清楚如何将一组命令重定向到telnet; Windows批处理文件没有Unix风格的"here here",并且调用scriptfile包含发送电子邮件的命令的"telnet <scriptfile"地方不起作用.我还使用CDO.Message在互联网上找到了几个解决方案,但我以前从未使用过,我不断收到我不理解的错误消息.

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

dma*_*tta 20

Max是正确的,他建议使用Windows Scripting来实现这一目的,而无需在机器上安装任何其他可执行文件.如果您使用"SMTP主机"设置转发出站电子邮件的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
Run Code Online (Sandbox Code Playgroud)

  • 'objFlds.Item' 不是内部或外部命令,也不是可运行的程序或批处理文件。 (2认同)

Max*_*Max 14

多年来我一直使用Blat(http://www.blat.net/).这是一个简单的命令行实用程序,可以从命令行发送电子邮件.它是免费的和开源的.

您可以使用"Blat myfile.txt -to fee@fi.com -server 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
Run Code Online (Sandbox Code Playgroud)

将文件另存为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
Run Code Online (Sandbox Code Playgroud)

将文件另存为something2.vbs

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


小智 6

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

--[批处理文件]--

:: ...your code here...
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -windowstyle hidden -command C:\MyScripts\EmailScript.ps1 %xVariable%
Run Code Online (Sandbox Code Playgroud)

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

--[POWERSHELL 脚本]--

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  
Run Code Online (Sandbox Code Playgroud)