Skip to content

Commit 8f6982c

Browse files
ece-jacob-scottiSazonov
authored andcommitted
Made -Subject parameter of SendMail command no longer mandatory. (#8961)
1 parent eac3101 commit 8f6982c

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/Microsoft.PowerShell.Commands.Utility/commands/utility/Send-MailMessage.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ public sealed class SendMailMessage : PSCmdlet
117117
/// <summary>
118118
/// Gets or sets the subject of the email message.
119119
/// </summary>
120-
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true)]
120+
[Parameter(Mandatory = false, Position = 1, ValueFromPipelineByPropertyName = true)]
121121
[Alias("sub")]
122-
[ValidateNotNullOrEmpty]
123122
public string Subject { get; set; }
124123

125124
/// <summary>

test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ Describe "Send-MailMessage" -Tags CI, RequireSudoOnUnix {
6161
SmtpServer = "127.0.0.1"
6262
}
6363
}
64+
@{
65+
Name = "with No Subject"
66+
InputObject = @{
67+
From = "user01@example.com"
68+
To = "user02@example.com"
69+
ReplyTo = "noreply@example.com"
70+
Body = "Body $(Get-Date)"
71+
SmtpServer = "127.0.0.1"
72+
}
73+
}
6474
)
6575

6676
It "Can send mail message using named parameters <Name>" -TestCases $testCases {
@@ -78,7 +88,9 @@ Describe "Send-MailMessage" -Tags CI, RequireSudoOnUnix {
7888
$mail.Headers["From"] | Should -BeExactly $InputObject.From
7989
$mail.Headers["To"] | Should -BeExactly $InputObject.To
8090
$mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo
81-
$mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject
91+
If ($InputObject.Subject -ne $null) {
92+
$mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject
93+
}
8294

8395
$mail.MessageParts.Count | Should -BeExactly 1
8496
$mail.MessageParts[0].BodyData | Should -BeExactly $InputObject.Body
@@ -101,7 +113,9 @@ Describe "Send-MailMessage" -Tags CI, RequireSudoOnUnix {
101113
$mail.Headers["From"] | Should -BeExactly $InputObject.From
102114
$mail.Headers["To"] | Should -BeExactly $InputObject.To
103115
$mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo
104-
$mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject
116+
If ($InputObject.Subject -ne $null) {
117+
$mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject
118+
}
105119

106120
$mail.MessageParts.Count | Should -BeExactly 1
107121
$mail.MessageParts[0].BodyData | Should -BeExactly $InputObject.Body

0 commit comments

Comments
 (0)