需要通过带有凭据和starttls的私有smtp服务器发送电子邮件。我阅读了很多教程,仍然无法建立starttls通信。
我的代码:
$smtpServer = "smtp.xserver.cz"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer, 25)#587 works
$smtp.EnableSsl = $false
$smtp.Credentials = New-Object System.Net.NetworkCredential("myuser@xserver.cz","mypassword"); #yes, correct login, tried via Thunderbird
#Email structure
$msg.From = "info@xserver.cz"
$msg.To.Add("testtest@gmail.com")
$msg.subject = "subject"
$msg.IsBodyHTML = $true
$msg.body = "AAA"+"<br /><br />"
$ok=$true
try{
$smtp.Send($msg)
Write-Host "SENT"
}
catch {
Write-Host "`tNOT WORK !!!!!"
$error[0]
$_.Exception.Response
$ok=$false
}
finally{
$msg.Dispose()
}
if($ok){
Write-Host "`tEVERYTHING OK"
}
需要在Powershell中使用.Net对象和类,而不是第三方库或Send-MailMessage,因为Send-MailMessage没有atachment发送选项。
答案 0 :(得分:0)
您未获得TLS连接,因为您已将EnableSsl设置为$ false。
不确定您认为Send-MailMessage没有附件发送选项的地方。使用Send-MailMailMessage发送附件非常容易。它接受一个或多个文件名以用作管道中的附件,或通过-Attachments参数。