Script encrypted mail - powershell

Please help me with an PowerShell script to send an encrypted email on Outlook application.
I have a PowerShell script to send mail with attachments on Outlook application but want to encrypt that mail.
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "mail"
$Mail.Subject = "Action"
$Mail.Body = "Hi Aviral, Good to see you"
$file = "C:\Users\a.raghorte\Desktop\UNIX\Utilization.txt"
$Mail.Attachments.Add($file)
$Mail.Send()

$Mail.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x6E010003", 0x03)
That should do the trick while Outlook is running. If outlook is not running, the Mail goes to "Outbox" without signature/encryption.
0 = none, 1 = sign, 2 = encrypt, 3 = both

Related

Send mail to Myself using Powershell

I managed to send mail using powershell, but what i want is for the one who runs the script be the one who send it and receive the mail so it should be automatic. and I don't know how i can manage to do that, So I'm asking for your help please.
here is my code
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = #need to be the mail of the one who runs the script
$Mail.Subject = "send mail"
$Mail.Body = "This is my mail"
$Mail.Send()
Write-Host "Mail Sent Successfully"
Adding to Mathias comment, this is working for me;
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.Sender = $Outlook.GetNamespace("MAPI").Accounts.Item(1).SmtpAddress # or 'noreply#domain.com'
$Mail.To = $Outlook.GetNamespace("MAPI").Accounts.Item(1).SmtpAddress
$Mail.Subject = "send mail"
$Mail.Body = "This is my mail"
$Mail.Send()
This is working without a Sender address for me, it uses my default mail profile - but you could try manually specifying that if it's not working for you.
Well I managed to find something that worked for me. all I did is put the $Outlook.GetNamespace("MAPI").Accounts.Item(1).SmtpAddress in a variable and call it after that
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$email = $Outlook.GetNamespace("MAPI").Accounts.Item(1).SmtpAddress
$Mail.To = "$email"
$Mail.Subject = "send mail"
$Mail.Body = "This is my mail"
$Mail.Send()
Thank you again for your help !

Send email with Outlook application with cutomized permissions (Using PowerShell)

I'm trying to create and send emails using the Outlook application with PowerShell applying Permissions for the email (Example: Encrypt-Only, Do Not Forward, etc.), is currently working on the outlook application (when creating the email manually), however, couldn't find a way to send emails using PowerShell applying this setting:
Permission templates in Outlook
I'm currently using the following code to create and send the emails (Working):
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem("olMailItem")
$Mail.To = "test#test.com"
$Mail.Subject = "Test Email"
$Mail.Body = "Email sent using PowerShell"
$file = "C:\ExampleFolder\test.txt"
$Mail.Attachments.Add($file)
$Mail.Send()
Is there a way to apply this setting using PowerShell?
I founded the solution for this, this solution involve two properties: "$Outlook.CreateItem("olMailItem").Permission" and "$Outlook.CreateItem("olMailItem").PermissionTemplateGuid", and work as follow:
If $Outlook.CreateItem("olMailItem").Permission is set to "0": The email is sent as unrestricted message, no PermissionTemplateGuid is needed for this value.
If $Outlook.CreateItem("olMailItem").Permission is set to "1": The email is sent as olDoNotForward automatically, no PermissionTemplateGuid is needed for this value.
If $Outlook.CreateItem("olMailItem").Permission is set to "2": The email require a PermissionTemplateGuid, therefore, it's required to get the GUID of the template, since each organization have a different template according with the configuration, send an email with the encryption template to your inbox and read it using the following code:
$olFolderInbox = 6
$outlook = new-object -com outlook.application
$mapi = $outlook.GetNameSpace("MAPI")
$inbox = $mapi.GetDefaultFolder($olFolderInbox)
$items = $inbox.items
$items[1].PermissionTemplateGuid
This is going to provide the correct permissionTemplateGuid with the desired encryption.
Therefore, to send the email the following formula works:
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem("olMailItem")
$Mail.To = "test#test.com"
$Mail.Subject = "Test Email"
$Mail.Body = "Email sent using PowerShell"
$file = "C:\ExampleFolder\test.txt"
$Mail.Attachments.Add($file)
$Mail.PermissionTemplateGuid = XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
$Mail.Permission = "2"
$Mail.Send()
For more information visit this website: http://jon.glass/blog/reads-e-mail-with-powershell/

