Thomas D.
Thomas D.

Reputation: 11

Not all attachments are added

I've been breaking my head on this for a few days now so I hope you guys see the issue here.

I have a PowerShell script to send mails and it works, but I have issues adding attachments.

When I add files (without the $sigmig part that adds a CID image for the img tag in the html body), my files are added fine. When I insert the $sigimg part, then it only adds the mailSig-rappo.png file, but not my other attachments.

I can't figure out why...

param(
    [Parameter(mandatory=$true)]
    [string]$to,
    [Parameter(mandatory=$false)]
    [string]$cc,
    [Parameter(mandatory=$false)]
    [string]$bcc,
    [Parameter(mandatory=$false)]
    [string]$subject
)

$Username = "[email protected]";
$Password = "myPassword";

$bijlage = "C:\Users\frank\Desktop\TEST\file1.txt,C:\Users\frank\Desktop\TEST\file2.txt";

$message = new-object System.Net.Mail.MailMessage;
$message.From = "John Lee <[email protected]>";

if (![string]::IsNullOrEmpty($to)) {
    $message.To.Add($to);
} else {
    $message.To.Add("[email protected]");
}

if (![string]::IsNullOrEmpty($cc)) {
    $message.Cc.Add($cc);
}

if (![string]::IsNullOrEmpty($bcc)) {
    $message.Bcc.Add($bcc);
}

if (![string]::IsNullOrEmpty($subject)) {
    $message.Subject = $subject;
} else {
    $message.Subject = "AUTOMATISCH: Mail verstuurd vanuit Connect4U.";
}

if (![string]::IsNullOrEmpty($bijlage)) {
    $files = $bijlage.split(",");
    ForEach ($file in $files) {
            $message.Attachments.Add($file);
        }
}

$sigimg = New-Object System.Net.Mail.Attachment("mailSig-rappo.png");
$sigimg.ContentType.MediaType = "image/png";
$sigimg.ContentId = "sig.png"; #can be anything as long as it matches the CID reference in the HTML
$message.Attachments.Add($sigimg);

$message.IsBodyHtml = $true;
$message.Body = Get-Content -Path body.txt;

$smtp = new-object System.Net.Mail.SmtpClient("smtp.provider.com", "587");
$smtp.EnableSSL = $true;
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message);

I already tried to add the sigim part before the attachments.

Or I can only add attachments, or I can only add the cid image

Upvotes: 0

Views: 36

Answers (0)

Related Questions