如何从 Windows 批处理文件发送简单的电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9038926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a simple email from a Windows batch file?
提问by user448810
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 2003 Service Pack 2。我有一个按需运行的批处理文件。我希望每次运行批处理文件时都发送一封电子邮件。邮件很简单,只是一句表明批处理文件运行了;每次都是一样的。
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 scriptfilecontains 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.
我已经尝试了几件事来完成这项工作。我想到了telnet,但是我不知道如何将一组命令重定向到telnet;Windows 批处理文件没有 Unix 样式的“此处文档”,并且调用包含发送电子邮件命令的脚本文件的"telnet <scriptfile"
位置不起作用。我还在互联网上找到了一些使用 CDO.Message 的解决方案,但我以前从未使用过,而且我不断收到我不明白的错误消息。
How can I send a simple email from a Windows batch file?
如何从 Windows 批处理文件发送简单的电子邮件?
采纳答案by dmarietta
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:
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 = "[email protected]"
objMail.To = "[email protected]"
objMail.Subject = "Email Subject Text"
objMail.TextBody = "The message of the email..."
objMail.Send
Set objFlds = Nothing
Set objConf = Nothing
Set objMail = Nothing
回答by Max
I've used Blat ( http://www.blat.net/) for many years. It's a simple command line utility that can send email from command line. It's free and opensource.
我已经使用 Blat ( http://www.blat.net/) 很多年了。这是一个简单的命令行实用程序,可以从命令行发送电子邮件。它是免费和开源的。
You can use command like "Blat myfile.txt -to [email protected] -server smtp.domain.com -port 6000"
您可以使用“Blat myfile.txt -to [email protected] -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://www.petri.co.il/sendmail.htm
http://www.petri.co.il/software/mailsend105.zip
http://retired.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm
这是您可以尝试从命令行发送电子邮件的其他一些软件(我从未使用过它们):
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
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 = "[email protected]"
objEmail.To = "[email protected]"
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 = "[email protected]"
.From = "[email protected]"
.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. I've not tested these scripts...
我认为这些 VBS 脚本使用 Windows 默认邮件服务器(如果存在)。我还没有测试过这些脚本...
回答by cabotscott
If PowerShell is available, the Send-MailMessagecommandlet is a single one-line command that could easily be called from a batch file to handle email notifications. 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 可用,则Send-MailMessagecommandlet 是一个单行命令,可以轻松地从批处理文件中调用以处理电子邮件通知。以下是您将包含在批处理文件中以调用 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%
从批处理文件传递
--[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
回答by saba
$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 )
回答by Aashutosh
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 '<[email protected]>' -Subject 'Blah Blah' -SmtpServer 'smtp.domain.com' -Attachments 'E:\path\file.log' -BODY "Blah Blah on Host: $env:computername "
回答by dbenham
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. You might have to work with your DBA to get that feature enabled and/or get the privilege to use it. 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.
我知道 Oracle 和 SqlServer 都有发送电子邮件的能力。您可能需要与 DBA 合作才能启用该功能和/或获得使用它的权限。当然,我可以看到这可能会带来一系列问题和繁文缛节。假设您可以访问该功能,那么通过批处理文件登录到数据库并发送邮件是相当简单的。
A batch file can easily run a VBScript via CSCRIPT. A quick google search finds many links showing how to send email with VBScript. The first one I happened to look at was http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/enterprise/mail/. It looks straight forward.
批处理文件可以通过 CSCRIPT 轻松运行 VBScript。一个快速的谷歌搜索找到了许多显示如何使用 VBScript 发送电子邮件的链接。我碰巧看到的第一个是http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/enterprise/mail/。它看起来很直。