How to send an outlook email from generic email address using PowerShell script?

I am trying to automate sending email task using PowerShell script.
I want to send an email using generic (common) email address, not using current logged in user.
I have tried SentOnBehalfOfName property, but no luck, it is still sent an email from current logged in user.
For an exmaple,
My email address is : abc#outlook.net
Generic (common) email address is : common#outlook.net
Here, I want to send an email from common#outlook.net from any computer.
Below is my code that I have tried so far.
$Outlook = New-Object -Com Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.SentOnBehalfOfName = "common#outlook.net"
$Mail.To = "abc#****.net"
$Mail.Subject = "Test Email"
$Mail.Body = "Testing"
$Mail.Send()
$Outlook.Quit()
Please let me know, how can I accomplish it? Thank you in Advance.
I will use native System.Net.SMTPClient:
$to = 'mailadress1'
$from = 'mailadress2'
$onBehalfOf = 'mailadress3'
$subject = "powershell send on behalf"
$body = "body"
$smtpserver = "an SMTP server in your domain"
$mailer = new-object Net.Mail.SMTPclient($smtpserver)
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$msg.Sender = $onBehalfOf
$mailer.send($msg)
The user account will need to have the Send on Behalf permission enabled for that generic account, I've tested this with 2 generic accounts that were setup in my Outlook with the mentioned permission:
Microsoft - Send email on behalf of another user

Script to import information from csv to auto send email from outlook

Pretty new to this so bear with me.....
I am attempting to find a way to be able to import a CSV list including name, email, subject and a word (info) to the body of the email , and send it via Outlook and then go to the next line send that out, so on and so forth till all emails have been sent out. So the email may look like:
Email: $email
Subject: $subject
Body:
$name,
Hello! blah blah blah blah blah $info blah blah blah
Thank You,
end
I am looking to send out many from one list it will all be the same generic email but with certain words changed so looking to cut down on hand emailing time.
I have tried and successfully sent out one email with a script but that is it
Here is what I found to attempt to work off of
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "todd#emconsultinginc.com"
$Mail.Subject = "test"
$Mail.Body ="test"
$Mail.Send()
This is where I got to. I was able to get it to pull from a CSV with each line. Now I am wondering if we can pull the content from a word doc, keeping formatting and send it. I tried to pull the Word Doc info but I do not think I am quite there. Any suggestions?
$Mail.Send()
$Outlook = New-Object -ComObject Outlook.Application
$csv = Import-Csv C:\code\test.csv
$wd = New-Object -ComObject Word.Application
$doc = $wd.Documents.Open("C:\code\test.html")
$doc.Range().text
foreach ($line in $csv){
$Mail = $Outlook.CreateItem(0)
$Mail.To = $line.Email
$Mail.Subject = $line.Subject
$Name = $Line.Name
$body = $doc.Text
$Mail.Body ="Hello $Name, $body
$Mail.Send()
}

Getting powershell to email message queue size before purging the queue

Is there away of getting powershell to email the number of messages in a queue prior to purging the queue ?
I'm able to purge the queue and get it to count the messages, but an email would possibly help identify issues. if the queue was suddenly start ramping up.
I suppose what i need to do is have a script that will count the messages and then if it is above a predefined limit it will send an email with the message count then run the purge on the queue
This is some Powershell code that I use to send emails from Powershell Scripts
$smtpServer = ""
$smtpFrom = ""
$smtpTo = ""
$messageSubject = "Email Message";
$messageBody = "Body of Email"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$mail = new-object System.Net.Mail.MailMessage
$mail.from = $smtpFrom
$mail.to.add($smtpTo)
$mail.subject = $messageSubject
$mail.body = $messageBody
$mail.IsBodyHtml = $true
$smtp.Send($mail)