Forum Discussion
JernejP
Jul 15, 2020Microsoft
Using PowerShell to create 15k accounts and email account data
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...
ShellBlazer
Brass Contributor
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
Jul 16, 2020Microsoft
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
- ShellBlazerJul 16, 2020Brass Contributor
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 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.