8 Ocak 2015 Perşembe

Powershell: Email Sending Tester (with Gmail example)

MailSendingTest01

Powershell ile eposta gönderilebilir ve eposta sunucuları test edebilir. Basit olarak Powershell ile eposta göndermek için aşağıdaki kod yeterlidir.

 






# ════════════════════════════════ ════════════════════════════════$SMTPServer = "smtp.gmail.com"
$SMTPPort = 587
$From = "siberblogorg@gmail.com"
$Password = "Abcd.12345"
$To = "siberblogorg@gmail.com"
$Subject = "Email Subject"
$Body = "Insert body text here"
$Priority = "High"$SecPasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($From, $SecPasswd)Send-MailMessage -From $From -to $To -Subject $Subject -Priority $Priority `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $Cred -ErrorAction Stop# ════════════════════════════ ════════════════════════════════════

 

 

Bu kodları kullanarak daha gelişmiş bir script yazılmıştır.

Script iki şekilde kullanılabilir.
a) Script çalıştırıldıktan sonra degerler girilir. Bunun için scripti doğrudan çalıştırabilirsiniz. Komut satırından çalıştırmak için,

Örnek: Powershell.exe -ExecutionPolicy Bypass -File "d:\MailSendingTest_v2.ps1"

b) Değerler script içinde tanımlanır. Bunun için scripti çalıştırıken herhangi bir parametre girmelisiniz. Komut satırından çalıştırmak için,

Örnek: Powershell.exe -ExecutionPolicy Bypass -File "d:\MailSendingTest_v2.ps1" default ‎

 

1- Scripti komut satırından veya Powershell içinden çalıştırın. Powershell içinden parametresiz çalıştırdığımızı varsayalım. Bizden sıra ile MailServer, Port, From, To, Subject ve Body bilgisinin girilmesini isteyecektir.

MailSendingTest02

 

2- Bilgiler girildikten sonra eposta gönderme işlemi başarılı ise "Gönderme BAŞARILI" mesajını alacaksınız. Eposta sunucusundan kontrol ettiğinizde aşağıdaki gibi bir mail göreceksiniz.

MailSendingTest03

 

3- Eposta sunucuya ait girilen (Domain name, port) bilgilerle suucuya erişim kontrol edilecektir. Erişim kontrolü sonucu hata var ise "Erişim başarısız" mesajını alacaksınız ve script sonlandırılacaktır.

MailSendingTest04

 

4- Script komut satırından çalıştırmak için öncelikle cmd.exe 'yi yönetici olarak çalıştırın sonra scripti çalıştırın. Örneğimizde script içinde hazır girilmiş olan bilgilerle eposta testi yapacağımız için scripti parametre ile çalıştırıyoruz.

Örn: Powershell.exe -ExecutionPolicy Bypass -File "d:\MailSendingTest_v2.ps1" default

MailSendingTest05

 

Parametre kullanıldığı için script içerisinde yazan bilgilerle eposta gönderilecektir. Bu seçeneği kullanmak için script içerisinde güncelleme yapınız.

MailSendingTest06

 

5- E-posta gönderimi başarılı ise "Gönderme BAŞARILI" şeklinde mesaj alınır.

6- Script parametresiz olarak çalıştırıldığında bilgiler sıra ile girilir. Gönderen bilgisi (From) girildikten sonra parola giriş ekranı gelmektedir. Daha sonra gönderme işlemi yapılır.

 

7- Eposta gönderilme işlemi başarısız ise 3 kez tekrarlanır. Her tekrarda karşılaşılan hata mesajı görüntülenir. Yanlış parola girilmesi sonucunu ekran görüntüsünde görüleceği üzere "... client was not authenticated." hatası alınmıştır.

MailSendingTest01

 

NOT: Eposta bilgileri doğru olduğu halde eposta gönderilemiyor ise farklı nedenler olabilir. Örneğin Gmail için aşağıdaki ayarı etkinleştirmeniz gerekmektedir.

MailSendingTest08

 

 

Script download: MailSendingTest_v2.rar

 

Kaynak Kod:






# ================================= ===================================# NAME: MailSendingTest_v2.ps1
# AUTHOR: Bekir Yalçın - SiberBlog.org
# DATE: 08/01/2015
# LINK: http://siberblog.org/index.php/powershell-email-sending-tester-with-gmail-example/
# COMMENT: Mail Sender Test - E-Posta gönderme testi
# : Scrit iki şekilde çalıştılabilir.
# a) Script çalıştırıldıktan sonra degerler girilir. Bunun için scripti doğrudan
# çalıştırabilirsiniz. Komut satırından çalıştırmak için örnek:
# Powershell.exe -ExecutionPolicy Bypass -File "d:\MailSendingTest_v2.ps1"
# b) Değerler script içinde tanımlanır. Bunun için scripti çalıştırıken herhangi bir
# parametre girmelisiniz. Komut satırından çalıştırmak için örnek:
# Powershell.exe -ExecutionPolicy Bypass -File "d:\MailSendingTest_v2.ps1" default
# ================================== ==================================param( [string]$Arg = "Empty" )# Clear$ErrorActionPreference = "SilentlyContinue"
$Exit = 0
$socket = $null
$Cred = $null
$SMTPServer = $null
$SMTPPort = $null
$From = $null
$Password = $null
$To = $null
$Subject = $null
$Body = $null
$Priority = $null
# $Attachments = $null
# $Cc = $null# ==================

Write-Host "`n ╔═══════════════════════ ═════════════════════╗" -ForegroundColor Green
Write-Host " ║ Mail Sender Test - E-Posta Gönderme Testi ║" -ForegroundColor Green
Write-Host " ╚═════════════════════════ ═══════════════════╝`n" -ForegroundColor Green

