已发送邮件/ PowerShell凭据

时间:2018-06-07 16:46:48

标签: powershell email credentials

我尝试使用基于此示例的代码发送电子邮件:https://blog.victorsilva.com.uy/credenciales-con-powershell/,但电子邮件未发送:

$emailSmtpServer = "smtp.mail.yahoo.com"
$emailSmtpServerPort = "587"
$password = Get-Content -Path "C:\Users\USUARIOPC\password2.txt" | 
ConvertTo-SecureString -String $password
$credential = New-Object 
System.Management.Automation.PsCredential("jrosh.01@yahoo.com", $password)

$emailMessage = New-Object System.Net.Mail.MailMessage

$emailMessage.From = "jrosh.01@yahoo.com"
$emailMessage.To.Add( "jrosh.01@yahoo.com" )
$emailMessage.subject = "Notificacion de: $($env:computername)"
$emailMessage.IsBodyHtml = $true

$evento = Get-WinEvent –FilterHashTable @{logname="System"} -MaxEvents 1 | 
Where {$_.ID -Match "1020"}

$emailMessage.Body = @"
    <H1>Alerta Scope DHCP</H1>
    <p>Evento a revisar en: <strong>$($evento.MachineName)</strong>.</p>
    <p>Identificador: <strong>$($evento.Id)</strong>.</p>
    <p>Fecha / Hora: <strong>$($evento.TimeCreated)</strong>.</p>
    <p>Texto: <strong>$($evento.Message)</strong>.</p>
    <br>
    <H5><i>$(get-date)</i></H5>
"@

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , 
$emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( 
$emailSmtpUser , $credential );

$SMTPClient.Send( $emailMessage )
Write-Host "Correo Enviado..."

1 个答案:

答案 0 :(得分:0)

根据您提交的错误消息,看起来密码文件存在问题。请确保您输出密码:

"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File C:\Users\USUARIOPC\password2.txt

P@ssword1是您的密码。