使用 PowerShell 发送电子邮件 [英] Send email with PowerShell

查看:88
本文介绍了使用 PowerShell 发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以通过 PowerShell 发送电子邮件.

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

使用 System.Management.Automation 参考,我可以使用 PowerShell 类,该类允许我添加将发送电子邮件的 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.

如果我直接在 PowerShell 窗口中输入它,它看起来像这样:

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('sender@email.com', $password)
Send-MailMessage -From 'sender@email.com' -To 'receiver@email.com' -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?

功能如下.

private void SendEmail()
{
    string from = "sender@email.com";
    string to = "receiver@email.com";
    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");
            }
        }
    }
}

推荐答案

我找到了一种使用 ps.HadErrors 来检查错误的方法

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);
         }
     }

 }

这篇关于使用 PowerShell 发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