简体   繁体   中英

Sending email through powershell issue

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 = "xyz@outlook.com"
    $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

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 = "mailbox@company.com"

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM