1

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.

not that: I'm using Gmail account as smtp and i opened smtp settings and enabled the less secure login option.

finally what to run the bat is that command. cmd execute command :

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

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)                                                               >> "%PSScript%"

please Help me what is the problem here. Thanks.

Deller
  • 29
  • 7
  • This question is duplicate and has an [answer here](https://stackoverflow.com/questions/62338807/file-bat-dont-run-properly?answertab=active#tab-top) – Hackoo Jun 14 '20 at 03:46
  • Does this answer your question? [file.bat don't run properly](https://stackoverflow.com/questions/62338807/file-bat-dont-run-properly) – Hackoo Jun 14 '20 at 03:48

0 Answers0