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("[email protected]","mypassword"); #yes, correct login, tried via Thunderbird #Email structure $msg.From = "[email protected]" $msg.To.Add("[email protected]") $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.