使用 bat 文件将文件发送到电子邮件地址而不公开电子邮件信息?

使用 bat 文件将文件发送到电子邮件地址而不公开电子邮件信息?

我想通过一个 bat 文件将文件发送到我的 Gmail 帐户,并在该 bat 文件中加密我的电子邮件信息,这样如果有人打开该 bat 文件,他们就无法从中获取电子邮件信息。

逻辑如:

  1. 检查互联网是否正常工作

    • 如果可以,请发送文件,然后删除该 bat 文件
  2. 检查互联网是否正常工作

    • 如果没有,则安排稍后执行或将其添加到启动

    • 完成后将其从计划或启动中删除!

答案1

如果你需要发送电子邮件(即使带有附件),只需PowerShell 脚本通过构建和发送电子邮件的逻辑,您可以通过批处理脚本将敏感值作为参数传递,而不是将敏感值硬编码到脚本逻辑中。

PowerShell 脚本逻辑可以接受诸如 Gmail 本地邮箱用户名、发送电子邮件的身份验证密码以及您不想在脚本中硬编码的任何其他内容等参数。

  • PowerShell 参数

    在脚本或函数中,您可以使用 $args 数组引用未命名的参数,例如将所有参数传递给 cmdlet。您还可以通过其位置引用特定参数:

    “第一个参数是” + $Args[0]

    “第二个参数是” + $Args[1]

您还可以将逻辑放入批处理脚本所以它也有参数,您可以在执行时将敏感值传递给它,并使用参数占位符而不是硬编码敏感值。

  • 批处理参数

    %您可以使用命令行上的数字位置来获取任何参数的值。传递的第一项始终是%1第二项始终是,%2依此类推

    批处理脚本中的 %* 指的是所有参数(例如),只有的%1 %2 %3 %4 %5 ...%255参数可以用数字引用。%1%9

这样,如果脚本被打开,您需要保护的敏感值将不会被暴露或硬编码到脚本逻辑中,任何人都无法看到谁可以读取该脚本。


重要的提示: 每个 PowerShell 脚本示例名称底部都有一个部分批量执行脚本它具有从批处理脚本中执行的逻辑,因此您将使用用户名、密码和/或附件完整路径作为批处理脚本的适当参数IEsendemail.bat "<GmailAccountName>" "<GmailPassword>" "<FullPathAttachment>"

PowerShell 脚本(无附件)

$Username      = $args[0]
$EmailPassword = $args[1]

$Username = $Username
$EmailTo = "[email protected]" 
$EmailFrom = "[email protected]"
$Subject = "Email Subject"
$Body = "Email Body"
$SMTPServer = "smtp.gmail.com" 
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword); 
$SMTPClient.Send($SMTPMessage)

批量执行脚本

SET GmailAccount=%~1
SET GmailPassword=%~2
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& 'C:\Scripts\SendEmail.ps1' '%GmailAccount%' '%GmailPassword%'"

PowerShell 脚本(带附件)

$Username      = $args[0]
$EmailPassword = $args[1]
$Attachment    = $args[2]

$Username = $Username
$EmailTo = "[email protected]" 
$EmailFrom = "[email protected]"
$Subject = "Email Subject"
$Body = "Email Body"
$SMTPServer = "smtp.gmail.com" 
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
$Attachment = New-Object System.Net.Mail.Attachment($Attachment)
$SMTPMessage.Attachments.Add($Attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword); 
$SMTPClient.Send($SMTPMessage)

批量执行脚本

SET GmailAccount=%~1
SET GmailPassword=%~2
SET Attachment=%~3
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%" 
Powershell -ExecutionPolicy Bypass -Command "& 'C:\Scripts\SendEmail.ps1' '%GmailAccount%' '%GmailPassword%' '%Attachment%'"

批处理脚本(动态)

这是一个一体化动态批处理脚本,您只需传递 Gmail 帐户用户名、Gmail 帐户密码和附件的完整路径。

@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     = "[email protected]" >> "%PSScript%"
ECHO $EmailFrom   = "[email protected]" >> "%PSScript%"
ECHO $Subject     = "Email Subject"           >> "%PSScript%"
ECHO $Body        = "Email Body"              >> "%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%"
GOTO :EOF

您可以像这样执行批处理脚本...

 sendemail.bat "<GmailAccountName>" "<GmailPassword>" "<FullPathAttachment>"

在此处输入图片描述


批处理脚本(静态和自删除)

GmailAccount=此脚本将在、和 的变量中设置硬编码值,GmailPassword=Attachment=一旦执行,它将通过 完全删除脚本,"%~FN0"其中 是0脚本本身。这意味着您需要确保复制此脚本,然后仅从您不想暴露此信息的机器上运行副本。

@ECHO OFF

SET GmailAccount=<GmailAccountName>
SET GmailPassword=<GmailPassword>
SET Attachment=<FullAttachmentPath>

CALL :PowerShell
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%' '%GmailAccount%' '%GmailPassword%' '%Attachment%'"
IF EXIST "%~FN0" DEL /Q /F "%~FN0"
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     = "[email protected]" >> "%PSScript%"
ECHO $EmailFrom   = "[email protected]" >> "%PSScript%"
ECHO $Subject     = "Email Subject"           >> "%PSScript%"
ECHO $Body        = "Email Body"              >> "%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%"
GOTO :EOF

更多资源

相关内容