Source Code and Samples

Friday, 2 February 2007

Check for POP3 Messages

Description

A great script from Paul Samways which allows you to check how many POP3 messages are in a mailbox.

Source Code

param ($Server, $Username, $Password);
$UsageScript = "Usage: getMail.msh [Server] [Username] [Password]";
$Port = 110;
if (($Server.length -lt 1) -or ($Username.length -lt 1) -or ($Password.length -lt 1)) {
write-output $UsageScript; break;
}
$TCPConnection = new-object -TypeName System.Net.Sockets.TcpClient($Server, $Port);
$NetStream = $TCPConnection.GetStream();
$Reader = new-object -TypeName System.IO.StreamReader($NetStream);
$Writer = new-object -TypeName System.IO.StreamWriter($NetStream);
$Buffer = $Reader.ReadLine();
$Writer.WriteLine("USER $Username");
$Writer.Flush(); $Buffer = $Reader.ReadLine();
$Writer.WriteLine("PASS $Password");
$Writer.Flush();
if ($Reader.ReadLine() -match "OK")
{
$writer.WriteLine("STAT"); $writer.Flush();
$NumOfMessage = $Reader.ReadLine().SubString(4, 1);
write-output "You Have $NumOfMessage Item(s) on $Server";
}
else
{
write-output "Authentication Error";
}
$Reader.Dispose();
$Writer.Dispose();
$NetStream.Dispose();
$TCPConnection.Close();

No comments: