Search code examples
powershellemailoutlookautomationreply

Reply email powershell


Hi again StackOverflow's comunity, I will ask about method ".reply()". This is the code I am trying:

function Unread ($correo) {
    if(($correo -eq $null) -or ($correo.Unread.ToString() -like "False")){
        $Noleido = $false
    }else{  
        $Noleido = $true
     return $Noleido
    }
}

    $body = "Bla bla bla"
    $firma = "I am here"
    #$cuerpo = "A test ps"
    $subject = "Re: automated reply"
    $Outlook = New-Object -comObject Outlook.Application 
    $OutlookFolders = $Outlook.Session.Folders.Item($buzon1).Folders
    #Map la bandeja de entrada.
    $bandeja_de_entrada=$OutlookFolders.Item("INBOX_FOLDER")

    #Creamos el objeto que hace referencia a la bandeja de entrada y los mensajes que contiene.
    $all_mail=$bandeja_de_entrada.Items


foreach ($mail in $all_mail){ 
    $flag1 = Unread($mail)
    if($flag1 -eq $true){     
       #$mail.to = ""         
       $mail.body =" $cuerpo" +"$firma"
       $mail.subject = $subject
       $mail.reply()
   }
}

Dont send the email.

Its solved in a answer


Solution

  • The method reply creates a MailItem prepopulated with the necessary properties based on the original mail.

    If you save the MailItem created by the reply method to a variable you can then use the method send to actually send the reply.

    Modifying your script to look something like the following would probably work.

    $body = "Bla bla bla"
        $firma = "I am here"
        $subject = "A test ps"
        $Outlook = New-Object -comObject Outlook.Application 
        $OutlookFolders = $Outlook.Session.Folders.Item($buzon1).Folders
        #Map la bandeja de entrada.
        $bandeja_de_entrada=$OutlookFolders.Item("INBOX_FOLDER")
    
        #Creamos el objeto que hace referencia a la bandeja de entrada y los mensajes que contiene.
        $all_mail=$bandeja_de_entrada.Items
    
        foreach ($mail in $all_mail){      
           #$mail.to = ""
           $reply = $mail.reply()
           $reply.body = " $cuerpo $firma"
           $reply.subject = $subject
           $reply.send()
        }
    

    If you want to include the original message instead of overwriting it you could change the $reply.body-line to something like this;

    $reply.body = $reply.body + " $cuerpo $firma"