Hello All,
I am running into trouble trying to automate emails that send with multiple attachments AND authenticates on port 587.
This is trying to take all files within the $CopyDir and attach them via email. However, it will only attach 1 file and send. It properly authenticates on port 587, which is what we use. This cannot be changed.
$CopyDir = "somelocationonserver\files\"
------
$EmailFrom = "sendmailfrom@nomail.com"
$EmailTo = "sendmailto@nomail.com"
$EmailBody = "Body of email"
$EmailSubject = "My Subject"
$Username = "myemailcredentials"
$Password = "mypassword"
$CopyDir = "C:\Users\Me\Desktop\Reports\"
$Message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $EmailBody)
$Logfiles = Get-Childitem $CopyDir | WHERE {-NOT $_.PSIsContainer} | foreach{$_.fullname}
$Attachment = New-Object Net.Mail.Attachment($Logfiles)
$Message.Attachments.Add($Attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient("mysmtpserver.smtp.com", 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$SMTPClient.Send($Message)
-------
I try to re-write this using the send-mailmessage cmdlet. Here I assume I can attach multiple files, but it won't authenticate because I can't figure out how to pass the port with this cmdlet. The attachments are piped into the send-mailmessage.
-----
Get-ChildItem $CopyDir | Where {-NOT $_.PSIsContainer} | foreach {$_.fullname} |
send-mailmessage -from $EmailFrom -to $EmailTo -subject $EmailSubject -priority High -dno onSuccess, onFailure UseSSL=true -smtpServer mysmtpserver.smtp.com
-----
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
----
I can't seem to meet in the middle between these two. Any advice would help great. Again, I am looking to send multiple attachments (from $CopyDir), through an email service that requires authentication via Port 587.