robdox
Goto Top

E-Mail Versand, null Value bei Net.Mail.Send PowerShell

Hallo,

bei dem Versuch E-Mails mit einer Funktion zu verschicken, welche auch sonst funktionierte, lässt mich nun auflaufen. Ich verstehe nicht, wohin die Werte sind, bzw. wieso keine richtige Übergabe stattfindet?

Fehler:

A constructor was not found. Cannot find an appropriate constructor for type System.Net.Mail.Attachment.
You cannot call a method on a null-valued expression.
At .....AVIS_MAIL.ps1:74 char:7
+       $smtp.Send($Mail_Msg)
+       ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) , RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Meine Powershell Funktion:

function AVIS_Mail($Mail, $body, $Mail_File, $Subject) {
     
     #Fehler Abfangen
     try { 
         
     #Mailobjekt erzeugen
     $SMTPServer = “smtp.office365.com”
     $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
     $SMTPClient.EnableSsl = $true
     $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("user", "pass")  
     $Mail_Msg          = New-Object System.Net.Mail.mailmessage
     $Mail_Att            = New-Object System.Net.Mail.Attachment($Mail_File)
     
     #E-Mail Anhang   
     $Mail_Msg.Attachments.Add($Mail_Att)           

     #Server
     #$smtp.Host           = "smtp.office365.com"  
     
     $Mail_Msg.from       = "avis@.....de"   
     $bccMail             = "e.....@.de"  
     
     $Mail_Msg.To.add($Mail)
     $Mail_Msg.Bcc.Add($bccMail)

     #Bereitstellen abhängig von der Sprache
     $Mail_Msg.Subject=$Subject

     #Format
     $Mail_Msg.IsBodyHtml = $true
     $Mail_Msg.Body       = $body
  
     #Versenden
     $SMTPClient.Send($Mail_Msg) 
     } 
    
     catch 
     {
      $_.exception.message
      $Mail_Msg.Subject    = "Fehler beim Zustellen einer Nachricht"  
      $Mail_Msg.Body       = "Fehler! Es konnte keine E-Mail an: "+$Mail+" geschickt werden<br><br>$_.exception.message"  
      $Mail_Msg.To.add("...@....")  
      $smtp.Send($Mail_Msg) 
      break;
     }

     #Prozess wieder freigeben, da sonst Zugriffsfehler
     $Mail_Att.Dispose()
     $Mail_Msg.Dispose()
     [gc]::Collect()
}

Die Variablen die ich Übergabe stimmen wenn ich mir diese Anzeigen lasse per Write-host:

[array]$AVIS_f = Get-ChildItem -Path $($AVIS.ToString()+"*") -Include "*.pdf"   

