Using PowerShell to create 15k accounts and email account data




We have a project where we will have to create 15000 new user accounts. I will get the information for the users (such as first name, last name, and email address where to send account info) in a CSV file.

I figure out the account creation part (and licensing), but I'm a bit stuck when it comes to emailing them.


First - I'm assuming sending 15k emails will hit some limits, and I don't want to get our domain blacklisted. Is there a way to queue them, and have the sever send them in chunks?

Second - I can't seem to be able to send emails at all. This is the code I have currently:


# Get the credential

$UserName = ""
$PWord = ConvertTo-SecureString -String "myPassword" -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $PWord

## Define the Send-MailMessage parameters
$mailParams = @{
    SmtpServer                 = ''
    Port                       = '587' 
    UseSSL                     = $true 
    Credential                 = $credentials
    From                       = ''
    To                         = ''
    Subject                    = "Sending: $(Get-Date -Format g)"
    Body                       = 'This is a test email'
    DeliveryNotificationOption = 'OnFailure', 'OnSuccess'

## Send the message
Send-MailMessage @mailParams


But I keep getting the following error:


Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.
At C:\SMail.ps1:21 char:1
+ Send-MailMessage @mailParams
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage



Any ideas what's wrong? The username and password are correct.



7 Replies



Not familiar with the error message. Could be al sort of things. 

What you could check: 

1. Is SMTP not blocked? (Firewall/policy)

2. No policy that blocks basic auth?


Have you tried the following code: 


# Sender and Recipient Info
$MailFrom = ""
$MailTo = ""

# Sender Credentials
$Username = ""
$Password = "SomePassword"

# Server Info
$SmtpServer = ""
$SmtpPort = "2525"

# Message stuff
$MessageSubject = "Live your best life now" 
$Message = New-Object System.Net.Mail.MailMessage $MailFrom,$MailTo
$Message.IsBodyHTML = $true
$Message.Subject = $MessageSubject
$Message.Body = @'
<!DOCTYPE html>
This is a test message to trigger an ETR.

# Construct the SMTP client object, credentials, and send
$Smtp = New-Object Net.Mail.SmtpClient($SmtpServer,$SmtpPort)
$Smtp.EnableSsl = $true
$Smtp.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)



As for the exchange online sending limits: 


You can read up in more details in the docs pages:



You sure '' is the correct SmtpServer?

I got the exact same error message using that SmtpServer with an on-prem Exchange mailbox for the Credential and From parameters. 

Hi @ShellBlazer 

This gives me the following error:

Exception calling "Send" with "1" argument(s): "Failure sending mail."
At C:\Smail2.ps1:33 char:1
+ $Smtp.Send($Message)
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SmtpException



You can verify if the port is open with telnet. 

In windows 10 telnet is not by default available. 


1. You can do this with Putty: 



220 Microsoft ESMTP MAIL Service ready at Thu, 16 Jul 2020 06:47:41 +0000

2. Use the telnet command in WSL (subsystem for linux). 

telnet 587
Connected to
Escape character is '^]'.
220 Microsoft ESMTP MAIL Service ready at Thu, 16 Jul 2020 06:44:07 +0000


If connection is not working, you probably have to look at the outbound connection first. And than my first guess is Firewall, second Policy. And maybe this connection attempt will get you a more descriptive error. 

@Ja4ed  - You're right, I checked the domain's MX records and saw that it's an on-prem server (, and when I tried telnet, I get an error "Could not open connection to the host, on port 25: Connect failed". The same happens for port 587.

How would I go about debugging this? To see why it doesn't connect? Whereas telnet to works fine.



I don't think you can use as outgoing server unless the user has has a mailbox and/or license in exchange online. If the mailbox is on-premises, than use the on-prem exchange to send. But chances are security has made this impossible by blocking this kind of traffic in the firewall. It is a best practice to protect your exchange infrastructure from being used as a relay for spamming ppl.