2015-09-26 89 views
-1

See this question's first two answers(曾经尝试都,都产生下面的错误):PowerShell中发送电子邮件

代码(根据需要更改件):

$EmailFrom = "[email protected]" 
$EmailTo = "[email protected]" 
$Subject = "Notification from XYZ" 
$Body = "this is a notification from XYZ Notifications.." 
$SMTPServer = "smtp.gmail.com" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password"); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) 

其他方式(根据需要更改件):

$credentials = new-object Management.Automation.PSCredential “[email protected]”, (“password” | ConvertTo-SecureString -AsPlainText -Force) 
Send-MailMessage -From $From -To $To -Body $Body $Body -SmtpServer {$smtpServer URI} -Credential $credentials -Verbose -UseSsl 

我得到这个错误:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

在第一个脚本中,端口是明确指定的,而不是使用PS的内置函数(尽管过去我没有遇到这个函数的问题)。

谢谢!

+0

我认为[这](http://stackoverflow.com/questions/20906077/gmail-error-the -smtp-server-requires-a-secure-connection-or-the-client-was-not)可能会有帮助。 –

回答

0

试试这个功能通过Gmail发送电子邮件:

Function batchsend-mailmessage ($to, $subject, $body, $attachments) 
{ 
    $smtpServer = "smtp.gmail.com" 
    $smtpServerport = "587" 
    $emailSmtpUser = "[email protected]" 
    $emailSmtpPass = "PasswordOfTheBatch" 

    $emailMessage = New-Object System.Net.Mail.MailMessage 
    $emailMessage.From = $emailSmtpUser 
    foreach ($addr in $to) 
    { 
    $emailMessage.To.Add($addr) 
    } 
    foreach ($attachment in $attachments) 
    { 
    $emailMessage.Attachments.Add($attachment) 
    } 
    $emailMessage.Subject = $subject 
    $emailMessage.IsBodyHtml = $true 
    $emailMessage.Body = $body 

    $smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer , $smtpServerport) 
    $smtpClient.EnableSsl = $true 
    $smtpClient.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser , $emailSmtpPass); 

    $SMTPClient.Send($emailMessage) 
} 

像这样:

batchsend-mailmessage -to $DesAdressesDest -subject $Subject -body $body -attachments $xlsFile 
0

我们使用PowerShell通过Office365发送邮件。这是我们使用的(完美的作品)。请记住,你与验证,用户必须具有“发送为”从地址的-permissions:

$PSEmailServer = "smtp.office365.com" 
$credentials = new-object Management.Automation.PSCredential “[email protected]”, (“password” | ConvertTo-SecureString -AsPlainText -Force) 
$enc = New-Object System.Text.utf8encoding 
$from = "FromAddress" 
$to = "ToAddress","ToAdress2" 
$body = "Test" 
$subject = "Test" 

Send-MailMessage -port 587 -From $from -BodyAsHtml -Encoding $enc -To $to -Subject $subject -Body $body -UseSsl -Credential $credentials