Using PowerShell to create 15k accounts and email account data

Microsoft

Hi,

 

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 = "account@domain.com"
$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                 = 'smtp.office365.com'
    Port                       = '587' 
    UseSSL                     = $true 
    Credential                 = $credentials
    From                       = 'account@domain.com'
    To                         = 'myEmail@email.com'
    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.

 

Thanks

7 Replies

@JernejP 

 

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 = "sender@senderdomain.com"
$MailTo = "recipient@recipientdomain.com"

# Sender Credentials
$Username = "SomeUsername@SomeDomain.com"
$Password = "SomePassword"

# Server Info
$SmtpServer = "smtp.domain.com"
$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>
<html>
<head>
</head>
<body>
This is a test message to trigger an ETR.
</body>
</html>
'@

# 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)
$Smtp.Send($Message)

@JernejP 

 

As for the exchange online sending limits: 

ShellBlazer_0-1594847401082.png

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

https://docs.microsoft.com/en-us/office365/servicedescriptions/exchange-online-service-description/e...

 

@JernejP 

You sure 'smtp.office365.com' 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

@JernejP 

 

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: 

ShellBlazer_0-1594881999349.png

=> 

220 AM0PR06CA0073.outlook.office365.com 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 smtp.office365.com 587
Trying 40.101.81.162...
Connected to ams-efz.ms-acdc.office.com.
Escape character is '^]'.
220 AM0PR06CA0118.outlook.office365.com 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 (mail.thedomain.com), 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 smtp.office365.com works fine.

@JernejP 

 

I don't think you can use smtp.office365.com 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.