I am Stuck at a powershell Script. I am trying to reply a email in outlook using powershell.
i have only 2 methods.
$email.reply()=the $email(is the email i want to reply)
$email.replyall()
How can i add additional email address in this.
if i do $email.recipients.add(“new@email.com”)
i got error
please help
You’ve not provided much information, but I think the problem you’re having is because you’re trying to add the recipient to the message you want to reply to, rather than to the reply itself.
if ($useremail -eq $null) {
Write-Host “#####################################################################################################################” -ForegroundColor Green
Write-host “…No email recived …” -ForegroundColor Red
Write-Host “#####################################################################################################################” -ForegroundColor Green
}
else
{
if (($useremail.to -like ($Sendername.DisplayName)))
{
$reply = $useremail.Recipients.Add(“$($cred.UserName)”)
$reply = $useremail.Recipients.ResolveAll()
$reply = $useremail.Reply()
$reply.body = “Hi $($Sendername.DisplayName),n The device has been removed from quarantine. It may take up to 1 hour to synchronize email on your phone.n n Kind Regards,n
Name.Lastname”
$reply.send()
}
else
{
$reply = $useremail.ReplyRecipients.Add(“$($x)”)
#$reply = $useremail.Recipients.ResolveAll()
$reply = $useremail.Recipients.ResolveAll()
$reply = $useremail.ReplyAll()
$reply.body = “Hi $($Sendername.DisplayName),n The device has been removed from quarantine. It may take up to 1 hour to synchronize email on your phone.n n Kind Regards,n”
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
To add additional email addresses to an email reply in Outlook using PowerShell, you can use the Add method of the Recipients property of the MailItem object. Here is an example of how you could do this:
The Add method allows you to add a new recipient to the list of recipients for the email. The ResolveAll method is then used to resolve the names of the recipients and ensure that they are valid.
You can also use the Add method to add multiple recipients at once by separating the email addresses with a comma:
$x = "example@email.com
$useremail = "the email i narrow down and want to reply to.
$reply=$useremail.Recipients.Add("$x")
(problem comes when i add this $x here, and when i check the get-member on $reply, it will have only 8 property. it will not give me .body or .send().
$reply=$useremail.Reply()
I again get .body property by this.
$reply=$useremail.Body ="Hi $($Sname.DisplayName),`n
The device has been removed from quarantine. It may take up to 1 hour to synchronize email on your phone.`n
`n
Kind Regards,`n"
Here it again , i cant find send property . so i have to run this reply() again. to activate .send().
$reply=$useremail.Reply()
$reply.send()
It send the email and reply but i cant see the origional email in my inbox.
You should slow down and read what people are suggesting to you. You try to add the emails to the ORIGINAL email, not to the REPLY. Store the reply in a variable and only work on the reply variable. Don’t add to the $useremail variable.
Maybe these notes on this example I typed up for you will help. Stop ignoring the help you are receiving please.
# instatiate outlook and connect to the default profile
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
# this is the inbox folder
$inbox = $namespace.GetDefaultFolder(6)
# The tostring('g') method will put the date in an outlook filter friendly format
$starttime = (Get-Date).AddMinutes(-2).ToString('g')
# this will find an item newer than the starttime with a specific subject.
# Note: The RE: and FWD: is NOT part of the subject, so all emails in chain have the same
$useremail = $inbox.Items.Find("[receivedtime] > '$starttime' And [subject] = 'test new'")
# this takes the email, creates a reply email object. This is the object you need to manipulate
# Do not use $useremail again, it is not the reply.
$reply = $useremail.Reply()
# if there is more than one email to add to the TO field, use a loop. Otherwise, simply add the one email.
# Remember, the original sender is already a recipeint on the reply.
$emaillist = 'user@domain.com','user2@domain.com'
foreach($email in $emaillist){
# capture the output to keep the pipeline clean
$null = $reply.Recipients.Add($email)
}
# if you need to add CC or BCC, just give a single string with semicolon delimited addresses to it.
$reply.CC = 'user3@domain.com; user4@domain.com'
# if you want to retain the CC addresses and add to it, then add to it. (But you would've needed to do ReplyAll() instead of Reply() earlier)
$reply.CC = $reply.CC += '; user3@domain.com; user4@domain.com'
# if you really want the exact same body in the original, you can set the reply body to the same, but this seems weird.
# Generally you would insert your own body of text into the body. However this would be one of the few times using the original email variable is fine.
$reply.Body = $useremail.body
# send the reply.
$reply.Send()