windows - får fejl mens du sender e-mails via Powershell script

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg forsøger at sende en mail med et Powershell-script, når jeg kører mit program, som jeg får en fejl, der ser sådan ud:


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

'Attachments' is a ReadOnly property.
At C:UsersdocumentseventSender.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:UsersdocumentseventSender.ps1:18 char:1
+ $smtpClient.Send($emailMessageObject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException


Nu er jeg temmelig ny til powerhell, så jeg forstår virkelig ikke fejlen, hvordan jeg forsøger at sende meddelelsen ser sådan ud:


$todaysDate = Get-Date -DisplayHint Date
$smtpServerAddress = "smtp.google.com"
$toEmailAddress = "admins@me.com"
$fromEmailAddress = "me@me.com"
$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)


Hvad gør jeg forkert til, hvor jeg får denne fejlmeddelelse, skal jeg også have legitimationsoplysninger, der matcher min e-mail-adresse for at sende denne?

Bedste reference


Du skal bruge Send-MailMessage i stedet for System.Net.Mail.Message.


Jeg finder den nemmeste måde er gennem Splatting:


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

    Send-MailMessage @Splat 


Du skal kun angive legitimationsoplysninger, hvis din udveksling kræver, at afsenderen er autoriseret. Hvis ja, ville du blot ændre $Splat til:


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

    Send-MailMessage @Splat 

Andre referencer 1


MailMessage.To.Add() er en metode, så du skal kalde det sådan $emailMessageObject.To.Add($toEmailAddress)


MailMessage.Attachments er også en samling, så du skal tilføje varen med $emailMessageObject.Attachments.Add($currentAttachment)


Kilde [12]