Skip to content

Instantly share code, notes, and snippets.

@ser1zw
Created May 25, 2015 09:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ser1zw/a6292f382a4b9dca2fe4 to your computer and use it in GitHub Desktop.
Save ser1zw/a6292f382a4b9dca2fe4 to your computer and use it in GitHub Desktop.
E-mail tool in PowerShell
################################################################################
# E-mail tool in PowerShell
#
# 0. Set execution policy to "RemoteSighed" in PowerShell
# Set-ExecutionPolicy RemoteSigned
#
# 1. Right click this file and select "Run with PowerShell" from context menus.
# Or run the following command in cmd.exe.
# powershell -Sta -File send-message.ps1
#
# 2. Drag & drop *.eml files to send.
#
# 3. Configure "Server", "Port", "MAIL FROM", "RCPT TO".
#
# 4. Click [Send] button.
################################################################################
Add-Type -AssemblyName PresentationFramework
$encoding = New-Object System.Text.AsciiEncoding
Function SendCommand($stream, $writer, $command) {
# Send command
foreach ($line in $command) {
$writer.WriteLine($line)
}
$writer.Flush()
Start-Sleep -m 100
# Get response
$buff = New-Object System.Byte[] 4096
$output = ""
while ($True) {
$size = $stream.Read($buff, 0, $buff.Length)
if ($size -gt 0) {
$output += $encoding.GetString($buff, 0, $size)
}
if (($size -lt $buff.Length) -or ($size -le 0)) {
break
}
}
if ([int]::Parse($output[0]) -gt 3) {
throw $output
}
$output
}
Function SendMessage($server, $port, $mailfrom, $rcptto, $filename) {
try {
$socket = New-Object System.Net.Sockets.TcpClient($server, $port)
$stream = $socket.GetStream()
$stream.ReadTimeout = 1000
$writer = New-Object System.IO.StreamWriter $stream
$endOfMessage = "`r`n."
SendCommand $stream $writer ("EHLO " + $server)
SendCommand $stream $writer ("MAIL FROM: <" + $mailfrom + ">")
SendCommand $stream $writer ("RCPT TO: <" + $rcptto + ">")
SendCommand $stream $writer "DATA"
$content = (Get-Content $filename) -join "`r`n"
SendCommand $stream $writer ($content + $endOfMessage)
SendCommand $stream $writer "QUIT"
}
finally {
if ($writer -ne $Null) {
$writer.Close()
}
if ($socket -ne $Null) {
$socket.Close()
}
}
}
Function Main() {
[Xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SendMessage" Height="400" Width="360" AllowDrop="True">
<StackPanel Orientation="Vertical" Margin="3">
<Grid Margin="10" Height="120">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label VerticalAlignment="Top">Server:</Label>
<TextBox Name="server" Text="localhost" VerticalAlignment="Top" Grid.Column="1" />
<Label VerticalAlignment="Top" Margin="0" Grid.Row="1">Port:</Label>
<TextBox Name="port" Text="25" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1"/>
<Label VerticalAlignment="Top" Grid.Row="2">MAIL FROM:</Label>
<TextBox Name="mailFrom" Text="from@example.com" VerticalAlignment="Top" Grid.Row="2" Grid.Column="1"/>
<Label VerticalAlignment="Top" Grid.Row="3">RCPT TO:</Label>
<TextBox Name="rcptTo" Text="to@example.com" VerticalAlignment="Top" Grid.Row="3" Grid.Column="1"/>
</Grid>
<Label Width="Auto">Files:</Label>
<ListBox Name="files" Width="Auto" Height="Auto" MinHeight="120"></ListBox>
<Button Name="send" Content="Send" />
<Button Name="clear" Content="Clear" />
</StackPanel>
</Window>
"@
$reader = New-Object System.Xml.XmlNodeReader $xaml
$window = [Windows.Markup.XamlReader]::Load($reader)
$textbox = $window.FindName("server")
$listBox = $window.FindName("files")
$sendButton = $window.FindName("send")
$sendButton.add_Click.Invoke({
$server = $window.FindName("server").Text
$port = $window.FindName("port").Text
$mailfrom = $window.FindName("mailFrom").Text
$rcptto = $window.FindName("rcptTo").Text
foreach ($filename in $listBox.ItemsSource) {
try {
SendMessage $server $port $mailfrom $rcptto $filename
}
catch [Exception] {
[System.Windows.MessageBox]::Show($Error[0], "Error")
}
}
})
$clearButton = $window.FindName("clear")
$clearButton.add_Click.Invoke({
$listBox.ItemsSource = @()
})
$listBox.ItemsSource = @()
$window.add_Drop({
foreach ($file in $_.Data.GetFileDropList()) {
Write-Host $file
if ($file -is [IO.FileInfo]) {
$listBox.ItemsSource += $file
}
else {
$listBox.ItemsSource += dir $file -recurse | ? { $_ -is [IO.FileInfo] } | % { $_.FullName }
}
}
})
$window.ShowDialog() | Out-Null
}
$execState = [Threading.Thread]::CurrentThread.GetApartmentState()
if ($execState -eq "STA") {
Main | Out-Null
}
else {
powershell -Sta -File $MyInvocation.MyCommand.Path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment