4

I am trying to send an email with High Importance using Powershell. But when mail comes to inbox it does not mark with High Importance.

Following is email script:

$EmailFrom = "monitoring@mydomainname.no"
$EmailTo = "fatherazrael@tcs.com"
$Subject = "Disk Space Low: $server"

$Body = "Server Name:  $server, <NEED NEW LINE> Drive: C,  <NEED NEW LINE>  Total Size: $sizeGB,  <NEED NEW LINE> Space Left: $freeSpaceGB"

$SMTPServer = "scan.opinergo.fn"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("<From mail ID>", "Password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)   

For High Importance i have added following but none is working when mail comes to outlook:

1) $EmailPriority = [System.Net.Mail.MailPriority]::High

Mail Comes but no importance

2) $Priority = [System.Net.Mail.MailPriority]::High

Mail Comes but no importance

3) $Priority = "high"

Mail Comes but no importance

4) $EmailPriority = "high"

Mail Comes but no importance

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
fatherazrael
  • 5,511
  • 16
  • 71
  • 155
  • how do you pass the `$EmailPriority` variable to the SMTPClient? – Gerald Schneider Nov 17 '15 at 07:44
  • I never used powershell to send mails, but from skimming over the docs I guess you have to build a message using the [MailMessage class](https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage(v=vs.110).aspx) and set the appropiate headers there. – Gerald Schneider Nov 17 '15 at 07:48
  • Possible duplicate of [Can I mark an Email as "High Importance" for Outlook using System.Net.Mail?](http://stackoverflow.com/questions/2692919/can-i-mark-an-email-as-high-importance-for-outlook-using-system-net-mail) – sodawillow Nov 17 '15 at 07:54
  • @Gerald Schneider: Simply just like other parameter shown in snippet – fatherazrael Nov 17 '15 at 09:18

3 Answers3

12

This code would send out a high priority email:

  $smtp = new-object Net.Mail.SmtpClient("yourDomain.com") 
  $email = New-Object System.Net.Mail.MailMessage
  $email.From = "fromEmail@email.com"
  $email.To.Add("ToEmail@email.com")
  $email.Subject = "Enter  your Email Subject"
  $email.Body = "Enter the body of your email"
  $email.Priority = [System.Net.Mail.MailPriority]::High
  $smtp.Send($email)
user2536008
  • 215
  • 1
  • 5
  • 15
8

Try using the Send-MailMessage cmdlet and specify the -Priority parameter:

$password = "pass" | ConvertTo-SecureString -AsPlainText -Force
$credentials = New-Object PSCredential("username", $password)

Send-MailMessage `
    -To recipient@example.com `
    -From sender@example.com `
    -Subject Example `
    -SmtpServer smtp.example.com `
    -Credential $credentials
    -Priority High
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
1

$emailMessage.Priority = 2

$emailSmtpServer = “xyz”
$emailSmtpServerPort = “587”
$emailSmtpUser = “x@y.z”
$emailSmtpPass = “abc”

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = “x@y.z”
$emailMessage.To.Add( “x1@y.z” )
$emailMessage.To.Add( “x2@y.z” )
$emailMessage.Subject = “EMAIL CHECK!”
$emailMessage.IsBodyHtml = “True”

#Low Priority
#$emailMessage.Priority = 1

#High Priority
$emailMessage.Priority = 2

$emailMessage.Body = @”
    Email body... <br />
    <br />
    <br />
    Message Sent from Power Shell.
“@

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );

$SMTPClient.Send($emailMessage )
Community
  • 1
  • 1
Zunair
  • 1,085
  • 1
  • 13
  • 21