foreach($af in $AVIS_f){
    $MC=$af.Name.Split("_")  
    [array]$CMD=$("  
                    	SELECT
	                    ('AVIS // '+Anschrift_Name1+', '+Anschrift_PLZ+' '+ Anschrift_Ort) 'AVIS',  
	                    (select top 1 Kund_Partner.email from Kund_Partner where Kund_Partner.ID_Kunde=KB.ID and Kund_Partner.Funktion='AVIS') 'MAIL',  
	                    (select top 1 Kund_Partner.Fax from Kund_Partner where Kund_Partner.ID_Kunde=KB.ID) 'FAX'  
	                    from Kund_Basis KB where ID=(select ID from Kund_Basis where MC='$MC')  
                    ")  
    [array]$SUB = (querySQL -cSQL $CMD).AVIS
    [array]$MAIL = (querySQL -cSQL $CMD).MAIL
    [array]$FAX = (querySQL -cSQL $CMD).FAX
    foreach($s in $SUB){
        foreach($m in $MAIL){
            foreach($f in $FAX){
                Write-Host $af", " $s ", " $m ", " $f  
                AVIS_Mail("..@...", $BODYtx, $af, $s)  
            }
        }
    }
}

Ergebnis mit Write-Host

 
"F:\mail\AVIS_FIRST\WIEDEIGERS_1125.PDF,  AVIS // Memann GmbH, 90009 Igereim, info@......de"  

Warum werden keine Werte korrekt übergeben?

Danke wieder vorab!

Content-Key: 627160

Url: https://administrator.de/contentid/627160

Ausgedruckt am: 28.03.2024 um 19:03 Uhr

Mitglied: 146707
Lösung 146707 01.12.2020 aktualisiert um 16:45:25 Uhr
Goto Top
Wieder das selbe Problem wie bei deinem letzten Post hier im Forum, du übergibst als Attachment ein [FILEINFO] Object statt einem String. Deswegen findet die PS keinen validen Konstruktor für System.Net.Mail.Attachment, weil es dafür keinen gibt.
Die Fehlermeldung sagt genau das aus
Cannot find an appropriate constructor for type System.Net.Mail.Attachment.
Die möglichen Konstruktoren kannst du hier nachschlagen
https://docs.microsoft.com/de-de/dotnet/api/system.net.mail.attachment.- ...
Dein genutzter Konstruktor ist nämlich dieser
public Attachment (string fileName);
Und der erwartet einen String und kein Object der Klasse FileInfo welche die Variable $af nunmal ist weil du diese mit Get-ChildItem erstellst.

Übergebe also statt nur das Objekt $af die passende Property des Objects
$af.Fullname an die Function.
Die Powershell arbeitet objektorientiert mit .NET Klassen und mit expliziten Typen und macht nicht alles selbst daran solltest du dich langsam mal gewöhnen!
Mitglied: mbehrens
Lösung mbehrens 01.12.2020 um 16:51:52 Uhr
Goto Top
Zitat von @robdox:

> A constructor was not found. Cannot find an appropriate constructor for type System.Net.Mail.Attachment.
> You cannot call a method on a null-valued expression.
> At .....AVIS_MAIL.ps1:74 char:7
> +       $smtp.Send($Mail_Msg)
> +       ~~~~~~~~~~~~~~~~~~~~~
>     + CategoryInfo          : InvalidOperation: (:) , RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull
> 

Die Attachment Klasse kennt keinen Konstruktor für Objekte des Typs System.IO.FileInfo.
Mitglied: robdox
robdox 01.12.2020 um 17:39:22 Uhr
Goto Top
Danke Dir, werde ich dann probieren!
Mitglied: robdox
robdox 02.12.2020 um 10:10:10 Uhr
Goto Top
Hallo zusammen,

also ich habe die Typkonvertierung eben probiert - selbst wenn ich den Pfad manuell eintrage, bekomme ich die gleiche Fehlermeldung.

AVIS_Mail("..@..de", $BODYtx,"F:\AVIS\ASBE_1128.PDF", $s)  

A constructor was not found. Cannot find an appropriate constructor for type System.Net.Mail.Attachment.
You cannot call a method on a null-valued expression.

AVIS_Mail("..@..de", $BODYtx, $af.FullName, $s)  

A constructor was not found. Cannot find an appropriate constructor for type System.Net.Mail.Attachment.
You cannot call a method on a null-valued expression.

Funktion weiterhin:

function AVIS_Mail($Mail, $body, $Mail_File, $Subject) {
     
     try { 
     $SMTPServer             = “smtp.office365.com”
     $SMTPClient             = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
     $SMTPClient.EnableSsl   = $true
     $SMTPClient.Credentials = New-Object System.Net.NetworkCredential(....)
    
     $Mail_Msg               = New-Object System.Net.Mail.mailmessage
     $Mail_Att               = New-Object System.Net.Mail.Attachment($Mail_File)  
     $Mail_Msg.Attachments.Add($Mail_Att)           
     $Mail_Msg.from          ="avis@...de"   

     $bccMail                ="...@..de"  
     $Mail_Msg.To.add($Mail)
     $Mail_Msg.Bcc.Add($bccMail)
     $Mail_Msg.Subject       =$Subject
     $Mail_Msg.IsBodyHtml    =$true
     $Mail_Msg.Body          =$body

     $SMTPClient.Send($Mail_Msg) 
     } 
    
     catch 
     {
      $_.exception.message
      $Mail_Msg.Subject    = "Fehler beim Zustellen einer Nachricht"  
      $Mail_Msg.Body       = "Fehler! Es konnte keine E-Mail an: "+$Mail+" geschickt werden<br><br>$_.exception.message"  
      $Mail_Msg.To.add(".....")  
      $smtp.Send($Mail_Msg) 
      break;
     }
     #Prozess wieder freigeben, da sonst Zugriffsfehler
     $Mail_Att.Dispose()
     $Mail_Msg.Dispose()
     [gc]::Collect()
}

Aus den MS-Dokumentationen habe ich zwar verstanden, was erwartet wird - jedoch bleibt der Fehler bestehen - danke vorab!
Mitglied: 146707
Lösung 146707 02.12.2020 aktualisiert um 10:21:15 Uhr
Goto Top
Du rufst die Powershell Funktion ja auch falsch auf...
So muss das aussehen
AVIS_Mail "..@..de" $BODYtx $af.FullName $s  
Eigene CMDLets werden nicht mit Klammern für Parameter noch Kommata um die Parameter zu trennen aufgerufen, macht man ja bei den üblichen CMDLets auch nicht!
Mitglied: robdox
robdox 02.12.2020 um 11:22:51 Uhr
Goto Top
`Danke, ich switche zuoft zwsichen verschiedenen Skriptsprachen - danke!
Mitglied: 146707
146707 02.12.2020 aktualisiert um 11:34:54 Uhr
Goto Top
Dann fürs nächste mal erst vorher nochmal gründlich nachdenken und die Docu zu Rate ziehen bevor man vorschnell einen Post lostritt face-wink.