I am using powershell send-mail command to send mail. It is working fine. But I cant see the mail which I sent in "sent items". I can see it if I sent mail manually, but I am not able to understand why it is not visible if i sent a mail using send-mail command.
Thanks
I have a function which creates an Outlook "Draft". I do it this way so that the e-mail can be reviewed before hitting "Send". This may not work for you; perhaps you'd want to add a send command at the end. But what you describe wanting to do is probably going to need outlook available on the system which is executing the commands.
It will check to see if you have a mail profile set up first, but otherwise it's not super-robust.
Function Compose-Email {
Param ([String]$recipients, [string]$subject, [string]$body)
$reg="HKCU:\Software\Microsoft\Office\16.0\Outlook\Profiles"
$child=(Get-ChildItem -Path $reg).name
if (!((Get-ChildItem -Path $reg).name)) {
Write-Error "No Mail Profile found! Cannot compose Draft."
}
else {
$olFolderDrafts = 16
$ol = New-Object -comObject Outlook.Application
$ns = $ol.GetNameSpace("MAPI")
# call the save method to save the email in the drafts folder
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add($recipients)
$Mail.Subject = $subject
$Mail.Body = $body
$Mail.save()
$mail.display()
}
}
Related
I have an automated powershell script to send alert email. Since the alert are send from a generic email.. i want people replying to the email to goto another email address say efg#outlook.com.
$Outlook = New-Object -comObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "asd#outlook.com"
$Mail.Subject = "Alert Email"
$Mail.HtmlBody = $HTML
$Mail.Headers.Add("In-Reply-To", "<efg#outlook.com>")
$Mail.display()
$Mail.send()
somehow this is not working.
Managed to get it from the below
ReplyRecipientNames
$Mail.ReplyRecipientNames = "efg#outlook.com"
Firstly, MailItem object does not expose the Headers property - your script will stop rigth there.
If you want to redirect replies to a different address, use Mail.ReplyRecipients.Add("efg#outlook.com")
You can use the MailItem.SendUsingAccount property which allows setting up an Account object that represents the account under which the MailItem is to be sent. Be aware, the account should be configured in Outlook in that case. For example, here is a VBA code sample which shows how to set up the property:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone#example.com")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
For Exchange users you may also consider using the MailItem.SentOnBehalfOfName property which is represented by a string indicating the display name for the intended sender of the mail message.
But also you may be interested in the MailItem.ReplyRecipients property which returns a Recipients collection that represents all the reply recipient objects for the Outlook item. The following article which I wrote for the technical blog shows how to deal with a recipient collection, see How To: Fill TO,CC and BCC fields in Outlook programmatically for more information.
I'm trying to delete specific messages from an MSMQ message queue using PowerShell. I'm aware that several new cmdlets for managing MSMQ queues & messages were introduced in PowerShell 4+, but from what I can tell, these still don't help me achieve my goal of deleting specific messages.
What I'm trying to achieve is a simple script that connects to a specified queue, then loops through every message in that queue, looks for a specified search string in the message body, and then MOVES any matching messages to another queue on the same server. I have this working very nicely for searching messages, and can COPY any matching using using the Send method:
$source = new-object System.Messaging.MessageQueue($sourcequeuename)
$destination = new-object System.Messaging.MessageQueue($destinationqueuename)
$msgs = $source.GetAllMessages()
foreach ( $msg in $msgs )
{
#Extra logic here - check message body for search string etc...
$destination.Send($msg)
}
But - once copied, my only option to delete the original message from the source queue seems to be purging the entire source queue, which will delete all other messages as well, which I don't want to do!
$source.Purge()
I thought I was onto something with the new (PowerShell 4+) 'Move-MsmqMessage' cmdlet...
Move-MsmqMessage -DestinationQueue $d -InputObject $q -Message $msg
But I'm struggling to get this to work - I'm unclear if I can pass it specific message objects to move. Can anyone give me a working example of using PowerShell (any version) to loop through messages in a queue and move specific messages using this Move-MsmqMessage cmdlet?
Not tested but you should be able to delete the message using the ReceiveById function:
foreach ( $msg in $msgs )
{
#Extra logic here - check message body for search string etc...
$source.ReceiveById($msg.Id, [timespan]::FromMinutes(1))
}
The question and answer do not contain the full scripts used. Here's the full script I used to remove a single message from the queue, once I knew the Id of the message.
[System.Reflection.Assembly]::LoadWithPartialName("System.Messaging")
$queuePath = "FormatName:DIRECT=OS:MachineName\private$\NameOfQueue"
$queue = New-Object System.Messaging.MessageQueue $queuePath
$poisonMessageId = "d53df8ba-3b6a-418b-ab27-198655534253\39996"
$queue.ReceiveById($poisonMessageId)
Btw, to find the message Id is easy, btw.
Go find the message in:
Computer Management -> Services and Applications -> Message Queueing -> Private Queues. Open up your private queue containing the message you want to get rid of
Select 'Properties' and in the 'General' tab is the message id.
Extended version of script from Jay Dubya with deletion by pattern
$StringToFind = "AMS1-COMET5"
$QueueName = "AMS1-COMET5"
[System.Reflection.Assembly]::LoadWithPartialName("System.Messaging")
$queuePath = "FormatName:DIRECT=OS:"+ [System.Net.Dns]::GetHostName() + "\private$\" + $QueueName
$queue = New-Object System.Messaging.MessageQueue $queuePath
$SrcQueue = Get-MsmqQueue($QueueName)
$Messages = $SrcQueue | Receive-MsmqQueue -Peek -Count 10000000
$Messages |foreach {
if ($_.Label -match $StringToFind) {
$_.Label
$queue.ReceiveById($_.Id)
}
}
Fairly simple script at the moment I simply need to get the recipients email address out of a msg file, apparently there's not an easy way to parse it in text so I've used the following code, but I only get a name such as 'Joe Smith' when reading the message not joe.smith#mydomain.com
Any ideas?
Thanks!
$outlook = New-Object -comobject outlook.application
$msg = $outlook.CreateItemFromTemplate("c:\MyMessage.msg")
$msg | Select to
You need to access the Recipients collection and read the Recipient.Address property for the items in that collection. You also might want to use Application.Session.OpenSharedItem instead of CreateItemFromTemplate.
I'm trying to write a powershell script (which will run periodicaly) for opening the new email windows of outlook with "to", "subject" and "body" filled with some data.
I found a way to send mails from powershell but you have to send it from powershell. this doesn't fit the need because I have to edit the body of the mail.
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "random.dude#email.com"
$Mail.Subject = "data for Subject"
$Mail.Body ="Example of body..."
$Mail.Send()
Basicaly what i need is a $Mail.Show() wich will open a new e-mail popup with the data pre-filled
powershell is not a requirement, it just seams able to manipulate outlook so I tried with it.
thanks to this thread, the $Mail.Show() is actually $Mail.Display()
I know this is a little late however if you add the following to your script removing your $Mail.Send() line it should open the email ready for editing:
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()
I have been trying to find answers with no luck if this is possible.
Is there a way to create an outlook email with custom voting options through powershell?
My goal is to have an automated script that sends email based on a criteria and have it sent with a custom voting options so that people can respond through a voting mechanism.
Here is one way to do it by using the Outlook COM Object model.
#Open Outlook
$outlook = New-Object -ComObject Outlook.Application
#Create new Mail Message
$mail = $outlook.CreateItem(0)
$mail.To = 'user#someplace.com'
$mail.Subject = 'Test Voting'
$mail.Body = 'Test Voting Message'
#Add Voting options
$mail.VotingOptions = "Yes;No;Maybe"
#Send Message
$mail.Send()
#Exit Outlook
$outlook.Quit()