Patrick O Reilly
Patrick O Reilly

Reputation: 63

Send email with PowerShell

I have a function that is able to send email by sending it through PowerShell.

Using the System.Management.Automation reference I am able to use the PowerShell class that allows me to add PowerShell script that will send the email.

If I were to type it directly into the PowerShell window it would look like this:

$password = ConvertTo-SecureString 'PASSWORD' -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential('[email protected]', $password)
Send-MailMessage -From '[email protected]' -To '[email protected]' -Subject 'Heres the Email Subject' -Body 'This is what I want to say' -SmtpServer 'smtp.office365.com' -Port '587' -UseSsl -Credential $Cred –DeliveryNotificationOption OnSuccess

It is able to send an email but I how would I check if an email wasn't sent?

The function is below.

private void SendEmail()
{
    string from = "[email protected]";
    string to = "[email protected]";
    string subject = "Heres the Email Subject";
    string body = "This is what I want to say";
    string server = "smtp.office365.com";
    string port = "587";

    //Password goes here
    string password = "PASSWORD";
    string pw = "ConvertTo-SecureString '" + password + "' -AsPlainText  -Force";

    string cred = "New-Object System.Management.Automation.PSCredential('" + from + "', $password)";
    string send = "Send-MailMessage -From '" + from + "' -To '" + to + "' -Subject '" + subject + "' -Body '" + body + "' -SmtpServer '" + server + "' -Port '" + port + "' -UseSsl -Credential $Cred -DeliveryNotificationOption OnSuccess";
    string psScript = "$password = " + pw + System.Environment.NewLine +
                      "$Cred = " + cred + System.Environment.NewLine +
                      send;

    using (PowerShell ps = PowerShell.Create())
    {
        ps.AddScript(psScript);

        // invoke execution on the pipeline (collecting output)
        Collection<PSObject> PSOutput = ps.Invoke();

        // loop through each output object item
        foreach (PSObject outputItem in PSOutput)
        {
            // if null object was dumped to the pipeline during the script then a null
            // object may be present here. check for null to prevent potential NRE.
            if (outputItem != null)
            {
                //TODO: do something with the output item 
                Console.WriteLine(outputItem.BaseObject.GetType().FullName);
                Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
            }
        }
    }
}

Upvotes: 1

Views: 1263

Answers (1)

Patrick O Reilly
Patrick O Reilly

Reputation: 63

I found a way to check for errors using ps.HadErrors

using (PowerShell ps = PowerShell.Create())
{
     //Add the powershell script to the pipeline
     ps.AddScript(psScript);

     // invoke execution on the pipeline (collecting output)
     Collection<PSObject> PSOutput = ps.Invoke();

     //check for any errors
     if (ps.HadErrors)
     {
         foreach (var errorRecord in ps.Streams.Error)
         {
              Console.WriteLine(errorRecord);
         }
     }

 }

Upvotes: 1

Related Questions