通过批处理文件发送带有附件的电子邮件消息

时间:2020-06-09 22:00:26

标签: email send

嗨,我正在尝试从批处理file.bat发送电子邮件 但是我得到了一个红色错误列表,并且滚动得非常快,并且窗口也很快关闭,我尝试通过命令

使其保持打开状态
cmd /k

它仍然打开,但是不会显示任何错误列表。

不是:我将Gmail帐户用作smtp,我打开了smtp设置并启用了不太安全的登录选项。

最后,运行bat的就是该命令。 cmd执行命令:

file.bat "mygmail@gmail.com" "mypassword" "D:\test\myFile.txt"

file.bat包含:

@ECHO OFF
    SET GmailAccount=%~1
    SET GmailPassword=%~2
    SET Attachment=%~3

    CALL :PowerShell
    CD /D "%PowerShellDir%"
    Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%' '%GmailAccount%' '%GmailPassword%' '%Attachment%'"
    EXIT

    :PowerShell
    SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
    SET PSScript=%temp%\~tmpSendeMail.ps1
    IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"

    ECHO $Username      = $args[0]>> "%PSScript%"
    ECHO $EmailPassword = $args[1]>> "%PSScript%"
    ECHO $Attachment    = $args[2]>> "%PSScript%"
    ECHO                          >> "%PSScript%"
    ECHO $Username    = $Username                 >> "%PSScript%"
    ECHO $EmailTo     = "target@mail.com" >> "%PSScript%"
    ECHO $EmailFrom   = "mygmail@gmail.com" >> "%PSScript%"
    ECHO $Subject     = "test"           >> "%PSScript%"
    ECHO $Body        = "test"              >> "%PSScript%"
    ECHO $SMTPServer  = "smtp.gmail.com"          >> "%PSScript%"
    ECHO $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body) >> "%PSScript%"
    ECHO $Attachment  = New-Object System.Net.Mail.Attachment($Attachment)                            >> "%PSScript%"
    ECHO $SMTPMessage.Attachments.Add($Attachment)                                                    >> "%PSScript%"
    ECHO $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)                               >> "%PSScript%"
    ECHO $SMTPClient.EnableSsl = $true                                                                >> "%PSScript%"
    ECHO $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword) >> "%PSScript%"
    ECHO $SMTPClient.Send($SMTPMessage)                                                               >> "%PSScript%"

请帮助我这里有什么问题。 谢谢。

0 个答案:

没有答案