Reputation: 531
I'm currently writing a script to automatically send some mails. I want to send an automated mail, and for purpose of a clean mailbox I want these automated mails to be deleted from the sent-folder straight away.
I have got the script so far as to send the mail, and I also have written a perfectly functioning function that clears the sent-folder.
The problem is that the mail will remain in the outbox while it is being sent and only enters the sent-folder when it is in fact sent, but by then my script will have ended and the Clear-function will have passed without having cleared the mail.
I will need some sort of timer or if possible some triggered event for when the mail is in fact sent and stored in the sent-folder.
function Send
{
$mItem = $ol.CreateItem(0)
$mail = $routlook.GetRDOObjectFromOutlookObject($mItem)
$mail.To = "[email protected]"
$mail.Subject = "some subject"
$mail.Body = "some body stuff"
$mail.Attachments.Add("<path to attachment>")
$mail.Send()
Clear
}
function Clear
{
$SItems = $sent.Items
foreach($s in $SItems)
{
if( $s.To -eq "'[email protected]'")
{
$s.Delete()
}
}
}
Please help me, thanks ;)
Upvotes: 2
Views: 1419
Reputation: 66235
Set the MailItem.DeleteAfterSubmit
property to true - the message will never be moved to the Sent Items folder after it is sent.
Upvotes: 1
Reputation: 28174
Don't send mail through Outlook in the first place, unless you absolutely must for some odd reason. Use Send-Mailmessage
instead.
Send-MailMessage -to [email protected] -subject "Some Subject" -body "some body stuff" -attachments <Collection of file paths> -SmtpServer Your_SMTP_HOST -from [email protected]
Upvotes: 0