How to catch error while sending an email with an hybrid code batch and powershell ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

How to catch error while sending an email with an hybrid code batch and powershell ?

#1 Post by Hackoo » 01 Oct 2017 10:09

Hi :)
I'm working for a script to send an e-mail with SSL Authentification (Gmail) with an hybrid code Batch and Powershell Script !
So my question how can i catch the error in order to show the correct message.
Thank you !

Code: Select all

<# : Batch portion
@rem # The previous line does nothing in Batch, but begins a multiline comment block
@rem # in PowerShell.  This allows a single script to be executed by both interpreters.
@echo off & Mode 100,5 & color 0A
Title Sending E-mail with SSL Authentification with an Hybrid code Batch and Powershell Script by Hackoo
echo(
rem # This a Powershell command executes the hybrid portion at the bottom of this script
for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0}|out-string)"') do set "%%I"
exit /b
rem # End multi-line PowerShell comment block.  Begin PowerShell scripting.
: end Batch / begin PowerShell hybrid code #>
#################################### First 1 Step ###########################################
# First Step we encrypt the Plain Text Password to an encrypted one using the key AES.key
# Première étape, nous cryptons le mot de passe en clair vers un mot de passe chiffré
# à l'aide de la clé AES.key
$AppData = [Environment]::GetFolderPath('ApplicationData')
$KeyFile = $AppData+"\AES.key"
$Key = New-Object Byte[] 32   # You can use 16, 24, or 32 for AES
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file $KeyFile
$AppData = [Environment]::GetFolderPath('ApplicationData')
$PasswordFile = $AppData+"\Password.txt"
$Key = Get-Content $KeyFile
$GmailUserName = Read-Host "Please enter your Gmail Account without ""@gmail.com"" "           # Without @gmail.com
$Password = Read-Host "Please enter your Gmail Password to be encrypted " -AsSecureString | ConvertFrom-SecureString -key $Key | Out-File $PasswordFile
#################################### First 1 Step ###########################################

#################################### Second 2 Step ##########################################
# We send the email with our encrypted Credentials
# Nous envoyons le courrier électronique avec les informations d'identification cryptés
#############################################################################################
$AppData = [Environment]::GetFolderPath('ApplicationData')
$PasswordFile = $AppData+"\Password.txt"
$keyFile = $AppData+"\AES.Key"
$key = Get-Content $KeyFile
$GmailEncryptedPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $key
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList($GmailUserName,$GmailEncryptedPassword)
$EmailFrom = $GmailUserName+"@gmail.com"
$EmailTo = $EmailFrom
$Subject = "Sending E-mail with SSL Authentification with an Hybrid code Batch and Powershell Script"
$Body = (Get-Date -format F)  + "  Hello ! the sending email is working now with PowerShell and Batch Script!"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer,587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = $Credentials
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
################################################################################################
Function Show-BalloonTip {
  [CmdletBinding(SupportsShouldProcess = $true)]
  param (
    [Parameter(Mandatory=$true)]$Text,
    [Parameter(Mandatory=$true)]$Title,   
    [ValidateSet('None', 'Info', 'Warning', 'Error')]$Icon = 'Info',
    $Timeout = 10000
  )
  Add-Type -AssemblyName System.Windows.Forms

  if ($script:balloon -eq $null) { $script:balloon = New-Object System.Windows.Forms.NotifyIcon }

  $path                    = Get-Process -id $pid | Select-Object -ExpandProperty Path
  $balloon.Icon            = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
  $balloon.BalloonTipIcon  = $Icon
  $balloon.BalloonTipText  = $Text
  $balloon.BalloonTipTitle = $Title
  $balloon.Visible         = $true
  $balloon.ShowBalloonTip($Timeout)
  Start-Sleep -s 3
  $balloon.Dispose()
}
################################################################################################
Show-BalloonTip -Text 'E-mail was sent, check your email' -Title 'E-mail was sent, check your email !' -Icon 'Info'

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: How to catch error while sending an email with an hybrid code batch and powershell ?

#2 Post by Hackoo » 01 Oct 2017 14:09

Hi :D
I think that i got it :lol: :wink: here the working code, all the comments are welcome for improving this script :wink:
For whom want to test this hybrid code, just copy and save this piece of code as Gmail_Batch_PS_Sender.bat
So, when you execute it, the script asked you to type your Gmail Account without "@gmail.com" and its password for sending emails, just all :lol:

Gmail users can access their account on the official website or by using first-party or third-party apps and services instead. A first party app is for instance Google's official Gmail app for Android, while Thunderbird and the mail client app of Windows 8 are third-party apps.

Google announced back in April 2014 that it would improve the sign-in security of its services and affect any application sending usernames and passwords to the company.

The company suggested to switch to OAuth 2.0 back then but did not enforce it up until now.

If you open the new less secure apps page under security settings on Google, you will notice that Google has disabled access by default.

Note: You see the page only if you are not using Google Apps or have enabled two-factor authentication for the account.

You can flip the switch here to enable less secure applications again so that access is regained.

Image

Code: Select all

