powershell – 如何将凭据传递给Send-MailMessage命令以发送电子邮件

我的凭据传递给Send-MailMessage命令时遇到困难

这是我正在运行的:

Send-MailMessage -smtpServer smtp.gmail.com -from 'myself@gmail.com' `
    -to 'myself@gmail.com' -subject 'Test' -attachment C:\CDF.pdf

它的错误在下面的消息显然是因为我没有通过我的gmail凭据

Send-MailMessage : The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first.

我googled了一点,并通过Send-MailMessage的手册页,发现“-credential”参数需要传递。

我的问题是:怎么样?

我用Get-Credentials尝试如下:

$mycredentials = Get-Credential

然后在弹出的框中输入我的用户名和密码。

然后我运行下面的命令:

Send-MailMessage -smtpServer smtp.gmail.com -credentail $mycredentials `
  -from 'myself@gmail.com' -to 'myself@gmail.com' -subject 'Test' -attachment C:\CDF.pdf

并且仍然失败,具有完全相同的错误。

所以我需要帮助你们如何将我的凭证传递给Send-MailMessage命令。我学到了PScredentials,但不完全确定它是什么,以及如何在这种情况下使用它。

最佳答案
我发现这个博客网站:Adam Kahtava
我也发现这个问题:send-mail-via-gmail-with-powershell-v2s-send-mailmessage
问题是,他们都不满足您的需求(附有密码),所以我做了两个组合,并提出了这一点:

$EmailTo = "myself@gmail.com"
$EmailFrom = "me@mydomain.com"
$Subject = "Test" 
$Body = "Test Body" 
$SMTPServer = "smtp.gmail.com" 
$filenameAndPath = "C:\CDF.pdf"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password"); 
$SMTPClient.Send($SMTPMessage)

因为我喜欢为事情做功能,而且我需要所有的练习,所以我写了这个:

Function Send-EMail {
    Param (
        [Parameter(`
            Mandatory=$true)]
        [String]$EmailTo,
        [Parameter(`
            Mandatory=$true)]
        [String]$Subject,
        [Parameter(`
            Mandatory=$true)]
        [String]$Body,
        [Parameter(`
            Mandatory=$true)]
        [String]$EmailFrom="myself@gmail.com",  #This gives a default value to the $EmailFrom command
        [Parameter(`
            mandatory=$false)]
        [String]$attachment,
        [Parameter(`
            mandatory=$true)]
        [String]$Password
    )

        $SMTPServer = "smtp.gmail.com" 
        $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
        if ($attachment -ne $null) {
            $SMTPattachment = New-Object System.Net.Mail.Attachment($attachment)
            $SMTPMessage.Attachments.Add($STMPattachment)
        }
        $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
        $SMTPClient.EnableSsl = $true 
        $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom.Split("@")[0], $Password); 
        $SMTPClient.Send($SMTPMessage)
        Remove-Variable -Name SMTPClient
        Remove-Variable -Name Password

} #End Function Send-EMail

要调用它,只需使用以下命令:

Send-EMail -EmailTo "Myself@gmail.com" -Body "Test Body" -Subject "Test Subject" -attachment "C:\cdf.pdf" -password "Passowrd"

我知道这是不安全的密码是这样明白的。我会看看我能否提出一些更安全和更新的东西,但至少这应该让你开始需要什么。有一个伟大的一周!

编辑:添加$ EmailFrom基于JuanPablo的评论

转载注明原文:powershell – 如何将凭据传递给Send-MailMessage命令以发送电子邮件 - 代码日志