1

new to the forum, so apologises if this in the wrong area.

I am looking to create a powershell script which emails a list (.txt) file consisting of 200 emails, the text file will be updated aswell with new email addresses. The email body which will be send is a standard (generic) email, nothing specific to each email address.

Is this possible?

This is the script I have so far, but its not sending the email..

$emails = @() 
      ForEach ($recipient in Get-Content "\\FILE Location")

      {
        $emails += "$recipient"
      }

$to = "TO EMAIL"
$smtp = "SMTP Server"
$from = "FROM EMAIL"
$subject = "Subject" 
$body = "EMAIL BODY"
send-MailMessage -SmtpServer $smtp -To $to -Bcc $emails -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high

Thanks in advance Matt.

MattG
  • 19
  • 6
  • 1
    Welcome to StackOverflow. You will generally get better help if you have a specific code problem that you need to solve and have some code you've written that you are struggling to get work. Have a look at the help/getting started pages and "How to ask a good question" guide. Regarding your question, you want to look at combining `Get-Content` with `Send-MailMessage`. – Mark Wragg Sep 03 '17 at 14:50
  • 1
    Possible duplicate of [Powershell send-mailmessage - email to multiple recipients](https://stackoverflow.com/questions/10241816/powershell-send-mailmessage-email-to-multiple-recipients) –  Sep 03 '17 at 14:54
  • This is what I have so far...$emails = @() ForEach ($recipient in Get-Content "\\FILE Location") { $emails += "$recipient" } $to = "TO EMAIL" $smtp = "SMTP Server" $from = "FROM EMAIL" $subject = "Subject" $body = "EMAIL BODY" send-MailMessage -SmtpServer $smtp -To $to -Bcc $emails -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high – MattG Sep 03 '17 at 14:57
  • You should edit your question to include that (you should see an "edit" link above) rather than put it in a comment where it's hard to read. – Mark Wragg Sep 03 '17 at 15:02
  • Apologies I have now added this in to the original question. Thanks. – MattG Sep 03 '17 at 15:09
  • @MattGeva are you getting an error when you run the command or is it just not arriving in the mailboxes? – colsw Sep 03 '17 at 15:22
  • @ConnorLSW No errors, its just not arriving in the mailboxes. – MattG Sep 03 '17 at 15:24
  • @MattGeva do you see it in your Exchange tracking logs? the cmdlet is usually pretty good about throwing errors on things it can't hand off to the SMTP server. – colsw Sep 03 '17 at 15:26
  • @ConnorLSW Nothing showing in the exchange tracking log.. the emails with the .txt would they need to be entered in a way? (I have each email on a separate line). – MattG Sep 03 '17 at 15:29
  • Try this: `send-MailMessage -SmtpServer $smtp -To $to -Bcc (Get-Content "\\FILE Location") -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high`. I'm just wondering if the ForEach is causing the problem, not that it should, but you can do without it. Have you ensured you can send a single email to your own email address with the other settings you've specified? Are you sure where you're running this can access the SMTP server and is permitted to relay through it with the From address specified? – Mark Wragg Sep 03 '17 at 15:38
  • @ConnorLSW We have plenty of other smtp mails which are sent, so I don't think the issue would be the SMTP server.. Why would I specific -attachments? as I don't want anything attached to the email which goes out. – MattG Sep 03 '17 at 15:41
  • @MarkWragg I can send the email to internal and external email addresses with the same setting specified.. Its only when I add in the section which looks for the txt file that it doesn't arrive in the mailbox. – MattG Sep 03 '17 at 15:47
  • Did you try it with the get-content inline as I showed? – Mark Wragg Sep 03 '17 at 15:49
  • @MarkWragg when trying inline I get the following error: send-mailmessage: unable to read data from the transport connection – MattG Sep 03 '17 at 16:12
  • @MarkWragg I only receive the above error when sending to the list of over 200 email addresses - I have tested using another .txt file with some test emails and the mail send fine. – MattG Sep 03 '17 at 17:01
  • Sounds like that's your answer then. Perhaps it opens a new socket for each recipient and you're suffering with port exhaustion. You could write a script that reads your text file in a number of small batches. – Mark Wragg Sep 03 '17 at 17:02
  • Thanks @MarkWragg - any advice on the script to read the text file in smaller batches? – MattG Sep 03 '17 at 17:04
  • Open a new question with that as the topic and i'll submit it as an answer (this one is now locked because it got put on hold earlier when it was too broad). – Mark Wragg Sep 03 '17 at 17:10

1 Answers1

0

In these situations it may be best to create the objects yourself. I would set it up like so:

$smtpServer = "SMTP Server"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtpCredentialsUsername = "blah@blah.com"
$smtpCredentialsPassword = ConvertTo-SecureString -String $smtpPwd -AsPlainText -Force

$Credentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList 
$smtpCredentialsUsername, $smtpCredentialsPassword
$smtp.Credentials = $Credentials

$msg = new-object Net.Mail.MailMessage
$msg.Body = $body
$msg.Subject = $subject
$msg.From = $from
$msg.To.ADD($to)
$msg.IsBodyHtml = $true


ForEach ($recipient in Get-Content "\\FILE Location")
  {
    $msg.Bcc.ADD($recipient)
  }

$smtp.Send($msg)

$msg.Dispose();
John Donnelly
  • 875
  • 1
  • 10
  • 29