繁体   English   中英

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

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

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

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

如何从 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

我已经使用 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

将文件另存为 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

将文件另存为 something2.vbs

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

如果 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%

以下是您可能包含在 PowerShell 脚本中的示例(如果您包括从批处理文件中传递%xVariable% ,则必须将 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  
$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 )

通过在变量周围使用双引号,它对我有用。

我正在使用批处理脚本调用 powershell Send-MailMessage

批处理脚本:send_email.bat

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -command 'E:\path\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 "

如果您不能按照 Max 的建议在您的服务器上安装 Blat(或任何其他实用程序),那么您的服务器可能已经安装了可以发送电子邮件的软件。

我知道 Oracle 和 SqlServer 都有发送电子邮件的能力。 您可能需要与 DBA 合作才能启用该功能和/或获得使用它的权限。 当然,我可以看到这可能会带来一系列问题和繁文缛节。 假设您可以访问该功能,那么通过批处理文件登录到数据库并发送邮件是相当简单的。

批处理文件可以通过 CSCRIPT 轻松运行 VBScript。 一个快速的谷歌搜索找到了许多显示如何使用 VBScript 发送电子邮件的链接。 我碰巧看到的第一个是http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/enterprise/mail/ 它看起来很直。

暂无
暂无

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

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