Technical Article

Send Email to recipients with Attachment using PowerShell

,

This script uses the Exchange snap-ins to send email.

Open Powershell ISE. Copy the script and add the correct email addresses.

Clear

Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue

function sendmail_withAttachment {
Param (

#[Parameter(Mandatory=$true)]
[string] $smtpServer = "",

[string] $From_mail = "",

[string] $to_mail = "",,

[string] $cc_mail = "",,

[string] $Subject = "Notification from email server"
)


Write-Host “`n Starting script, Sending Email to Recipients.....” -ForegroundColor Green

$text= ''
$body = ''


$text = "Attached is the email server mailbox report"

$body = "<font color="red"><b>$text</b></font><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"

$body += "--------------------------------------------------------------------------------------------------------"

 
$body += "<footer><p><font size="2">Contact DBA Team if you have any question.<Br>--------------------------------------------------------------------------------------------------------</footer></font><br>" 


$filename = "sqllog.txt"

$file= "C:\Folder\" + $filename


$att = new-object Net.Mail.Attachment($file)


$msg = new-object Net.Mail.MailMessage


$smtp = new-object Net.Mail.SmtpClient($smtpServer)

#$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody) 

$msg.IsBodyHTML = $true

$msg.Body = $body 

$msg.From = $From_mail;


$msg.To.Add($to_mail)


$msg.CC.Add($cc_mail)

$msg.Subject = $Subject;

$logs = Get-Content $file | Out-String


$msg.Attachments.Add($att)


$smtp.Send($msg)
#--Priority high 

$att.Dispose()
}

sendmail_withAttachment

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating