简体   繁体   English

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

[英]How to send a simple email from a Windows batch file?

I'm running Windows 2003 Service Pack 2. I have a batch file that runs on demand.我正在运行 Windows 2003 Service Pack 2。我有一个按需运行的批处理文件。 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.每次都是一样的。

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;我想到了telnet,但是我不知道如何将一组命令重定向到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. Windows 批处理文件没有 Unix 样式的“此处文档”,并且调用"telnet <scriptfile"其中scriptfile包含发送电子邮件的命令)不起作用。 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.我还在互联网上找到了一些使用 CDO.Message 的解决方案,但我以前从未使用过,而且我不断收到我不明白的错误消息。

How can I send a simple email from a Windows batch file?如何从 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. Max 的建议是正确的,他建议使用 Windows 脚本来实现,而无需在机器上安装任何额外的可执行文件。 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.如果您设置了 IIS SMTP 服务以使用“智能主机”设置转发出站电子邮件,或者机器也恰好运行 Microsoft Exchange,那么他的代码将起作用。 Otherwise if this is not configured, you will find your emails just piling up in the message queue folder (\\inetpub\\mailroot\\queue).否则,如果没有配置,您会发现您的电子邮件只是堆积在消息队列文件夹 (\\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:为此,您可以在 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

I've used Blat ( http://www.blat.net/ ) for many years.我已经使用 Blat ( http://www.blat.net/ ) 很多年了。 It's a simple command line utility that can send email from command line.这是一个简单的命令行实用程序,可以从命令行发送电子邮件。 It's free and opensource.它是免费和开源的。

You can use command like "Blat myfile.txt -to fee@fi.com -server smtp.domain.com -port 6000"您可以使用“Blat myfile.txt -to fee@fi.com -server smtp.domain.com -port 6000”之类的命令

Here is some other software you can try to send email from command line (I've never used them):这是您可以尝试从命令行发送电子邮件的其他一些软件(我从未使用过它们):
http://caspian.dotconf.net/menu/Software/SendEmail/ http://caspian.dotconf.net/menu/Software/SendEmail/
http://www.petri.co.il/sendmail.htm http://www.petri.co.il/sendmail.htm
http://www.petri.co.il/software/mailsend105.zip http://www.petri.co.il/software/mailsend105.zip
http://retired.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm http://retired.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm

Here ( http://www.petri.co.il/send_mail_from_script.htm ) you can find other various way of sending email from a VBS script, plus link to some of the mentioned software在这里( http://www.petri.co.il/send_mail_from_script.htm )您可以找到从 VBS 脚本发送电子邮件的其他各种方式,以及一些提到的软件的链接

The following VBScript code is taken from that page以下 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

Save the file as something.vbs将文件另存为 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

Save the file as something2.vbs将文件另存为 something2.vbs

I think these VBS scripts use the windows default mail server, if present.我认为这些 VBS 脚本使用 Windows 默认邮件服务器(如果存在)。 I've not tested these scripts...我还没有测试过这些脚本...

If PowerShell is available, the Send-MailMessage commandlet is a single one-line command that could easily be called from a batch file to handle email notifications.如果 PowerShell 可用,则Send-MailMessage commandlet 是一个单行命令,可以轻松地从批处理文件中调用以处理电子邮件通知。 Below is a sample of the line you would include in your batch file to call the PowerShell script (the %xVariable% is a variable you might want to pass from your batch file to the PowerShell script):以下是您将包含在批处理文件中以调用 PowerShell 脚本的行示例( %xVariable%是您可能希望从批处理文件传递到 PowerShell 脚本的变量):

--[BATCH FILE]-- --[批处理文件]--

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

Below is an example of what you might include in your PowerShell script (you must include the PARAM line as the first non-remark line in your script if you included passing the %xVariable% from your batch file:以下是您可能包含在 PowerShell 脚本中的示例(如果您包括从批处理文件中传递%xVariable% ,则必须将 PARAM 行作为脚本中的第一个非注释行:

--[POWERSHELL SCRIPT]-- --[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  
$emailSmtpServerPort = "587"
$emailSmtpUser = "username"
$emailSmtpPass = 'password'
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "[From email address]"
$emailMessage.To.Add( "[Send to email address]" )
$emailMessage.Subject = "Testing e-mail"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
<p>Here is a message that is <strong>HTML formatted</strong>.</p>
<p>From your friendly neighborhood IT guy</p>
"@
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )

It works for me, by using double quotes around variables.通过在变量周围使用双引号,它对我有用。

I am using batch script to call powershell Send-MailMessage我正在使用批处理脚本调用 powershell Send-MailMessage

Batch Script:send_email.bat批处理脚本:send_email.bat

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -command 'E:\path\send_email.ps1

Pwershell Script send_email.ps1 Pwershell 脚本 send_email.ps1

Send-MailMessage -From "noreply@$env:computername" -To '<target_email@example.com>' -Subject 'Blah Blah' -SmtpServer  'smtp.domain.com'  -Attachments 'E:\path\file.log' -BODY "Blah Blah on Host: $env:computername "

If you can't follow Max's suggestion of installing Blat (or any other utility) on your server, then perhaps your server already has software installed that can send emails.如果您不能按照 Max 的建议在您的服务器上安装 Blat(或任何其他实用程序),那么您的服务器可能已经安装了可以发送电子邮件的软件。

I know that both Oracle and SqlServer have the capability to send email.我知道 Oracle 和 SqlServer 都有发送电子邮件的能力。 You might have to work with your DBA to get that feature enabled and/or get the privilege to use it.您可能需要与 DBA 合作才能启用该功能和/或获得使用它的权限。 Of course I can see how that might present its own set of problems and red tape.当然,我可以看到这可能会带来一系列问题和繁文缛节。 Assuming you can access the feature, it is fairly simple to have a batch file login to a database and send mail.假设您可以访问该功能,那么通过批处理文件登录到数据库并发送邮件是相当简单的。

A batch file can easily run a VBScript via CSCRIPT.批处理文件可以通过 CSCRIPT 轻松运行 VBScript。 A quick google search finds many links showing how to send email with VBScript.一个快速的谷歌搜索找到了许多显示如何使用 VBScript 发送电子邮件的链接。 The first one I happened to look at was http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/enterprise/mail/ .我碰巧看到的第一个是http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/enterprise/mail/ It looks straight forward.它看起来很直。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM