Send-MailMessage

avatar

While I understand that Microsoft no longer supports Send-MailMessage (but doesn't have a replacement, yet)....I can do the following from PowerShell (on the server hosting PSU), but not within PSU itelself when I try to use Send-MailMessage and embedding an image in an e-mail.

$Attachment = New-Object System.Net.Mail.Attachment -FilePath


however, within PSU - when I hit that point in the Web App - I get a "could not find System.Net.Mail.Attachment".



My understanding is that this isn't a module that I need to import, however I can't seem to get the app to load it.

0abd9f28-57c2-4af7-a836-7f7f7b8ca890.png

f7d0a786-d7f3-4337-acfa-b262478e90d8.png

All Comments (5)

avatar

@bcaydelotte That is a pretty weird error message. Can you be more explicit about the parameters to see if that helps?

New-Object -TypeName System.Net.Mail.Attachment -ArgumentList $File


What environment is your app running in?

Adam Driscoll
PowerShell Expert and Developer at Devolutions

avatar

Appreciate the quick response.

Looks like -ArgumentList is the only parameter available to me.

The environment is running via the default environment - I've also tried to specify PowerShell 7, but same error occurs.

avatar

I will verify this tomorrow. However, I have used New-Object System.Net.Mail.Attachment for a long time without any issues. Please note that I haven't tested it with the latest release yet.

avatar

Much appreciated. FYI - I'm on version 2026.1.5

avatar

this is my function

function Send-Mail {
    param($subject, $body, $to_mail, $bcc_mail, $cc_mail, $from_mail, $from_mail_name, $files, $smtpServer, $SmtpClientObject)
    if (! $smtpServer) {
        $smtpServer = "redacted"
    }
    $Message = New-Object System.Net.Mail.MailMessage
    $Message.Subject = "$subject"
    if ($from_mail_name) {
        $Message.From = New-Object System.Net.Mail.MailAddress ("$from_mail", "$from_mail_name")
    }
    else {
        $Message.From = New-Object System.Net.Mail.MailAddress "$from_mail"
    }
    if ($to_mail) {
        $Message.To.Add("$to_mail")
    }
    if ($bcc_mail) {
        $Message.Bcc.Add("$bcc_mail")
    }
    if ($cc_mail) {
        $Message.CC.Add("$cc_mail")
    }
    if ($files) {
        foreach ($file in $files) {
            $attachment = New-Object System.Net.Mail.Attachment($file)
            $Message.Attachments.Add($attachment)
        }
    }
    $Message.Body = "$body"
    $Message.IsBodyHtml = $true
    if (! $SmtpClientObject) {
        $SmtpClient = New-Object System.Net.Mail.SmtpClient $smtpServer
    }
    else {
        $SmtpClient = $SmtpClientObject
    }
    $SmtpClient.Send($Message)
    $Message.Dispose()
}

Ends in 6 days