<# : Batch portion
@rem # The previous line does nothing in Batch, but begins a multiline comment block
@rem # in PowerShell.  This allows a single script to be executed by both interpreters.
@echo off & Mode 100,5 & color 0A
Title Sending E-mail with SSL Authentification with an Hybrid code Batch and Powershell Script by Hackoo
echo(
rem # This a Powershell command executes the hybrid portion at the bottom of this script
for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0}|out-string)"') do set "%%I"
exit /b
rem # End multi-line PowerShell comment block.  Begin PowerShell scripting.
: end Batch / begin PowerShell hybrid code #>
#################################### First 1 Step ###########################################
# First Step we encrypt the Plain Text Password to an encrypted one using the key AES.key
# Première étape, nous cryptons le mot de passe en clair vers un mot de passe chiffré
# à l'aide de la clé AES.key
$AppData = [Environment]::GetFolderPath('ApplicationData')
$KeyFile = $AppData+"\AES.key"
$Key = New-Object Byte[] 32   # You can use 16, 24, or 32 for AES
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file $KeyFile
$AppData = [Environment]::GetFolderPath('ApplicationData')
$PasswordFile = $AppData+"\Password.txt"
$Key = Get-Content $KeyFile
$GmailUserName = Read-Host "Please enter your Gmail Account without ""@gmail.com"" "
$Password = Read-Host "Please enter your Gmail Password to be encrypted " -AsSecureString `
| ConvertFrom-SecureString -key $Key | Out-File $PasswordFile
#################################### First 1 Step ###########################################

#################################### Second 2 Step ##########################################
# We send the email with our encrypted Credentials
# Nous envoyons le courrier électronique avec les informations d'identification cryptés
#############################################################################################
Function Show-BalloonTip {
  [CmdletBinding(SupportsShouldProcess = $true)]
  param (
    [Parameter(Mandatory=$true)]$Text,
    [Parameter(Mandatory=$true)]$Title,   
    [ValidateSet('None', 'Info', 'Warning', 'Error')]$Icon = 'Info',
    $Timeout = 10000
  )
  Add-Type -AssemblyName System.Windows.Forms

  if ($script:balloon -eq $null) { $script:balloon = New-Object System.Windows.Forms.NotifyIcon }

  $path                    = Get-Process -id $pid | Select-Object -ExpandProperty Path
  $balloon.Icon            = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
  $balloon.BalloonTipIcon  = $Icon
  $balloon.BalloonTipText  = $Text
  $balloon.BalloonTipTitle = $Title
  $balloon.Visible         = $true
  $balloon.ShowBalloonTip($Timeout)
  Start-Sleep -s 10
  $balloon.Dispose()
}
################################################################################################
function Show-Message {

param (
    [string]$Message = "Veuillez entrer votre message",
    [string]$Titre = "Titre de la fenêtre",
    [switch]$OKCancel,
    [switch]$AbortRetryIgnore,
    [switch]$YesNoCancel,
    [switch]$YesNo,
    [switch]$RetryCancel,
    [switch]$IconErreur,
    [switch]$IconQuestion,
    [switch]$IconAvertissement,
    [switch]$IconInformation
    )

# Affecter la valeur selon le type de boutons choisis
if ($OKCancel) { $Btn = 1 }
elseif ($AbortRetryIgnore) { $Btn = 2 }
elseif ($YesNoCancel) { $Btn = 3 }
elseif ($YesNo) { $Btn = 4 }
elseif ($RetryCancel) { $Btn = 5 }
else { $Btn = 0 }

# Affecter la valeur pour l'icone
if ($IconErreur) {$Icon = 16 }
elseif ($IconQuestion) {$Icon = 32 }
elseif ($IconAvertissement) {$Icon = 48 }
elseif ($IconInformation) {$Icon = 64 }
else {$Icon = 0 }
   
# Charger la biblithèque d'objets graphiques Windows.Forms
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null

# Afficher la boite de dialogue et renvoyer la valeur de retour (bouton appuyé)
$Reponse = [System.Windows.Forms.MessageBox]::Show($Message, $Titre , $Btn, $Icon)
Return $Reponse
}
################################################################################################
$SuccessMsg = "The email was sent successfully ; Please, check your email !"
$FailureMsg = "ERROR occurred while sending the email"
$AppData = [Environment]::GetFolderPath('ApplicationData')
$PasswordFile = $AppData+"\Password.txt"
$keyFile = $AppData+"\AES.Key"
$key = Get-Content $KeyFile
$GmailEncryptedPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $key
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList($GmailUserName,$GmailEncryptedPassword)
$EmailFrom = $GmailUserName+"@gmail.com"
$EmailTo = $EmailFrom
$Subject = "Sending E-mail with SSL Authentification with an Hybrid code Batch and Powershell Script"
$Body = (Get-Date -format F)  + "  Hello ! the sending email is working now with PowerShell and Batch Script!"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer,587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = $Credentials
try
{
  $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
  Show-Message -Message $SuccessMsg -Titre $SuccessMsg -IconInformation
  Show-BalloonTip -Text $SuccessMsg -Title $SuccessMsg -Icon 'Info'
}
catch
{
  Show-Message -Message $_.Exception.Message -Titre $FailureMsg -IconErreur
  Show-BalloonTip -Text $_.Exception.Message -Title 'ERROR occurred while sending the email' -Icon 'Error'
}
exit(1)

Post Reply