# ==================

if ($Arg -ne "Empty")
{
# Default degerler
# ==================
$SMTPServer = "smtp.gmail.com"
$SMTPPort = 587
$From = "siberblogorg@gmail.com"
$Password = "Abcd.12345"
$To = "siberblogorg@gmail.com"
$Subject = "Email Subject"
$Body = "Insert body text here"
$Priority = "High"
# $Attachments = "C:\a.txt"
# $Cc = "siberblogorg@gmail.com"
# ==================
Write-Host " Mail Server : " $SMTPServer
Write-Host " Port : " $SMTPPort
Write-Host " From : " $From
Write-Host " Password : " $Password
Write-Host " To : " $To
Write-Host " Subject : " $Subject
Write-Host " Body : " $Body
Write-Host " Priority : " $Priority
# Write-Host " Attachments : " $Attachments
# Write-Host " Cc : " $Cc

# $Cred = Get-Credential -Message "Password:" -UserName $From
$SecPasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($From, $SecPasswd)
}
else
{
$SMTPServer = Read-Host -Prompt " Mail Server (Örn:smtp.gmail.com) "
$SMTPPort = Read-Host -Prompt " Port (Örn1:465, Örn2:587) "

#Create object for connecting to port on computer
$tcpobject = New-Object -TypeName System.Net.Sockets.TcpClient

#Connect to remote machine's port
$connect = $tcpobject.BeginConnect($SMTPServer,$SMTPPort,$null,$null)

#Configure a timeout before quitting
$socket = $connect.AsyncWaitHandle.WaitOne(5000,$false)

If($socket)
{
Write-Host "`n IP&Port Test: " -ForegroundColor White -NoNewline
Write-Host "$($SMTPServer):$($SMTPPort) " -ForegroundColor Green -NoNewline
Write-Host "Access successful & Erişim başarılı.`n" -ForegroundColor White
}
else
{
Write-Host "`n IP&Port Test: " -ForegroundColor White -NoNewline
Write-Host "$($SMTPServer):$($SMTPPort) " -ForegroundColor Red -NoNewline
Write-Host "Access failed & Erişim başarısız.`n" -ForegroundColor White
break
}

$From = Read-Host -Prompt " From (Örn:siberblogorg@gmail.com) "
$Cred = Get-Credential -Message " Password - Parolanızı giriniz " -UserName $From
$To = Read-Host -Prompt " To (Örn:siberblogorg@gmail.com) "
$Subject = Read-Host -Prompt " Subject "
$Body = Read-Host -Prompt " Body "
$Priority = "High"
# $Attachments = Read-Host -Prompt " Attachments "
# $Cc = Read-Host -Prompt " Cc "
# $Attachments = "C:\a.txt"
# $Cc = "siberblogorg@gmail.com"
}
# ==================
Do {
Try {
""
Send-MailMessage -From $From -to $To -Subject $Subject -Priority $Priority `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $Cred -ErrorAction Stop # -Attachments $Attachments -Cc $Cc

$Exit = 3

Write-Host "`n ╔═══════════════════════════════════════════════╗" -ForegroundColor Green
Write-Host " ║ Mail Sender Succesfully - Gönderme BAŞARILI ║" -ForegroundColor Green
Write-Host " ╚═══════════════════════════════════════════════╝`n" -ForegroundColor Green

}
Catch {
$Exit ++
Write-Warning "$($Exit): Failed to send message because: $($_.Exception.Message)"

If ($Exit -eq 3)
{
Write-Host "`n ╔══════════════════════ ═════════════════════════╗" -ForegroundColor Red
Write-Host " ║ Unable To Send Message! - Gönderme BAŞARISIZ ║" -ForegroundColor Red
Write-Host " ╚════════════════════════ ═══════════════════════╝`n" -ForegroundColor Red
}
}

} Until ($Exit -eq 3)

pause

# ================================ ====================================

Script download:  MailSendingTest_v2.rar

 

Zimbra Mail Server için aşağıdaki versiyonu kullanabilirsiniz.

Script download: MailSendingTest_v3.rar

Hiç yorum yok:

Yorum Gönder