@@ -147,6 +147,53 @@ function makeMessage
147147<# ---------------------------------------------------------------------------
148148 Run the demo.
149149 ---------------------------------------------------------------------------#>
150+
151+ # Use Send-MailMessage command to send an email
152+ Write-Host " Sending message 1..."
153+
154+ $messageSubject = " Test Subject 1"
155+ $messageBody = " This is a test message 1."
156+
157+ $tempPassword = ConvertTo-SecureString - String $outgoingPassword - AsPlainText - Force
158+ $credential = New-Object - TypeName " System.Management.Automation.PSCredential" `
159+ - ArgumentList $outgoingUsername , $tempPassword
160+
161+ Remove-Variable - Name tempPassword
162+
163+ $parameters = @ {
164+ " From" = $outgoingFromAddress ;
165+ " Subject" = $messageSubject ;
166+ " Body" = $messageBody ;
167+ " SmtpServer" = $outgoingServer ;
168+ " Port" = $outgoingPortSMTP ;
169+ " UseSsl" = $outgoingEnableSSL ;
170+ " Credential" = $credential
171+ }
172+
173+ if ( $outgoingToAddressList )
174+ {
175+ $parameters.add ( " To" , $outgoingToAddressList )
176+ }
177+
178+ if ( $outgoingCCAddressList )
179+ {
180+ $parameters.add ( " CC" , $outgoingCCAddressList )
181+ }
182+
183+ if ( $outgoingBCCAddressList )
184+ {
185+ $parameters.add ( " BCC" , $outgoingBCCAddressList )
186+ }
187+
188+ if ( $outgoingAttachmentURLList )
189+ {
190+ $parameters.add ( " Attachments" , $outgoingAttachmentURLList )
191+ }
192+
193+ Send-MailMessage @parameters
194+
195+
196+ # Use .NET Frameworks to send emails
150197Write-Host " Connecting to SMTP server: $outgoingServer `:$outgoingPortSMTP "
151198
152199$smtpClient = makeSMTPClient `
@@ -159,18 +206,18 @@ $outgoingMessage = makeMessage `
159206 $outgoingFromAddress $outgoingReplyToAddressList `
160207 $outgoingToAddressList $outgoingCCAddressList `
161208 $outgoingBCCAddressList $outgoingAttachmentURLList `
162- " Test Subject 1 " " This is test message 1 ." $false
209+ " Test Subject 2 " " This is test message 2 ." $false
163210
164211try {
165- Write-Host " Sending message 1 ..."
212+ Write-Host " Sending message 2 ..."
166213
167214 $smtpClient.send ( $outgoingMessage )
168215
169- Write-Host " Sending message 2 ..."
216+ Write-Host " Sending message 3 ..."
170217
171218 $smtpClient.send (
172219 $outgoingFromAddress , $outgoingToAddressList [0 ],
173- " Test Subject 2 " , " This is test message 2 ." )
220+ " Test Subject 3 " , " This is test message 3 ." )
174221 }
175222
176223catch { Write-Error " Caught SMTP client exception:`n`t $PSItem " }
0 commit comments