PoshCode Logo PowerShell Code Repository

Read Gmail POP by s0261001 4 months ago (modification of post by Will Steele view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6650"></script>download | new post

This script is a proof of concept. Further work needs to be done. It requires the user to enter a valid username and password for a gmail.com account. It then attempts to form an SSL connection with the server, and, retrieve the first email. Unfortunately it returns random results. Perhaps someone can improve upon it with some more sockets knowledge than I have.

  1. <#
  2.         .AUTHOR
  3.                 Will Steele (wlsteele@gmail.com)
  4.  
  5.         .DEPENDENCIES
  6.                 Powershell v2 (for Out-Gridview cmdlet)
  7.  
  8.         .DESCRIPTION
  9.                 This script is a proof of concept. Further work needs to be done.  It
  10.                 requires the user to enter a valid username and password for a gmail.com
  11.                 account.  It then attempts to form an SSL connection with the server, and,
  12.                 retrieve the first email. Unfortunately it returns random results.  Perhaps
  13.                 someone can improve upon it.
  14.        
  15.         .EXAMPLE
  16.                 Get-Gmail -username 'you@gmail.com' -password '\/0u|>p@55w0rd'
  17.        
  18.         .EXTERNALHELP
  19.                 None.
  20.                
  21.         .FORWARDHELPTARGETNAME
  22.                 None.
  23.                
  24.         .INPUTS
  25.                 System.Object
  26.                
  27.         .LINK
  28.                 http://learningpcs.blogspot.com/2012/01/powershell-v2-read-gmail-more-proof-of.html.
  29.                
  30.         .NAME
  31.                 Get-Gmail.ps1
  32.                
  33.         .NOTES
  34.                 The script is further explored to find 'From' addressses in the link above.
  35.                 I have piped the $str variable to Out-Gridview. This can be changed to something
  36.                 more suitable for real processing.
  37.                
  38.         .OUTPUTS
  39.                 System.String
  40.                
  41.         .PARAMETER username
  42.                 A required parameter for a valid gmail email user. Use the whole string.
  43.                
  44.         .PARAMETER password
  45.                 A required parameter for the account password.
  46.        
  47.         .SYNOPSIS
  48.                 Read .
  49. #>
  50.  
  51. [CmdletBinding()]
  52. param(
  53.         [Parameter(
  54.                 Mandatory = $true,
  55.                 Position = 0,
  56.                 ValueFromPipeline = $true
  57.         )]
  58.         [ValidateNotNullOrEmpty()]
  59.         [String]
  60.         $username,
  61.        
  62.         [Parameter(
  63.                 Mandatory = $true,
  64.                 Position = 1,
  65.                 ValueFromPipeline = $true
  66.         )]
  67.         [ValidateNotNullOrEmpty()]
  68.         [String]
  69.         $password
  70. )
  71.  
  72. Clear-Host
  73.  
  74. try {
  75.         Write-Output "Creating new TcpClient."
  76.         $tcpClient = New-Object -TypeName System.Net.Sockets.TcpClient
  77.        
  78.         # Connect to gmail
  79.         $tcpClient.Connect("pop.gmail.com", 995)
  80.        
  81.         if($tcpClient.Connected) {
  82.                 Write-Output "You are connected to the host. Attempting to get SSL stream."
  83.                
  84.                 # Create new SSL Stream for tcpClient
  85.                 Write-Output "Getting SSL stream."
  86.                 [System.Net.Security.SslStream] $sslStream = $tcpClient.GetStream()
  87.                
  88.                 # Authenticating as client
  89.                 Write-Output "Authenticating as client."               
  90.                 $sslStream.AuthenticateAsClient("pop.gmail.com");
  91.                
  92.                 if($sslStream.IsAuthenticated) {
  93.                         Write-Output "You have authenticated. Attempting to login."
  94.                         # Asssigned the writer to stream
  95.                         [System.IO.StreamWriter] $sw = $sslstream
  96.                        
  97.                         # Assigned reader to stream
  98.                         [System.IO.StreamReader] $reader = $sslstream
  99.                        
  100.                         # refer POP rfc command, there very few around 6-9 command
  101.                         $sw.WriteLine("USER $username")
  102.                        
  103.                         # sent to server
  104.                         $sw.Flush()
  105.                        
  106.                         # send pass
  107.                         $sw.WriteLine("PASS $password");
  108.                         $sw.Flush()
  109.                        
  110.                         # this will retrive your first email
  111.                         $sw.WriteLine("RETR 1")
  112.                         $sw.Flush()
  113.                        
  114.                         $sw.WriteLine("Quit ");
  115.                         $sw.Flush();
  116.                        
  117.                         [String] $str = [String]::Empty
  118.                         [String] $strTemp = [String]::Empty
  119.  
  120.                         while (($strTemp = $reader.ReadLine()) -ne $null) {
  121.                                 # find the . character in line
  122.                                 if($strTemp -eq '.') {
  123.                                         break;
  124.                                 }
  125.  
  126.                                 if ($strTemp.IndexOf('-ERR') -ne -1) {
  127.                                         break;
  128.                                 }
  129.                                        
  130.                                 $str += $strTemp;
  131.                         }
  132.                        
  133.                         # Return raw data
  134.                         Write-Output "`nOutput email"
  135.                         $str | Out-GridView
  136.                 } else {
  137.                         Write-Error "You were not authenticated. Quitting."
  138.                 }
  139.                
  140.         } else {
  141.                 Write-Error "You are not connected to the host. Quitting"
  142.         }
  143. }
  144.  
  145. catch {
  146.         $_
  147. }
  148.  
  149. finally {
  150.         Write-Output "Script complete."
  151. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me