通过Powershell使用批处理文件发送带有附件的电子邮件 [英] Sending email message with attachment by powershell with a batch file
问题描述
我正在尝试从批处理file.bat发送电子邮件,但是我得到了一个红色错误列表,并且滚动非常快,并且窗口也很快关闭,我试图通过命令使其保持打开状态
Hi i'm trying to send email from batch file.bat but I'm getting a list of red errors and rolling really fast and the window close also fast I tried to keep it open by the command
cmd /k
它仍然打开,但不会显示任何错误列表.
and it's still open but it won't show any error list.
不是:我使用Gmail帐户作为smtp,我打开了smtp设置并启用了不太安全的登录选项.
not that: I'm using Gmail account as smtp and i opened smtp settings and enabled the less secure login option.
最后,运行bat的就是该命令. cmd执行命令:
finally what to run the bat is that command. cmd execute command :
file.bat "mygmail@gmail.com" "mypassword" "D:\test\myFile.txt"
file.bat包含:
file.bat contains :
@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)
请帮助我这里有什么问题.谢谢.
please Help me what is the problem here. Thanks.
推荐答案
PS-Gmail-Sender.bat
@ECHO OFF
REM https://stackoverflow.com/questions/28605803/can-not-send-mail-using-smtp-gmail-com-port-587-from-vbs-script/28606754#28606754
Title Sending E-Mail with Gmail Less Secure Applications using Powershell and Batch
SET GmailAccount="%~1"
SET GmailPassword="%~2"
SET Attachment="%~3"
REM We write our Powershell script
CALL :WritePS
REM We execute our Powershell script .PS1 by passing arguments from the command line or a batch file
Powershell -ExecutionPolicy bypass -noprofile -file "%PSScript%" "%GmailAccount%" "%GmailPassword%" "%Attachment%"
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
pause
EXIT
REM -----------------------------------------------------------------------------------------------------
:WritePS
SET PSScript=%temp%\temp_SendeMail.ps1
> "%PSScript%" (
ECHO $Username = $args[0]
ECHO $EmailPassword = $args[1]
ECHO $Attachment= $args[2]
ECHO $EmailTo = $Username
ECHO $EmailFrom = $Username
ECHO $Subject = "This email was sent from Powershell script into a batch file with Less Secure Application Enabled"
ECHO $Body= "Test Email Sending with a script"
ECHO $SMTPServer = "smtp.gmail.com"
ECHO $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body^)
ECHO $Attachment = New-Object System.Net.Mail.Attachment($Attachment^)
ECHO $SMTPMessage.Attachments.Add($Attachment^)
ECHO $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587^)
ECHO $SMTPClient.EnableSsl = $true
ECHO $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword^)
ECHO $SMTPClient.Send($SMTPMessage^)
)
Exit /B
REM -----------------------------------------------------------------------------------------------------
因此,通过命令行或批处理文件,我们可以将其命名如下:
So with the command line or a batch file we can call it as below :
PS-Gmail-Sender.bat "Mygmail_Account@gmail.com" "MyGmail_Password" "D:\test\myFile.txt"
这篇关于通过Powershell使用批处理文件发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!