Need to send email via private smtp server with credential and starttls. I read many tutorials and still cannot establish starttls communication.
My code:
$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"
}
Need to use .Net objects and class, not third party library, or Send-MailMessage in Powershell, because Send-MailMessage doesn't have atachment sending options.