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:
Meine Powershell Funktion:
Die Variablen die ich Übergabe stimmen wenn ich mir diese Anzeigen lasse per Write-host:
Ergebnis mit Write-Host
Warum werden keine Werte korrekt übergeben?
Danke wieder vorab!
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
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()
}
[array]$AVIS_f = Get-ChildItem -Path $($AVIS.ToString()+"*") -Include "*.pdf"
foreach($af in $AVIS_f){
$MC=$af.Name.Split("_")[0]
[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)
}
}
}
}
"F:\mail\AVIS_FIRST\WIEDEIGERS_1125.PDF, AVIS // Memann GmbH, 90009 Igereim, info@......de"
Danke wieder vorab!
7 Antworten
- LÖSUNG 146707 schreibt am 01.12.2020 um 16:37:54 Uhr
- LÖSUNG robdox schreibt am 01.12.2020 um 17:39:22 Uhr
- LÖSUNG robdox schreibt am 02.12.2020 um 10:10:10 Uhr
- LÖSUNG 146707 schreibt am 02.12.2020 um 10:19:15 Uhr
- LÖSUNG robdox schreibt am 02.12.2020 um 11:22:51 Uhr
- LÖSUNG 146707 schreibt am 02.12.2020 um 11:34:26 Uhr
- LÖSUNG robdox schreibt am 02.12.2020 um 11:22:51 Uhr
- LÖSUNG 146707 schreibt am 02.12.2020 um 10:19:15 Uhr
- LÖSUNG robdox schreibt am 02.12.2020 um 10:10:10 Uhr
- LÖSUNG robdox schreibt am 01.12.2020 um 17:39:22 Uhr
- LÖSUNG mbehrens schreibt am 01.12.2020 um 16:51:52 Uhr
LÖSUNG 01.12.2020, aktualisiert um 16:45 Uhr
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
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!
Die Fehlermeldung sagt genau das aus
Cannot find an appropriate constructor for type System.Net.Mail.Attachment.
Die möglichen Konstruktoren kannst du hier nachschlagenhttps://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!
LÖSUNG 01.12.2020 um 16:51 Uhr
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.
LÖSUNG 02.12.2020 um 10:10 Uhr
Hallo zusammen,
also ich habe die Typkonvertierung eben probiert - selbst wenn ich den Pfad manuell eintrage, bekomme ich die gleiche Fehlermeldung.
Funktion weiterhin:
Aus den MS-Dokumentationen habe ich zwar verstanden, was erwartet wird - jedoch bleibt der Fehler bestehen - danke vorab!
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.
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()
}
LÖSUNG 02.12.2020, aktualisiert um 10:21 Uhr
LÖSUNG 02.12.2020 um 11:22 Uhr
LÖSUNG 02.12.2020, aktualisiert um 11:34 Uhr