Skip to content
Merged
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
18 changes: 14 additions & 4 deletions PoshMailKit/SendMKMailMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using MailKit;
using MailKit.Security;
using System.Net;
using System;

namespace PoshMailKit
{
Expand Down Expand Up @@ -76,11 +77,9 @@ public string[]
[Alias("ComputerName")]
[Parameter(
ParameterSetName = "Modern",
Mandatory = true,
Position = 3)]
[Parameter(
ParameterSetName = "Legacy",
Mandatory = true,
Position = 3)]
public string
SmtpServer { get; set; }
Expand Down Expand Up @@ -181,7 +180,7 @@ public SwitchParameter

protected override void BeginProcessing()
{
ProcessParameterSets();
ProcessParameters();
ProcessAttachments();
}

Expand Down Expand Up @@ -216,8 +215,19 @@ protected override void ProcessRecord()
processor.SendMailMessage();
}

private void ProcessParameterSets()
private void ProcessParameters()
{
if (string.IsNullOrEmpty(SmtpServer))
SmtpServer = (string)SessionState.PSVariable.Get("PSEmailServer").Value;
if (string.IsNullOrEmpty(SmtpServer))
{
string errorMessage = "The email cannot be sent because no SMTP server was specified. " +
"You must specify an SMTP server by using either the SmtpServer parameter or the $PSEmailServer variable.";
InvalidOperationException exception = new InvalidOperationException(errorMessage);
ErrorRecord errorRecord = new ErrorRecord(exception, "", ErrorCategory.InvalidArgument, null);
ThrowTerminatingError(errorRecord);
}

if (ParameterSetName == "Legacy")
{
SetLegacySsl();
Expand Down