From cf1bbc4d04d186dfedf28947899deac2bd183d61 Mon Sep 17 00:00:00 2001 From: ThreeFive-O Date: Sat, 23 Mar 2019 21:45:07 +0100 Subject: [PATCH 1/4] [feature] Add feature tests for Send-MailMessage --- .../Send-MailMessage.Tests.ps1 | 232 ++++++++++++++++-- 1 file changed, 209 insertions(+), 23 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 9b003a208dd..0676e83db8d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 @@ -1,16 +1,19 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -Describe "Send-MailMessage" -Tags CI, RequireSudoOnUnix { - BeforeAll { - Register-PackageSource -Name nuget.org -Location https://api.nuget.org/v3/index.json -ProviderName NuGet -ErrorAction SilentlyContinue +if(-not ("netDumbster.smtp.SimpleSmtpServer" -as [type])) +{ + Register-PackageSource -Name nuget.org -Location https://api.nuget.org/v3/index.json -ProviderName NuGet -ErrorAction SilentlyContinue - $nugetPackage = "netDumbster" - Install-Package -Name $nugetPackage -ProviderName NuGet -Scope CurrentUser -Force -Source 'nuget.org' + $nugetPackage = "netDumbster" + Install-Package -Name $nugetPackage -ProviderName NuGet -Scope CurrentUser -Force -Source 'nuget.org' - $dll = "$(Split-Path (Get-Package $nugetPackage).Source)\lib\netstandard2.0\netDumbster.dll" - Add-Type -Path $dll + $dll = "$(Split-Path (Get-Package $nugetPackage).Source)\lib\netstandard2.0\netDumbster.dll" + Add-Type -Path $dll +} +Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { + BeforeAll { $server = [netDumbster.smtp.SimpleSmtpServer]::Start(25) function Read-Mail @@ -61,45 +64,91 @@ Describe "Send-MailMessage" -Tags CI, RequireSudoOnUnix { SmtpServer = "127.0.0.1" } } + @{ + Name = "with multiple To" + InputObject = @{ + From = "user01@example.com" + To = "user02@example.com","user03@example.com","user04@example.com" + Subject = "Subject $(Get-Date)" + Body = "Body $(Get-Date)" + SmtpServer = "127.0.0.1" + } + } + @{ + Name = "with multiple Cc" + InputObject = @{ + From = "user01@example.com" + To = "user02@example.com" + Cc = "user03@example.com","user04@example.com" + Subject = "Subject $(Get-Date)" + Body = "Body $(Get-Date)" + SmtpServer = "127.0.0.1" + } + } + @{ + Name = "with multiple Bcc" + InputObject = @{ + From = "user01@example.com" + To = "user02@example.com" + Bcc = "user03@example.com","user04@example.com" + Subject = "Subject $(Get-Date)" + Body = "Body $(Get-Date)" + SmtpServer = "127.0.0.1" + } + } @{ Name = "with No Subject" InputObject = @{ From = "user01@example.com" To = "user02@example.com" - ReplyTo = "noreply@example.com" Body = "Body $(Get-Date)" SmtpServer = "127.0.0.1" } } ) - It "Can send mail message using named parameters " -TestCases $testCases { - param($InputObject) - + It "Shows obsolete message for cmdlet" { $server | Should -Not -Be $null $powershell = [PowerShell]::Create() - $null = $powershell.AddCommand("Send-MailMessage").AddParameters($InputObject).AddParameter("ErrorAction","SilentlyContinue") + $null = $powershell.AddCommand("Send-MailMessage").AddParameters($testCases[0].InputObject).AddParameter("ErrorAction","SilentlyContinue") $powershell.Invoke() $warnings = $powershell.Streams.Warning $warnings.count | Should -BeGreaterThan 0 - $warnings[0].ToString() | Should -BeLike "The command 'Send-MailMessage' is obsolete. *" + $warnings[0].ToString() | Should -BeLike "The command 'Send-MailMessage' is obsolete. *" + } + + It "Can send mail message using named parameters " -TestCases $testCases { + param($InputObject) + + $server | Should -Not -Be $null + + Send-MailMessage @InputObject -ErrorAction SilentlyContinue $mail = Read-Mail $mail.FromAddress | Should -BeExactly $InputObject.From - $mail.ToAddresses | Should -BeExactly $InputObject.To + $mail.ToAddresses | Should -BeIn ([array]$InputObject.To + $InputObject.Cc + $InputObject.Bcc) $mail.Headers["From"] | Should -BeExactly $InputObject.From - $mail.Headers["To"] | Should -BeExactly $InputObject.To - $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo - If ($InputObject.Subject -ne $null) { - $mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject + $mail.Headers["To"].Split(", ") | Should -BeExactly $InputObject.To + If($mail.Headers["Cc"]) + { + $mail.Headers["Cc"].Split(", ") | Should -BeExactly $InputObject.Cc + } + If($mail.Headers["Bcc"]) + { + $mail.Headers["Bcc"].Split(", ") | Should -BeExactly $InputObject.Bcc + } + If($mail.Headers["Reply-To"]) + { + $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo } + $mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject $mail.MessageParts.Count | Should -BeExactly 1 $mail.MessageParts[0].BodyData | Should -BeExactly $InputObject.Body @@ -117,16 +166,153 @@ Describe "Send-MailMessage" -Tags CI, RequireSudoOnUnix { $mail = Read-Mail $mail.FromAddress | Should -BeExactly $InputObject.From - $mail.ToAddresses | Should -BeExactly $InputObject.To + $mail.ToAddresses | Should -BeIn ([array]$InputObject.To + $InputObject.Cc + $InputObject.Bcc) $mail.Headers["From"] | Should -BeExactly $InputObject.From - $mail.Headers["To"] | Should -BeExactly $InputObject.To - $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo - If ($InputObject.Subject -ne $null) { - $mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject + $mail.Headers["To"].Split(", ") | Should -BeExactly $InputObject.To + If($mail.Headers["Cc"]) + { + $mail.Headers["Cc"].Split(", ") | Should -BeExactly $InputObject.Cc + } + If($mail.Headers["Bcc"]) + { + $mail.Headers["Bcc"].Split(", ") | Should -BeExactly $InputObject.Bcc } + If($mail.Headers["Reply-To"]) + { + $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo + } + $mail.Headers["Subject"] | Should -BeExactly $InputObject.Subject $mail.MessageParts.Count | Should -BeExactly 1 $mail.MessageParts[0].BodyData | Should -BeExactly $InputObject.Body } } + +Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { + BeforeEach { + $server = [netDumbster.smtp.SimpleSmtpServer]::Start(25) + + function Read-Mail + { + param() + + if($server) + { + return $server.ReceivedEmail[0] + } + return $null + } + } + + AfterEach { + if($server) + { + $server.Stop() + } + } + + $InputObject = @{ + From = "user01@example.com" + To = "user02@example.com" + Subject = "Subject $(Get-Date)" + Body = "Body $(Get-Date)" + SmtpServer = "127.0.0.1" + } + + It "Can send mail message using custom port 2525" { + $server.Stop() + $customPortServer = [netDumbster.smtp.SimpleSmtpServer]::Start(2525) + + $customPortServer | Should -Not -Be $null + $customPortServer.ReceivedEmailCount | Should -BeExactly 0 + + Send-MailMessage @InputObject -Port 2525 -ErrorAction SilentlyContinue + + $customPortServer.ReceivedEmailCount | Should -BeExactly 1 + $customPortServer.Stop() + } + + It "Can throw on wrong mail addresses" { + $server | Should -Not -Be $null + + $obj = $InputObject.Clone() + $obj.To = "not_a_valid_mail.address" + + { Send-MailMessage @obj -ErrorAction Stop } | Should -Throw -ErrorId "FormatException,Microsoft.PowerShell.Commands.SendMailMessage" + } + + It "Can send mail with free-form email address" { + $server | Should -Not -Be $null + + $obj = $InputObject.Clone() + $obj.From = "User01 " + $obj.To = "User02 " + + Send-MailMessage @obj -ErrorAction SilentlyContinue + + $mail = Read-Mail + + $mail.FromAddress | Should -BeExactly "user01@example.com" + $mail.ToAddresses | Should -BeExactly "user02@example.com" + } + + It "Can send mail with high priority" { + $server | Should -Not -Be $null + + Send-MailMessage @InputObject -Priority High -ErrorAction SilentlyContinue + + $mail = Read-Mail + $mail.Priority | Should -BeExactly "urgent" + } + + It "Can send mail with body as HTML" { + $server | Should -Not -Be $null + + $obj = $InputObject.Clone() + $obj.Body = "

PowerShell

" + + Send-MailMessage @obj -BodyAsHtml -Encoding utf8 -ErrorAction SilentlyContinue + + $mail = Read-Mail + $mail.MessageParts.Count | Should -BeExactly 1 + $mail.MessageParts[0].BodyData | Should -Be $obj.Body + } + + It "Can send mail with UTF8 encoding" { + $server | Should -Not -Be $null + + $obj = $InputObject.Clone() + $obj.Body = "We ❤ PowerShell" + + Send-MailMessage @obj -Encoding utf8Bom -ErrorAction SilentlyContinue + + $mail = Read-Mail + $mail.MessageParts.Count | Should -BeExactly 1 + $mail.Headers["content-transfer-encoding"] | Should -BeExactly "base64" + $utf8Text = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($mail.MessageParts[0].BodyData)) + $utf8Text | Should -Be $obj.Body + } + + It "Can send mail with attachments" { + $attachment1 = "TestDrive:\attachment1.txt" + $attachment2 = "TestDrive:\attachment2.txt" + + $pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAnElEQVR42u3RAQ0AAAgDoL9/aK3hHFSgyUw4o0KEIEQIQoQgRAhChAgRghAhCBGCECEIEYIQhAhBiBCECEGIEIQgRAhChCBECEKEIAQhQhAiBCFCECIEIQgRghAhCBGCECEIQYgQhAhBiBCECEEIQoQgRAhChCBECEIQIgQhQhAiBCFCEIIQIQgRghAhCBGCECFChCBECEKEIOS7BU5Hx50BmcQaAAAAAElFTkSuQmCC" + + Set-Content $attachment1 -Value "First attachment" + Set-Content $attachment2 -AsByteStream -Value ([Convert]::FromBase64String($pngBase64)) + + $server | Should -Not -Be $null + + Send-MailMessage @InputObject -Attachments $attachment1,$attachment2 -ErrorAction SilentlyContinue + + $mail = Read-Mail + $mail.MessageParts.Count | Should -BeExactly 3 + + $txt = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($mail.MessageParts[1].BodyData)) -replace "`n|`r" + $txt | Should -BeExactly "First attachment" + + ($mail.MessageParts[2].BodyData -replace "`n|`r") | Should -BeExactly $pngBase64 + } +} From a5ec64e8a569ec242b51ffb24b0e47dbac1422ac Mon Sep 17 00:00:00 2001 From: ThreeFive-O Date: Wed, 27 Mar 2019 11:05:38 +0100 Subject: [PATCH 2/4] Fix comments and add additional checks --- .../Send-MailMessage.Tests.ps1 | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 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 0676e83db8d..b5879125067 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 @@ -132,19 +132,18 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { $mail = Read-Mail $mail.FromAddress | Should -BeExactly $InputObject.From + $mail.ToAddresses.Count | Should -BeExactly ($InputObject.To.Count + $InputObject.Cc.Count + $InputObject.Bcc.Count) $mail.ToAddresses | Should -BeIn ([array]$InputObject.To + $InputObject.Cc + $InputObject.Bcc) $mail.Headers["From"] | Should -BeExactly $InputObject.From + $mail.Headers["To"] | Should -Not -BeNullOrEmpty $mail.Headers["To"].Split(", ") | Should -BeExactly $InputObject.To - If($mail.Headers["Cc"]) + If($InputObject.Cc) { + $mail.Headers["Cc"] | Should -Not -BeNullOrEmpty $mail.Headers["Cc"].Split(", ") | Should -BeExactly $InputObject.Cc } - If($mail.Headers["Bcc"]) - { - $mail.Headers["Bcc"].Split(", ") | Should -BeExactly $InputObject.Bcc - } - If($mail.Headers["Reply-To"]) + If($InputObject.ReplyTo) { $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo } @@ -166,19 +165,18 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { $mail = Read-Mail $mail.FromAddress | Should -BeExactly $InputObject.From + $mail.ToAddresses.Count | Should -BeExactly ($InputObject.To.Count + $InputObject.Cc.Count + $InputObject.Bcc.Count) $mail.ToAddresses | Should -BeIn ([array]$InputObject.To + $InputObject.Cc + $InputObject.Bcc) $mail.Headers["From"] | Should -BeExactly $InputObject.From + $mail.Headers["To"] | Should -Not -BeNullOrEmpty $mail.Headers["To"].Split(", ") | Should -BeExactly $InputObject.To - If($mail.Headers["Cc"]) + If($InputObject.Cc) { + $mail.Headers["Cc"] | Should -Not -BeNullOrEmpty $mail.Headers["Cc"].Split(", ") | Should -BeExactly $InputObject.Cc } - If($mail.Headers["Bcc"]) - { - $mail.Headers["Bcc"].Split(", ") | Should -BeExactly $InputObject.Bcc - } - If($mail.Headers["Reply-To"]) + If($InputObject.ReplyTo) { $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo } @@ -266,7 +264,7 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { $mail.Priority | Should -BeExactly "urgent" } - It "Can send mail with body as HTML" { + It "Can send mail with HTML content as body" { $server | Should -Not -Be $null $obj = $InputObject.Clone() @@ -275,6 +273,7 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { Send-MailMessage @obj -BodyAsHtml -Encoding utf8 -ErrorAction SilentlyContinue $mail = Read-Mail + $mail.Headers["content-type"] | Should -BeLike "text/html*" $mail.MessageParts.Count | Should -BeExactly 1 $mail.MessageParts[0].BodyData | Should -Be $obj.Body } From 1673be0b5f7f6a52c8002f5018cf9324a23cffc4 Mon Sep 17 00:00:00 2001 From: ThreeFive-O Date: Tue, 9 Apr 2019 20:21:49 +0200 Subject: [PATCH 3/4] Add comment for testing BCC addresses --- .../Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) 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 b5879125067..7738713f86a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 @@ -143,6 +143,7 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { $mail.Headers["Cc"] | Should -Not -BeNullOrEmpty $mail.Headers["Cc"].Split(", ") | Should -BeExactly $InputObject.Cc } + # BCC addresses can't be tested against mail message header, as this information is by design never included in the mail message itself. If($InputObject.ReplyTo) { $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo @@ -176,6 +177,7 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { $mail.Headers["Cc"] | Should -Not -BeNullOrEmpty $mail.Headers["Cc"].Split(", ") | Should -BeExactly $InputObject.Cc } + # BCC addresses can't be tested against mail message header, as this information is by design never included in the mail message itself. If($InputObject.ReplyTo) { $mail.Headers["Reply-To"] | Should -BeExactly $InputObject.ReplyTo From e0679eecf7757ab238dcf0e3f7c754acd88a8dd7 Mon Sep 17 00:00:00 2001 From: ThreeFive-O Date: Wed, 10 Apr 2019 19:43:03 +0200 Subject: [PATCH 4/4] Group feature tests by Context --- .../Send-MailMessage.Tests.ps1 | 184 ++++++++++-------- 1 file changed, 99 insertions(+), 85 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 7738713f86a..723e2dc05e9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Send-MailMessage.Tests.ps1 @@ -12,6 +12,14 @@ if(-not ("netDumbster.smtp.SimpleSmtpServer" -as [type])) Add-Type -Path $dll } +$DefaultInputObject = @{ + From = "user01@example.com" + To = "user02@example.com" + Subject = "Subject $(Get-Date)" + Body = "Body $(Get-Date)" + SmtpServer = "127.0.0.1" +} + Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { BeforeAll { $server = [netDumbster.smtp.SimpleSmtpServer]::Start(25) @@ -28,7 +36,7 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { } } - AfterEach { + BeforeEach { if($server) { $server.ClearReceivedEmail() @@ -45,13 +53,7 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { $testCases = @( @{ Name = "with mandatory parameters" - InputObject = @{ - From = "user01@example.com" - To = "user02@example.com" - Subject = "Subject $(Get-Date)" - Body = "Body $(Get-Date)" - SmtpServer = "127.0.0.1" - } + InputObject = $DefaultInputObject } @{ Name = "with ReplyTo" @@ -190,9 +192,7 @@ Describe "Send-MailMessage DRT Unit Tests" -Tags CI, RequireSudoOnUnix { } Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { - BeforeEach { - $server = [netDumbster.smtp.SimpleSmtpServer]::Start(25) - + BeforeAll { function Read-Mail { param() @@ -205,115 +205,129 @@ Describe "Send-MailMessage Feature Tests" -Tags Feature, RequireSudoOnUnix { } } - AfterEach { + BeforeEach { if($server) { - $server.Stop() + $server.ClearReceivedEmail() } } - $InputObject = @{ - From = "user01@example.com" - To = "user02@example.com" - Subject = "Subject $(Get-Date)" - Body = "Body $(Get-Date)" - SmtpServer = "127.0.0.1" - } + Context "Default Port 25" { + BeforeAll { + $server = [netDumbster.smtp.SimpleSmtpServer]::Start(25) + } - It "Can send mail message using custom port 2525" { - $server.Stop() - $customPortServer = [netDumbster.smtp.SimpleSmtpServer]::Start(2525) + AfterAll { + if($server) + { + $server.Stop() + } + } - $customPortServer | Should -Not -Be $null - $customPortServer.ReceivedEmailCount | Should -BeExactly 0 + It "Can throw on wrong mail addresses" { + $server | Should -Not -Be $null - Send-MailMessage @InputObject -Port 2525 -ErrorAction SilentlyContinue + $obj = $DefaultInputObject.Clone() + $obj.To = "not_a_valid_mail.address" - $customPortServer.ReceivedEmailCount | Should -BeExactly 1 - $customPortServer.Stop() - } + { Send-MailMessage @obj -ErrorAction Stop } | Should -Throw -ErrorId "FormatException,Microsoft.PowerShell.Commands.SendMailMessage" + } - It "Can throw on wrong mail addresses" { - $server | Should -Not -Be $null + It "Can send mail with free-form email address" { + $server | Should -Not -Be $null - $obj = $InputObject.Clone() - $obj.To = "not_a_valid_mail.address" + $obj = $DefaultInputObject.Clone() + $obj.From = "User01 " + $obj.To = "User02 " - { Send-MailMessage @obj -ErrorAction Stop } | Should -Throw -ErrorId "FormatException,Microsoft.PowerShell.Commands.SendMailMessage" - } + Send-MailMessage @obj -ErrorAction SilentlyContinue - It "Can send mail with free-form email address" { - $server | Should -Not -Be $null + $mail = Read-Mail - $obj = $InputObject.Clone() - $obj.From = "User01 " - $obj.To = "User02 " + $mail.FromAddress | Should -BeExactly "user01@example.com" + $mail.ToAddresses | Should -BeExactly "user02@example.com" + } - Send-MailMessage @obj -ErrorAction SilentlyContinue + It "Can send mail with high priority" { + $server | Should -Not -Be $null - $mail = Read-Mail + Send-MailMessage @DefaultInputObject -Priority High -ErrorAction SilentlyContinue - $mail.FromAddress | Should -BeExactly "user01@example.com" - $mail.ToAddresses | Should -BeExactly "user02@example.com" - } + $mail = Read-Mail + $mail.Priority | Should -BeExactly "urgent" + } - It "Can send mail with high priority" { - $server | Should -Not -Be $null + It "Can send mail with HTML content as body" { + $server | Should -Not -Be $null - Send-MailMessage @InputObject -Priority High -ErrorAction SilentlyContinue + $obj = $DefaultInputObject.Clone() + $obj.Body = "

PowerShell

" - $mail = Read-Mail - $mail.Priority | Should -BeExactly "urgent" - } + Send-MailMessage @obj -BodyAsHtml -Encoding utf8 -ErrorAction SilentlyContinue - It "Can send mail with HTML content as body" { - $server | Should -Not -Be $null + $mail = Read-Mail + $mail.Headers["content-type"] | Should -BeLike "text/html*" + $mail.MessageParts.Count | Should -BeExactly 1 + $mail.MessageParts[0].BodyData | Should -Be $obj.Body + } - $obj = $InputObject.Clone() - $obj.Body = "

PowerShell

" + It "Can send mail with UTF8 encoding" { + $server | Should -Not -Be $null - Send-MailMessage @obj -BodyAsHtml -Encoding utf8 -ErrorAction SilentlyContinue + $obj = $DefaultInputObject.Clone() + $obj.Body = "We ❤ PowerShell" - $mail = Read-Mail - $mail.Headers["content-type"] | Should -BeLike "text/html*" - $mail.MessageParts.Count | Should -BeExactly 1 - $mail.MessageParts[0].BodyData | Should -Be $obj.Body - } + Send-MailMessage @obj -Encoding utf8Bom -ErrorAction SilentlyContinue - It "Can send mail with UTF8 encoding" { - $server | Should -Not -Be $null + $mail = Read-Mail + $mail.MessageParts.Count | Should -BeExactly 1 + $mail.Headers["content-transfer-encoding"] | Should -BeExactly "base64" + $utf8Text = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($mail.MessageParts[0].BodyData)) + $utf8Text | Should -Be $obj.Body + } - $obj = $InputObject.Clone() - $obj.Body = "We ❤ PowerShell" + It "Can send mail with attachments" { + $attachment1 = "TestDrive:\attachment1.txt" + $attachment2 = "TestDrive:\attachment2.txt" - Send-MailMessage @obj -Encoding utf8Bom -ErrorAction SilentlyContinue + $pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAnElEQVR42u3RAQ0AAAgDoL9/aK3hHFSgyUw4o0KEIEQIQoQgRAhChAgRghAhCBGCECEIEYIQhAhBiBCECEGIEIQgRAhChCBECEKEIAQhQhAiBCFCECIEIQgRghAhCBGCECEIQYgQhAhBiBCECEEIQoQgRAhChCBECEIQIgQhQhAiBCFCEIIQIQgRghAhCBGCECFChCBECEKEIOS7BU5Hx50BmcQaAAAAAElFTkSuQmCC" - $mail = Read-Mail - $mail.MessageParts.Count | Should -BeExactly 1 - $mail.Headers["content-transfer-encoding"] | Should -BeExactly "base64" - $utf8Text = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($mail.MessageParts[0].BodyData)) - $utf8Text | Should -Be $obj.Body - } + Set-Content $attachment1 -Value "First attachment" + Set-Content $attachment2 -AsByteStream -Value ([Convert]::FromBase64String($pngBase64)) - It "Can send mail with attachments" { - $attachment1 = "TestDrive:\attachment1.txt" - $attachment2 = "TestDrive:\attachment2.txt" + $server | Should -Not -Be $null - $pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAnElEQVR42u3RAQ0AAAgDoL9/aK3hHFSgyUw4o0KEIEQIQoQgRAhChAgRghAhCBGCECEIEYIQhAhBiBCECEGIEIQgRAhChCBECEKEIAQhQhAiBCFCECIEIQgRghAhCBGCECEIQYgQhAhBiBCECEEIQoQgRAhChCBECEIQIgQhQhAiBCFCEIIQIQgRghAhCBGCECFChCBECEKEIOS7BU5Hx50BmcQaAAAAAElFTkSuQmCC" + Send-MailMessage @DefaultInputObject -Attachments $attachment1,$attachment2 -ErrorAction SilentlyContinue - Set-Content $attachment1 -Value "First attachment" - Set-Content $attachment2 -AsByteStream -Value ([Convert]::FromBase64String($pngBase64)) + $mail = Read-Mail + $mail.MessageParts.Count | Should -BeExactly 3 - $server | Should -Not -Be $null + $txt = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($mail.MessageParts[1].BodyData)) -replace "`n|`r" + $txt | Should -BeExactly "First attachment" - Send-MailMessage @InputObject -Attachments $attachment1,$attachment2 -ErrorAction SilentlyContinue + ($mail.MessageParts[2].BodyData -replace "`n|`r") | Should -BeExactly $pngBase64 + } + } - $mail = Read-Mail - $mail.MessageParts.Count | Should -BeExactly 3 + Context "Custom Port 2525" { + BeforeAll { + $server = [netDumbster.smtp.SimpleSmtpServer]::Start(2525) + } - $txt = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($mail.MessageParts[1].BodyData)) -replace "`n|`r" - $txt | Should -BeExactly "First attachment" + AfterAll { + if($server) + { + $server.Stop() + } + } - ($mail.MessageParts[2].BodyData -replace "`n|`r") | Should -BeExactly $pngBase64 + It "Can send mail message using custom port 2525" { + $server | Should -Not -Be $null + $server.ReceivedEmailCount | Should -BeExactly 0 + + Send-MailMessage @DefaultInputObject -Port 2525 -ErrorAction SilentlyContinue + + $server.ReceivedEmailCount | Should -BeExactly 1 + } } }