批量发送电子邮件

时间:2013-12-14 14:41:18

标签: email batch-file

我正在使用批处理程序,在崩溃或错误时,它将生成一个名为debug.txt的文本文件。我需要知道是否有办法通过批处理将此文件自动发送到以下电子邮件地址“something@example.com”。 debug.txt与批处理文件位于同一位置。有谁知道我可以使用的代码。它不能有任何额外的软件。

2 个答案:

答案 0 :(得分:1)

我现在看到3个选项:

1。底线是批量没有内置方式,但是可以从批处理文件调用第三方工具,如blat等,但正如你所提到的你不需要任何额外的软件。

2. 您可以启用已安装的Windows SMTP服务器。然后运行Powershell脚本:

$smtpServer = "system.abc.com"
$smtpFrom = "dontreply@abc.com"
$smtpTo = "something@abc.com"
$messageSubject = "Put your subject here"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = Get-Content debug.txt

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

3. 您可以启用已安装的Windows SMTP服务器。然后运行VBScript:

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const FileToBeUsed = "debug.txt"
Dim objCDO1
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(FileToBeUsed, ForReading)
Set objCDO1 = CreateObject("CDO.Message")
objCDO1.Textbody = f.ReadAll
f.Close
objCDO1.TO ="something@abc.com"
objCDO1.From = "dontreply@abc.com"
objCDO1.Subject = "Put your subject here"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /sendusing") = 2 
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /smtpserver") = "system.abc.com"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /smtpserverport") = 25 
objCDO1.Configuration.Fields.Update     
objCDO1.Send
Set f = Nothing
Set fso = Nothing

正如您所提到的,您使用的是Windows 7终极版,因此选项2和3都可以很好地运行在您的系统上。

答案 1 :(得分:0)

您可以使用免费工具,例如sendemail.exe http://caspian.dotconf.net/menu/Software/SendEmail/。下载sendemail.exe并将其复制到系统路径。这仅适用于简单的内部SMTP邮件。除非您的Exchange服务器设置为发送匿名外部邮件,否则不能用于发送外部邮件。几乎所有的Exchange服务器都没有设置为执行此操作。

您可以在批处理脚本中使用此简单例程。

CALL:SENDEMAILALERT "From SMTP address" "To SMTP addresses" "Subject" "Message" "File to attach" "smtp.host.com:25"

:SENDEMAILALERT
SET SENDEMAILCMD=-f "%~1"
SET SENDEMAILCMD=%SENDEMAILCMD% -t "%~2"
SET SENDEMAILCMD=%SENDEMAILCMD% -u "%~3"
SET SENDEMAILCMD=%SENDEMAILCMD% -m "%~4"
SET SENDEMAILCMD=%SENDEMAILCMD% -a "%~5"
SET SENDEMAILCMD=%SENDEMAILCMD% -s "%~6"
SENDEMAIL %SENDEMAILCMD% >NUL 2>&1
SET SENDEMAILCMD=
GOTO:EOF