Read gmail emails from powershell

1

Good Day

I have been developing a script in Powershell that signs in gmail with the module Gmail.ps and reads the message of a specific email.

Of the emails that I have in the tray I have only been able to capture the subject that I need but do not enter the (Body) message of that subject, is there any way to solve this problem?

I appreciate very much that you can help me.

# Obtiene credenciales seguras del correo electrónico
$SecurePassword = ConvertTo-SecureString -String (Unprotect-String -ProtectedString $PassEmail) -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Email, $SecurePassword

# Entra a gmail para leer el correo
$Gmail = New-GmailSession -Credential $Credentials # Crea una sesión de email

$Subject = "VEE - Nuevo registro de usuario" # Asunto del correo

$Inbox = $Gmail | Get-Mailbox # obtenga la bandeja de entrada
$Sbjt = $Inbox | Get-Message -Unread | Select-Object 'Subject' | Where-Object {$_.Subject -ccontains $Subject} 
    
asked by JuliethB 12.01.2018 в 19:36
source

1 answer

0

I could not reproduce the problem, because after having the module installed it generates an error when trying to start the Gmail session:

$Gmail = New-GmailSession

The error:

New-Object : Exception calling ".ctor" with "7" argument(s): "* NO [WEBALERT https://accounts.google.com/signin/continu
e?sarp=1&scc=1&plt=AKgnsbs6Bl94pIFbWWVYrDHhyJ6Cn280LgWptlpK-60A5UkvPSPgLPug5XTffRxaWQE1H4yU1TlzU8HVOAvcx4AdNQXCRmwbFELE
FeoYwTblvDWfE0CJk1h3xVYGkSSjDTBgHXWZfuUpRrs2GHxLee00EZCwVf7e7uFKk9Hp8sc4UIF5115fzjLxEJP82iRP6VLBnedGj3wAvpmzYQp2bfqT7UQ
Y_DJatBo4BFphEtuPevaGzw1MejA] Web login required."
At C:\Users\vmsilvamolina\Documents\WindowsPowerShell\Modules\Gmail.ps\Gmail.ps.psm1:16 char:16
+ ...  $session = New-Object -TypeName AE.Net.Mail.ImapClient -ArgumentList ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

But what I could find in the documentation of the module is the following:

  

Receive-Message

     

Fetches the whole message from the server (including   the body and the attachments).

What should solve your problem, using it in the following way:

$Subject = "VEE - Nuevo registro de usuario" # Asunto del correo

$Inbox = $Gmail | Get-Mailbox # obtenga la bandeja de entrada
$Sbjt = $Inbox | Get-Message -Unread | Select-Object 'Subject' | Where-Object {$_.Subject -ccontains $Subject} | Receive-Message
$Msge = $Sbjt.Body
    
answered by 13.01.2018 в 19:38