使用PowerShell 4.0发送邮件遇认证错误,现有方案无效求助
First, let's break down what that error is telling you:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
This means your script isn't providing valid login details to the SMTP server, and it's refusing to let you send mail without proper authentication. Since existing solutions haven't worked for PowerShell 4.0, let's focus on methods that play nice with this older version.
Solution 1: Use Send-MailMessage with Proper Credentials & SSL
PowerShell 4.0 includes the native Send-MailMessage cmdlet, but you need to explicitly pass credentials and configure secure settings. Here's a corrected version of your code:
$EmailTo = "abc@email.com" $EmailFrom = "ced@email.com" $Subject = "Test" $Body = "Test Body" $SMTPServer = "smtp.yourserver.com" # Example: smtp.office365.com for Office 365 $SMTPPort = 587 # Standard TLS port for most SMTP providers $Credentials = Get-Credential -Message "Enter your SMTP server login details" # Send the email with authentication and SSL enabled Send-MailMessage -To $EmailTo ` -From $EmailFrom ` -Subject $Subject ` -Body $Body ` -SmtpServer $SMTPServer ` -Port $SMTPPort ` -UseSsl ` -Credential $Credentials
PS 4.0-Specific Tips:
- Skip plaintext passwords:
Get-Credentialis the safest way to input credentials manually. For automation, store credentials securely usingConvertTo-SecureString(encrypt the string with a machine/user-specific key to avoid exposure). - Office 365 Users: Ensure Basic Authentication is enabled for your mailbox (PowerShell 4.0 doesn't support modern auth natively). Use
smtp.office365.comas the server and port 587.
Solution 2: Fallback to .NET SmtpClient Class
If Send-MailMessage still fails (some environments block cmdlet-based SMTP calls), use the .NET SmtpClient class directly—it’s reliable in PS 4.0:
$EmailTo = "abc@email.com" $EmailFrom = "ced@email.com" $Subject = "Test" $Body = "Test Body" $SMTPServer = "smtp.yourserver.com" $SMTPPort = 587 $Username = "ced@email.com" # Match your SMTP login username $SecurePassword = ConvertTo-SecureString "your-smtp-password" -AsPlainText -Force # Use secure storage in production! $Credentials = New-Object System.Management.Automation.PSCredential ($Username, $SecurePassword) # Build the email message $Message = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body) # Configure SMTP client $SmtpClient = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort) $SmtpClient.EnableSsl = $true $SmtpClient.Credentials = $Credentials # Send the email with error handling try { $SmtpClient.Send($Message) Write-Host "Email sent successfully!" } catch { Write-Error "Failed to send email: $_" } finally { # Clean up resources to avoid leaks $Message.Dispose() $SmtpClient.Dispose() }
Critical Reminders:
- Never hardcode passwords: In production, use encrypted credential files or a secrets manager to retrieve login details safely.
- Validate Server Settings: Double-check that your SMTP server allows connections from your machine, and that the
$EmailFromaddress matches the username in your credentials (many servers enforce this). - Check Network Access: Ensure your firewall/network isn’t blocking outbound traffic on port 587 (TLS) or 465 (SSL).
内容的提问来源于stack exchange,提问作者Anuj

