0

When i try to send email through powershell im facing following issues 1.Im not able to change the sender as group mailbox 2.it does not read the attachments i add but the mail gets sent.

code im using :

    $Outlook = New-Object -ComObject Outlook.Application
    $Mail = $Outlook.CreateItem(0)
    $Mail.To = "[email protected]"
    $Mail.Subject = "sql"
    $mail.attachments = 'C:\Users\desktop.ini'
    $Mail.Body = ""
    $Mail.Send()

error i'm receiving :

     Property is read-only.
     At C:\Users\Documents\sending email.ps1:5 char:1
     + $mail.attachments = 'C:\Users\desktop.ini'
     + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : OperationStopped: (:) [], COMException
     + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

But the file is in my local

2
  • Is there a reason to use the Outlook COM object and not Send-MailMessage? Commented May 29, 2020 at 8:27
  • when i add smtp and port details my outlook dosent allow me to send email.so tried alternate method lik this Commented May 29, 2020 at 8:57

1 Answer 1

1

You are missing an additional parameter

Your script should look something like this:

$attachment = "C:\Users\YourUser\Documents\test.txt"    
$mail.attachments.add($attachment)

or, if not with a variable

$mail.attachments.add("C:\Users\YourUser\Documents\test.txt")

As for the group mailbox, the mailbox should be controllable by using

$mail.sendusingaccount = "[email protected]"

As for adding multiple attachments, you will have to have them in one directory:

$getfiles = Get-ChildItem "C:\Users\YourUser\Attachmentfolder\"
Foreach ($getfile in $getfiles) {
$mail.attachments.add($getfile.FullName)
}
Sign up to request clarification or add additional context in comments.

6 Comments

So what its worth, I tried it out and could not get it to work for myself sadly. But, some research later, I would advise you in general of using the following: send-mailmessage -smtpServer (servername or IP) -from [email protected] -to @([email protected], [email protected]) -subject "Email Subject" -body "Email Body" SMTP works like a charm compared to a regular outlook item request. - That is also how I could get it to work then.
Ill just quickly add you another stackoverflow post: stackoverflow.com/questions/41149684/… You can try the outlook-item piece of script, which is recommended by the first answer, but it did not work for me. This might be some other problem on my side though.
How do i add multiple attachments ?
$mailobject.attachment.add supports an array, so doing something like $mail.attachments.add("C:Users\YourUser\Documents\test.txt","C:Users\YourUser\Documents\test2.txt","C:Users\YourUser\Documents\test3.txt") should work in theory.
So I researched a bit and found out that only the send-mailmessage cmdlet accepts arrays for its -attachment parameter. Sorry for that. BUT, of course, I found a way around for you which I added for additional clarity to my initial answer, as you can see. I hope this works for you, because it worked like a charm for me. Good luck! Edit: You will need to delete the $mail.attachments.add($attachments) from outside the foreach loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.