From cab5c84c1d2b537c15fcd84eea581b954ef545f3 Mon Sep 17 00:00:00 2001 From: sethvs Date: Mon, 21 May 2018 18:40:26 +0300 Subject: [PATCH 1/4] Add "ValueFromPipelineByPropertyName = true" to all of the parameters of the Send-MailMessage cmdlet. --- .../commands/utility/Send-MailMessage.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Send-MailMessage.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Send-MailMessage.cs index 0b6651920aa..8a58518b9b4 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Send-MailMessage.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Send-MailMessage.cs @@ -24,7 +24,7 @@ public sealed class SendMailMessage : PSCmdlet /// If the filename specified can not be found, then the relevant error /// message should be thrown. /// - [Parameter(ValueFromPipeline = true)] + [Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] [ValidateNotNullOrEmpty] [Alias("PsPath")] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] @@ -42,7 +42,7 @@ public String[] Attachments /// Specifies the address collection that contains the /// blind carbon copy (BCC) recipients for the e-mail message. /// - [Parameter] + [Parameter(ValueFromPipelineByPropertyName = true)] [ValidateNotNullOrEmpty] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public String[] Bcc @@ -58,7 +58,7 @@ public String[] Bcc /// /// Specifies the body (content) of the message /// - [Parameter(Position = 2)] + [Parameter(Position = 2, ValueFromPipelineByPropertyName = true)] [ValidateNotNullOrEmpty] public String Body { @@ -73,7 +73,7 @@ public String Body /// /// Specifies a value indicating whether the mail message body is in Html. /// - [Parameter] + [Parameter(ValueFromPipelineByPropertyName = true)] [Alias("BAH")] public SwitchParameter BodyAsHtml { @@ -89,7 +89,7 @@ public SwitchParameter BodyAsHtml /// Specifies the encoding used for the content of the body and also the subject. /// This is set to ASCII to ensure there are no problems with any email server /// - [Parameter()] + [Parameter(ValueFromPipelineByPropertyName = true)] [Alias("BE")] [ValidateNotNullOrEmpty] [ArgumentCompletions( @@ -110,7 +110,7 @@ public SwitchParameter BodyAsHtml /// Specifies the address collection that contains the /// carbon copy (CC) recipients for the e-mail message. /// - [Parameter] + [Parameter(ValueFromPipelineByPropertyName = true)] [ValidateNotNullOrEmpty] [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Cc")] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] @@ -128,7 +128,7 @@ public String[] Cc /// Specifies the delivery notifications options for the e-mail message. The various /// option available for this parameter are None, OnSuccess, OnFailure, Delay and Never /// - [Parameter()] + [Parameter(ValueFromPipelineByPropertyName = true)] [Alias("DNO")] [ValidateNotNullOrEmpty] public DeliveryNotificationOptions DeliveryNotificationOption @@ -145,7 +145,7 @@ public DeliveryNotificationOptions DeliveryNotificationOption /// Specifies the from address for this e-mail message. The default value for /// this parameter is the email address of the currently logged on user /// - [Parameter(Mandatory = true)] + [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true)] [ValidateNotNullOrEmpty] public String From { @@ -162,7 +162,7 @@ public String From /// to the Powershell variable PSEmailServer,if this host can not reached an appropriate error /// message will be displayed. /// - [Parameter(Position = 3)] + [Parameter(Position = 3, ValueFromPipelineByPropertyName = true)] [Alias("ComputerName")] [ValidateNotNullOrEmpty] public String SmtpServer @@ -178,7 +178,7 @@ public String SmtpServer /// /// Specifies the priority of the email message. The valid values for this are Normal, High and Low /// - [Parameter] + [Parameter(ValueFromPipelineByPropertyName = true)] [ValidateNotNullOrEmpty] public MailPriority Priority { @@ -193,7 +193,7 @@ public MailPriority Priority /// /// Specifies the subject of the email message. /// - [Parameter(Mandatory = true, Position = 1)] + [Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true)] [Alias("sub")] [ValidateNotNullOrEmpty] public String Subject @@ -209,7 +209,7 @@ public String Subject /// /// Specifies the To address for this e-mail message. /// - [Parameter(Mandatory = true, Position = 0)] + [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)] [ValidateNotNullOrEmpty] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public String[] To @@ -225,7 +225,7 @@ public String[] To /// /// Specifies the credential for this e-mail message. /// - [Parameter()] + [Parameter(ValueFromPipelineByPropertyName = true)] [Credential] [ValidateNotNullOrEmpty] public PSCredential Credential @@ -241,7 +241,7 @@ public PSCredential Credential /// /// Specifies if Secured layer is required or not /// - [Parameter()] + [Parameter(ValueFromPipelineByPropertyName = true)] public SwitchParameter UseSsl { get { return _usessl; } @@ -258,7 +258,7 @@ public SwitchParameter UseSsl /// /// Value must be greater than zero. /// - [Parameter()] + [Parameter(ValueFromPipelineByPropertyName = true)] [ValidateRange(0, Int32.MaxValue)] public int Port { From dcc8425565c8a06d3c4fa909061c2f99b67cacf9 Mon Sep 17 00:00:00 2001 From: sethvs Date: Mon, 21 May 2018 18:52:08 +0300 Subject: [PATCH 2/4] Fix CodeFactor issue. --- .../commands/utility/Send-MailMessage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Send-MailMessage.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Send-MailMessage.cs index 8a58518b9b4..4c3be4da07a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Send-MailMessage.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Send-MailMessage.cs @@ -389,7 +389,7 @@ protected override this.ThrowTerminatingError(er); } - if (0 == _port) + if (_port == 0) { _mSmtpClient = new SmtpClient(_smtpserver); } From 52f12e4064f72fd309fb0fd0b2832e079d57bfac Mon Sep 17 00:00:00 2001 From: sethvs Date: Wed, 23 May 2018 11:09:51 +0300 Subject: [PATCH 3/4] Add test. --- .../Send-MailMessage.Tests.ps1 | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 index 680bb205a3c..dff76420103 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 @@ -87,7 +87,7 @@ Describe "Basic Send-MailMessage tests" -Tags CI { return $rv } - $PesterArgs = @{ Name = "Can send mail message from user to self"} + $PesterArgs = @{Name = ""} $alreadyHasMail = $true if (-not $IsLinux) @@ -126,14 +126,18 @@ Describe "Basic Send-MailMessage tests" -Tags CI { } $alreadyHasMail = $false } - AfterAll { + + AfterEach { if (-not $alreadyHasMail) { Set-Content -Value "" -Path $mailBox -Force -ErrorAction SilentlyContinue } } - It @PesterArgs { + $ItArgs = $PesterArgs.Clone() + $ItArgs['Name'] = "Can send mail message from user to self " + $ItArgs['Name'] + + It @ItArgs { $body = "Greetings from me." $subject = "Test message" Send-MailMessage -To $address -From $address -Subject $subject -Body $body -SmtpServer 127.0.0.1 @@ -146,4 +150,22 @@ Describe "Basic Send-MailMessage tests" -Tags CI { $mail.Body.Count | Should -BeExactly 1 $mail.Body[0] | Should -BeExactly $body } + + $ItArgs = $PesterArgs.Clone() + $ItArgs['Name'] = "Can send mail message from user to self using pipeline " + $ItArgs['Name'] + + It @ItArgs { + $body = "Greetings from me." + $subject = "Test message" + $object = [PSCustomObject]@{"To" = $address; "From" = $address; "Subject" = $subject; "Body" = $body; $SmtpServer = '127.0.0.1'} + $object | Send-MailMessage + Test-Path -Path $mailBox | Should -BeTrue + $mail = read-mail $mailBox + $mail.From | Should -BeExactly $address + $mail.To.Count | Should -BeExactly 1 + $mail.To[0] | Should -BeExactly $address + $mail.Subject | Should -BeExactly $subject + $mail.Body.Count | Should -BeExactly 1 + $mail.Body[0] | Should -BeExactly $body + } } From e9356d7611e85f010cbd309b1938b84dfbf00ca1 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 14 Jun 2018 23:19:04 -0700 Subject: [PATCH 4/4] Fix indentation --- .../Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 index dff76420103..9f419609ae4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 @@ -134,8 +134,8 @@ Describe "Basic Send-MailMessage tests" -Tags CI { } } - $ItArgs = $PesterArgs.Clone() - $ItArgs['Name'] = "Can send mail message from user to self " + $ItArgs['Name'] + $ItArgs = $PesterArgs.Clone() + $ItArgs['Name'] = "Can send mail message from user to self " + $ItArgs['Name'] It @ItArgs { $body = "Greetings from me."