0

I'm trying to send an email with a Powershell script, when I run my program as is I get an error that looks like this:

Exception setting "Add": "Cannot set the Value property for PSMemberInfo object of type 
"System.Management.Automation.PSMethod"."
At C:\Users\documents\eventSender.ps1:11 char:1
+ $emailMessageObject.To.Add = $toEmailAddress
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

'Attachments' is a ReadOnly property.
At C:\Users\documents\eventSender.ps1:14 char:1
+ $emailMessageObject.Attachments = $currentAttachment
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Exception calling "Send" with "1" argument(s): "A recipient must be specified."
At C:\Users\documents\eventSender.ps1:18 char:1
+ $smtpClient.Send($emailMessageObject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

Now I'm fairly new to powershell so I don't really understand the error, how I'm trying to send the message looks like this:

$todaysDate = Get-Date -DisplayHint Date
$smtpServerAddress = "smtp.google.com"
$toEmailAddress = "[email protected]"
$fromEmailAddress = "[email protected]"
$subjectMessage = "Files for date -> $todaysDate"
$body = "log files sent on $todaysDate (this is a test)"
$currentAttachment = "C:/Users/Downloads/TBG-1.htm"

$emailMessageObject = New-Object System.Net.Mail.MailMessage
$emailMessageObject.From = $fromEmailAddress
$emailMessageObject.To.Add = $toEmailAddress
$emailMessageObject.Subject = $subjectMessage
$emailMessageObject.Body = $body
$emailMessageObject.Attachments = $currentAttachment

$smtpClient = New-Object Net.Mail.SmtpClient($smtpServerAddress, 587)
$smtpClient.EnableSsl = $true
$smtpClient.Send($emailMessageObject)

What am I doing wrong to where I am getting this error message, also do I need to have credentials that match my email address to send this?

2 Answers 2

1

You should use Send-MailMessage in place of System.Net.Mail.Message.

I find the easiest way is through Splatting:

$Splat = @{ 
    To         =$toEmailAddress  
    Body       =$body
    Subject    =$subjectMessage  
    SmtpServer =$smtpServerAddress  
    From       =$fromEmailAddress
    Attachements = $currentAttachment    
    } 

    Send-MailMessage @Splat 

You should only have to specify credentials if your exchange requires that the sender be authorized. If so, you would simply just change the $Splat to:

 $Splat = @{ 
    To         =$toEmailAddress  
    Body       =$body
    Subject    =$subjectMessage  
    SmtpServer =$smtpServerAddress  
    From       =$fromEmailAddress
    Attachements = $currentAttachment 
    Crendential = $creds   
    } 

    Send-MailMessage @Splat 
Sign up to request clarification or add additional context in comments.

Comments

0

MailMessage.To.Add() is a method, so you have to call it like this $emailMessageObject.To.Add($toEmailAddress)

MailMessage.Attachments is also a collection, so you have to add the item with $emailMessageObject.Attachments.Add($currentAttachment)

Source

Comments

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.