How to create Powershell script to send an email to all email addresses listed in a file and send an attachment called via a DOS batch file.

Tom Townsend 1 Reputation point
2021-03-23T13:45:48.693+00:00

Is there a way to do this?
Prefer that the Powershell script uses the logged on credentials rather than storing the credentials in the script.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,326 Reputation points
    2021-03-23T14:36:38.343+00:00

    Have you looked at Send-MailMessage? It sends emails with optional attachments.

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2021-03-23T18:35:55.53+00:00

    The Send-MailMessage may be able to use the credentials of the account under which it's been run, but I've never tried it, and there's no parameter that you can use to tell it to.

    The Send-MailMessage cmdlet is build on the System.Net.Mail.SMTPClien class, and that has a "UseDefaultCredentials" property. If necessary you can always use the SMTPClient class in your script.

    However, the Send-MailMessage cmdlet (and the SMTPClient class) have been deprecated because neither of them use the latest TLS version -- and there are no plans to add that ability -- so using SSL may not be possible, and that will expose your user/password on the network (they're only Base64 encoded so it's extremely easy to "decode").

    You may be better off looking at the MailKit module. Have a look here for a good bit of information: powershell-email

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.