Merhaba Arkadaşlar;

Bir zamandan beri sizlere Eğitim Makalesi yayınlamadım umarım sizlere yardımcı olur;

Exchange Server Üzerinde Oluşturulan Yeni Mail Hesabına Otomatik Hoş Geldin Mesaji Gönderme
Microsoft Exchange 2010 versiyonundan itibaren belirli işlemleri, denetimleri “Scripting Agent” yönetimini kullanarak uygulamak mümkün olmuştur.

Bu makalemizde Exchange Control Panel üzerinden yeni bir mail hesabı veya Active Directory’de daha önceden oluşturulmuş bir kullanıcıya mail hesabı oluşturduğumuzda, oluşturmuş olduğumuz mail hesabına otomatik olarak mail gönderimini gerçekleştireceğiz.

Otomatik mail gönderimi için ilk olarak yapmamız gereken işlem “ScriptingAgentConfig.xml” dosyasını hazırlamak. Aşağıdaki kodları kopyalayıp “xml” formatında kaydediyoruz.

<?xml version=”1.0″ encoding=”utf-8″ ?>

<Configuration version=”1.0″>

<Feature Name=”WelcomeEmail” Cmdlets=”New-Mailbox,Enable-Mailbox”>

<ApiCall Name=”OnComplete”>

