Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
[Parameter(ValueFromPipeline = true)]
[Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
[Alias("PsPath")]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
Expand All @@ -42,7 +42,7 @@ public String[] Attachments
/// Specifies the address collection that contains the
/// blind carbon copy (BCC) recipients for the e-mail message.
/// </summary>
[Parameter]
[Parameter(ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public String[] Bcc
Expand All @@ -58,7 +58,7 @@ public String[] Bcc
/// <summary>
/// Specifies the body (content) of the message
/// </summary>
[Parameter(Position = 2)]
[Parameter(Position = 2, ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public String Body
{
Expand All @@ -73,7 +73,7 @@ public String Body
/// <summary>
/// Specifies a value indicating whether the mail message body is in Html.
/// </summary>
[Parameter]
[Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("BAH")]
public SwitchParameter BodyAsHtml
{
Expand All @@ -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
/// </summary>
[Parameter()]
[Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("BE")]
[ValidateNotNullOrEmpty]
[ArgumentCompletions(
Expand All @@ -110,7 +110,7 @@ public SwitchParameter BodyAsHtml
/// Specifies the address collection that contains the
/// carbon copy (CC) recipients for the e-mail message.
/// </summary>
[Parameter]
[Parameter(ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Cc")]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
Expand All @@ -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
/// </summary>
[Parameter()]
[Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("DNO")]
[ValidateNotNullOrEmpty]
public DeliveryNotificationOptions DeliveryNotificationOption
Expand All @@ -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
/// </summary>
[Parameter(Mandatory = true)]
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public String From
{
Expand All @@ -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.
/// </summary>
[Parameter(Position = 3)]
[Parameter(Position = 3, ValueFromPipelineByPropertyName = true)]
[Alias("ComputerName")]
[ValidateNotNullOrEmpty]
public String SmtpServer
Expand All @@ -178,7 +178,7 @@ public String SmtpServer
/// <summary>
/// Specifies the priority of the email message. The valid values for this are Normal, High and Low
/// </summary>
[Parameter]
[Parameter(ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public MailPriority Priority
{
Expand All @@ -193,7 +193,7 @@ public MailPriority Priority
/// <summary>
/// Specifies the subject of the email message.
/// </summary>
[Parameter(Mandatory = true, Position = 1)]
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true)]
[Alias("sub")]
[ValidateNotNullOrEmpty]
public String Subject
Expand All @@ -209,7 +209,7 @@ public String Subject
/// <summary>
/// Specifies the To address for this e-mail message.
/// </summary>
[Parameter(Mandatory = true, Position = 0)]
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public String[] To
Expand All @@ -225,7 +225,7 @@ public String[] To
/// <summary>
/// Specifies the credential for this e-mail message.
/// </summary>
[Parameter()]
[Parameter(ValueFromPipelineByPropertyName = true)]
[Credential]
[ValidateNotNullOrEmpty]
public PSCredential Credential
Expand All @@ -241,7 +241,7 @@ public PSCredential Credential
/// <summary>
/// Specifies if Secured layer is required or not
/// </summary>
[Parameter()]
[Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter UseSsl
{
get { return _usessl; }
Expand All @@ -258,7 +258,7 @@ public SwitchParameter UseSsl
/// <remarks>
/// Value must be greater than zero.
/// </remarks>
[Parameter()]
[Parameter(ValueFromPipelineByPropertyName = true)]
[ValidateRange(0, Int32.MaxValue)]
public int Port
{
Expand Down Expand Up @@ -389,7 +389,7 @@ protected override
this.ThrowTerminatingError(er);
}

if (0 == _port)
if (_port == 0)
{
_mSmtpClient = new SmtpClient(_smtpserver);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest using distinguishable strings in this test i.e. "Greetings from me again" instead of "Greetings from me", "Second test message" instead of "Test message.

$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
}
}