austineric / Send-MailKitMessage

A replacement for PowerShell's obsolete Send-MailMessage implementing the Microsoft-recommended MailKit library.
https://www.powershellgallery.com/packages/Send-MailKitMessage
MIT License
44 stars 5 forks source link

Not working with gmail and pwsh version 7.4 #40

Closed johncblacker closed 9 months ago

johncblacker commented 9 months ago

I had tried using windows powershell 5.1 to no avail - kept getting errors from google regarding credentials. So I installed powershell 7.4 and tried again and am still getting the errors - google complaining about my user/password credentials; but those credentials are the same I use with my outlook 2013 email program and same as the one's I use to sign into gmail.com. I'm not sure where to go from this point nor how to troubleshoot/debug the problem. I'm not more than a neophyte with powershell in general. I'm using the example provided with "all parameters" but I comment out the optional parameters I don't need, like cc, bcc and attachment.

johncblacker commented 9 months ago

I switched from using smtp.google.com to using mail.twc.com (Spectrum/Charter communications), used port 587, -UseSsl and the example in the mailkit repo; However, I have to now find a way to pick up the user/pwd without having to get user creds without having to request from user as that won't work when the script is spawned from the task scheduler. Ideas would be helpful.

austineric commented 9 months ago

Hi @johncblacker,

However, I have to now find a way to pick up the user/pwd without having to get user creds without having to request from user as that won't work when the script is spawned from the task scheduler. Ideas would be helpful.

Do you mean how to have a scheduled job get the credentials without including them in the script? If so then have you looked at environment variables? https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.4#saving-changes-to-environment-variables Then use like "$Env:SMTPUsername" and "$Env:SMTPPassword" or whatever you choose to name them.

johncblacker commented 9 months ago

Thanks for the link...I can't use the env approach because that still exposes the password. I've discovered another approach using a couple powershell functions which get the password from the user (admin user on the server) in an install script, creates a file with the encrypted password and then uses that file data to create a System.Security.SecureString object that I use to create a Credential object which is used in the Send. The file remains on the server disk and persists across server reboots (if necessary). Should the file become corrupted or deleted for some reason, it is easily re-created which is performed by anyone who can log into the server.