if($succeeded) {

if ($provisioningHandler.TaskName -eq “New-Mailbox”) {

# Waiting for synchronization after mailbox has been created.

Set-ADServerSettings -ViewEntireForest $true

Import-Module ActiveDirectory

# Uncomment the next line to enable logging – this logs information helpful when debugging/modifying script and associated HTML message

# $logFile = “c:\WelcomeMessage\log.txt”

if ($logFile) { $provisioningHandler.UserSpecifiedParameters | fl | Out-File $logFile -Append }

# Get the mailbox

$attempts = 0

$mbx = $null # Just to be sure…

do {

try

{

$ADUser = $provisioningHandler.UserSpecifiedParameters[“Name”].ToString()

$mbx = Get-Mailbox -Identity $ADUser

}

catch

{

$Error.Clear()

}

if ($mbx -eq $null)

{

Start-Sleep -s 30

$attempts++

}

} while ($mbx -eq $null)

Start-Sleep -s 20

# If we have the mailbox, we can send the introductory email

if ($mbx)

{

# Mailbox does not have first name property, so we need to get the user account

$usr = Get-User $mbx.Identity

# Create the objects we need to create the message and send the mail

$message = New-Object System.Net.Mail.MailMessage

$smtpClient = New-Object System.Net.Mail.SmtpClient(“localhost”) # Change this to point to your SMTP server

# Obtain the message text

$messageText = [string](Get-Content (“c:\WelcomeMessage\Welcome.html”)) # Change to point to your HTML welcome message

$messageText = $messageText.Replace(“#UserFirstName#”, $usr.FirstName) # This replaces #UserFirstName# with the user’s first name – you can add further replacements as needed

# Create the HTML view for this message

$view = [System.Net.Mail.AlternateView]::CreateAlternateViewFromString($messageText, $null, “text/html”)

# Add any linked resources (e.g. images)

$image = New-Object System.Net.Mail.LinkedResource(“C:\WelcomeMessage\image001.png”)

$image.ContentId = “image001.png”

$image.ContentType = “image/png”

$view.LinkedResources.Add($image)

# Add any linked resources (e.g. images)

# Create the message

$message.From = “Administrator@CanberkPiskin.local” # Update this to the address you want the welcome message to be sent from

$message.To.Add($mbx.PrimarySmtpAddress.ToString())

$message.Subject = “Welcome to email!” # Update this to your email subject

$message.AlternateViews.Add($view)

$message.IsBodyHtml = $true

# Send the message

$smtpClient.Send($message)

# Tidy up variables

Remove-Variable image

Remove-Variable view

Remove-Variable messageText

Remove-Variable message

Remove-Variable smtpClient

Remove-Variable usr

Remove-Variable mbx

}

}

if ($provisioningHandler.TaskName -eq “Enable-Mailbox”) {

# Waiting for synchronization after mailbox has been created.

Set-ADServerSettings -ViewEntireForest $true

Import-Module ActiveDirectory

# Uncomment the next line to enable logging – this logs information helpful when debugging/modifying script and associated HTML message

# $logFile = “c:\WelcomeMessage\log.txt”

if ($logFile) { $provisioningHandler.UserSpecifiedParameters | fl | Out-File $logFile -Append }

# Get the mailbox

$attempts = 0

$mbx = $null # Just to be sure…

do {

try

{

if ($provisioningHandler.UserSpecifiedParameters.Contains(“Alias”)) {

$UsrAlias = $provisioningHandler.UserSpecifiedParameters[“Alias”].ToString()

if ($logFile) { [string]::Format(“Retrieving mailbox using alias: {0}”, $UsrAlias) | Out-File $logFile -Append }

$mbx = (Get-Mailbox -Filter {Alias -eq $UsrAlias} -ErrorAction SilentlyContinue)

Remove-Variable UsrAlias

}

if (($mbx -eq $null) -and ($provisioningHandler.UserSpecifiedParameters.Contains(“SamAccountName”)))

{

$UsrSamAccountName = $provisioningHandler.UserSpecifiedParameters[“SamAccountName”].ToString()

if ($logFile) { [string]::Format(“Retrieving mailbox using SamAccountName: {0}”, $UsrSamAccountName) | Out-File $logFile -Append }

$mbx = (Get-Mailbox -Filter {SamAccountName -eq $UsrSamAccountName} -ErrorAction SilentlyContinue)

Remove-Variable UsrSamAccountName

}

if (($mbx -eq $null) -and ($provisioningHandler.UserSpecifiedParameters.Contains(“Identity”)))

{

$UsrIdentity = $provisioningHandler.UserSpecifiedParameters[“Identity”].ToString()

if ($logFile) { [string]::Format(“Retrieving mailbox using identity: {0}”, $UsrIdentity) | Out-File $logFile -Append }

$mbx = (Get-Mailbox -Identity $UsrIdentity -ErrorAction SilentlyContinue)

Remove-Variable UsrIdentity

}

}

catch

{

$Error.Clear()

}

if ($mbx -eq $null)

{

Start-Sleep -s 10

$attempts++

}

} while (($attempts -lt 3) -and ($mbx -eq $null))

# If we have the mailbox, we can send the introductory email

if ($mbx)

{

# Mailbox does not have first name property, so we need to get the user account

$usr = Get-User $mbx.Identity

# Create the objects we need to create the message and send the mail

$message = New-Object System.Net.Mail.MailMessage

$smtpClient = New-Object System.Net.Mail.SmtpClient(“localhost”) # Change this to point to your SMTP server

# Obtain the message text

$messageText = [string](Get-Content (“c:\WelcomeMessage\Welcome.html”)) # Change to point to your HTML welcome message

$messageText = $messageText.Replace(“#UserFirstName#”, $usr.FirstName) # This replaces #UserFirstName# with the user’s first name – you can add further replacements as needed

# Create the HTML view for this message

$view = [System.Net.Mail.AlternateView]::CreateAlternateViewFromString($messageText, $null, “text/html”)

# Add any linked resources (e.g. images)

$image = New-Object System.Net.Mail.LinkedResource(“C:\WelcomeMessage\image001.png”)

$image.ContentId = “image001.png”

$image.ContentType = “image/png”

$view.LinkedResources.Add($image)

# Create the message

$message.From = “Administrator@CanberkPiskin.local” # Update this to the address you want the welcome message to be sent from

$message.To.Add($mbx.PrimarySmtpAddress.ToString())

$message.Subject = “Welcome to email!” # Update this to your email subject

$message.AlternateViews.Add($view)

$message.IsBodyHtml = $true

# Send the message

$smtpClient.Send($message)

# Tidy up variables

Remove-Variable image

Remove-Variable view

Remove-Variable messageText

Remove-Variable message

Remove-Variable smtpClient

Remove-Variable usr

Remove-Variable mbx

}

}

}

</ApiCall>

</Feature>

</Configuration>

XML dosyasını kaydettikten sonra xml içerisinde aşağıda belirtmiş olduğum değerleri kendi yapımıza göre düzenliyoruz.

$messageText = [string](Get-Content (“c:\WelcomeMessage\Welcome.html”)) (Bu komut içerisinde de html formatta ki mail içeriğinin dosya yolunu belirtiyoruz.)

$smtpClient = New-Object System.Net.Mail.SmtpClient(“localhost”) (Mailin hangi sunucu üzerinden gönderileceğini belirliyoruz.)

$image = New-Object System.Net.Mail.LinkedResource(“C:\WelcomeMessage\image001.png”) (Mail içerisinde yer alacak olan resim dosyasının yolunu belirliyoruz.)

$image.ContentId = “image001.png”

$image.ContentType = “image/png” (Ekleyeceğimiz resim png formatından farklı ise belirlemiş olduğumuz uzantıyı
$image.ContentType değişkeni içerisinde belirliyoruz.

$message.From = “Administrator@Canberk.Piskin.local” (Yeni açılan mail hesabına hangi mail adresi üzerinden mail göndereceğimizi belirliyoruz.)

Bu değişlikleri yaptıktan sonra varsayılan olarak “C:\Program Files\Microsoft\Exchange Server\V15\Bin\CmdletExtensionAgents” dizini içerisine kopyalıyoruz.

HTML ve XML dosyalarını ilgili klasörlere kopyaladıktan sonra Exchange Management Shell’i açıyoruz ve aşağıdaki powershell komutu çalıştırıyoruz.

Enable-CmdletExtensionAgent “Scripting Agent”

Artık Exchange Control Panel üzerinden oluşturduğumuz her bir yeni mail hesabına otomatik olarak hoş geldin mesajını göndermiş bulunmaktayız.

Bir sonraki eğitim makalemde görüşmek üzere.