Skip to content

[Robustness] ServyFailureEmail.ps1 — SmtpClient is never disposed; comment about 'PS 2.0 / .NET 3.5' is incorrect #884

@Christophe-Rogiers

Description

@Christophe-Rogiers

Severity: Warning

File: setup/taskschd/ServyFailureEmail.ps1

Lines: 183-188

} finally {
    if (\$null -ne \$mailMessage) { \$mailMessage.Dispose() }
    # .NET 3.5 SmtpClient doesn't implement IDisposable (PS 2.0 limitation)
    # but we null it for GC safety.
    \$smtp = \$null
}

The script declares #Requires -Version 3.0 (line 1), so the PowerShell 2.0 / .NET 3.5 reasoning in the inline comment does not apply. PowerShell 3.0+ runs on .NET 4.0+, where System.Net.Mail.SmtpClient implements IDisposable (added in .NET 4.0). The comment justifies skipping Dispose() based on a constraint that does not match this script's documented prerequisites.

SmtpClient holds an underlying TCP connection (and, with EnableSsl = \$true, an SSL stream). Without Dispose() the connection is closed only when finalization runs — under a Scheduled Task that runs every few minutes this can briefly leak SMTP sockets and SSL handles to the GC.

Same bug appears in setup/taskschd/ServyFailureNotification.ps1 if it constructs SmtpClient; please verify.

Suggested fix: Dispose the SmtpClient explicitly:

} finally {
    if (\$null -ne \$mailMessage) { \$mailMessage.Dispose() }
    if (\$null -ne \$smtp) { \$smtp.Dispose() }
}

And update the misleading comment, or remove it entirely.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions