需要使用批处理文件发送邮件

时间:2014-04-19 13:59:22

标签: batch-file

我有以下代码,它给了我2个值。

  1. 首先是基于某个ext文件(test.mtc)
  2. 的最近修改过的目录
  3. 其次是获取该(test.mtc)文件的名称并添加域以发送邮件。例如test.companyname.com
  4. 我能够使用以下批处理文件实现此目的,但现在我需要使用此批处理文件的输出发送邮件。使用blat是可行的吗?

    以下是我在批处理文件中编写的脚本 Output.bat

    cd C:\Desktop\Foldername
    for /f "tokens=*" %%a in ('dir /b /a-d *.mtc') do echo User=%%~na@companyname.com >> Output.txt
    for /f "delims=" %%x in ('dir /a:d /b /o:d') do set recent=%%x
    echo Path=\\Mycomputername\Desktop\FolderName\%recent% >> Output.txt
    

    我需要使用这些值并使用批处理文件发送邮件。类似下面的东西 - 从主题测试完整的身体发送

1 个答案:

答案 0 :(得分:1)

您可以使用此批处理文件使用本机VBS脚本发送电子邮件。

:: email.bat :::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal

:: use these settings to send from a gmail account
:: set port=465 and set SSL=True

:: use these settings for standard email SMTP port and no encryption
:: set port=25 and set SSL=False

:: Change these following items to use the same variables all the time
:: or use the command line to pass all the variables

set Port=25
set SSL=False
set From="myemail@myemailserver.com"
set To="recipient@server.com"
set Subject="Subject line"
set Body="Email Body in one line"
set SMTPServer="mailservername.myemailserver.com"
set User="username"
set Pass="password"
set fileattach="d:\myfolder\file.txt"


:: This section sets the command line arguments
:: use this format:  CALL email.bat "myname@gmail.com" "RecipientEmailAddress@server.com" "Subject line" "Email Body in one line" "smtp.gmail.com"  "myname@gmail.com" "password" "d:\folder\filename to attach.txt"


if "%~7" NEQ "" (
set From="%~1"
set To="%~2"
set Subject="%~3"
set Body="%~4"
set SMTPServer="%~5"
set User="%~6"
set Pass="%~7"
set fileattach="%~8"
)

set "vbsfile=%temp%\email-bat.vbs"
del "%vbsfile%" 2>nul
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
echo >>"%vbsfile%" Set objArgs       = WScript.Arguments
echo >>"%vbsfile%" Set objEmail      = CreateObject("CDO.Message")
echo >>"%vbsfile%" objEmail.From     = %From%
echo >>"%vbsfile%" objEmail.To       = %To%
echo >>"%vbsfile%" objEmail.Subject  = %Subject%
echo >>"%vbsfile%" objEmail.Textbody = %body%
if exist %fileattach% echo >>"%vbsfile%" objEmail.AddAttachment %fileattach%
echo >>"%vbsfile%" with objEmail.Configuration.Fields
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusing")        = 2 ' not local, smtp
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserver")       = %SMTPServer%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserverport")   = %port%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusername")     = %user%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendpassword")     = %pass%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpusessl")       = %SSL%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpconnectiontimeout") = 30
echo >>"%vbsfile%"  .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send

cscript.exe /nologo "%vbsfile%"
echo email sent (if variables were correct)
del "%vbsfile%" 2>nul
goto :EOF