Using PowerShell to create 15k accounts and email account data

%3CLINGO-SUB%20id%3D%22lingo-sub-1524112%22%20slang%3D%22en-US%22%3EUsing%20PowerShell%20to%20create%2015k%20accounts%20and%20email%20account%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1524112%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20have%20a%20project%20where%20we%20will%20have%20to%20create%2015000%20new%20user%20accounts.%20I%20will%20get%20the%20information%20for%20the%20users%20(such%20as%20first%20name%2C%20last%20name%2C%20and%20email%20address%20where%20to%20send%20account%20info)%20in%20a%20CSV%20file.%3C%2FP%3E%0A%3CP%3EI%20figure%20out%20the%20account%20creation%20part%20(and%20licensing)%2C%20but%20I'm%20a%20bit%20stuck%20when%20it%20comes%20to%20emailing%20them.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20-%20I'm%20assuming%20sending%2015k%20emails%20will%20hit%20some%20limits%2C%20and%20I%20don't%20want%20to%20get%20our%20domain%20blacklisted.%20Is%20there%20a%20way%20to%20queue%20them%2C%20and%20have%20the%20sever%20send%20them%20in%20chunks%3F%3C%2FP%3E%0A%3CP%3ESecond%20-%20I%20can't%20seem%20to%20be%20able%20to%20send%20emails%20at%20all.%20This%20is%20the%20code%20I%20have%20currently%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%20Get%20the%20credential%0A%0A%24UserName%20%3D%20%22account%40domain.com%22%0A%24PWord%20%3D%20ConvertTo-SecureString%20-String%20%22myPassword%22%20-AsPlainText%20-Force%0A%24credentials%20%3D%20New-Object%20-TypeName%20System.Management.Automation.PSCredential%20-ArgumentList%20%24UserName%2C%20%24PWord%0A%0A%23%23%20Define%20the%20Send-MailMessage%20parameters%0A%24mailParams%20%3D%20%40%7B%0A%20%20%20%20SmtpServer%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20'smtp.office365.com'%0A%20%20%20%20Port%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20'587'%20%0A%20%20%20%20UseSSL%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20%24true%20%0A%20%20%20%20Credential%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20%24credentials%0A%20%20%20%20From%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20'account%40domain.com'%0A%20%20%20%20To%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20'myEmail%40email.com'%0A%20%20%20%20Subject%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20%22Sending%3A%20%24(Get-Date%20-Format%20g)%22%0A%20%20%20%20Body%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20'This%20is%20a%20test%20email'%0A%20%20%20%20DeliveryNotificationOption%20%3D%20'OnFailure'%2C%20'OnSuccess'%0A%7D%0A%0A%23%23%20Send%20the%20message%0ASend-MailMessage%20%40mailParams%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20I%20keep%20getting%20the%20following%20error%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESend-MailMessage%20%3A%20Unable%20to%20read%20data%20from%20the%20transport%20connection%3A%20net_io_connectionclosed.%0AAt%20C%3A%5CSMail.ps1%3A21%20char%3A1%0A%2B%20Send-MailMessage%20%40mailParams%0A%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20InvalidOperation%3A%20(System.Net.Mail.SmtpClient%3ASmtpClient)%20%5BSend-MailMessage%5D%2C%20SmtpException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20SmtpException%2CMicrosoft.PowerShell.Commands.SendMailMessage%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAny%20ideas%20what's%20wrong%3F%20The%20username%20and%20password%20are%20correct.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1524112%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20AD%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1524851%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PowerShell%20to%20create%2015k%20accounts%20and%20email%20account%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1524851%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F717878%22%20target%3D%22_blank%22%3E%40JernejP%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20familiar%20with%20the%20error%20message.%20Could%20be%20al%20sort%20of%20things.%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20you%20could%20check%3A%26nbsp%3B%3C%2FP%3E%3CP%3E1.%20Is%20SMTP%20not%20blocked%3F%20(Firewall%2Fpolicy)%3C%2FP%3E%3CP%3E2.%20No%20policy%20that%20blocks%20basic%20auth%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHave%20you%20tried%20the%20following%20code%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%23%20Sender%20and%20Recipient%20Info%0A%24MailFrom%20%3D%20%22sender%40senderdomain.com%22%0A%24MailTo%20%3D%20%22recipient%40recipientdomain.com%22%0A%0A%23%20Sender%20Credentials%0A%24Username%20%3D%20%22SomeUsername%40SomeDomain.com%22%0A%24Password%20%3D%20%22SomePassword%22%0A%0A%23%20Server%20Info%0A%24SmtpServer%20%3D%20%22smtp.domain.com%22%0A%24SmtpPort%20%3D%20%222525%22%0A%0A%23%20Message%20stuff%0A%24MessageSubject%20%3D%20%22Live%20your%20best%20life%20now%22%20%0A%24Message%20%3D%20New-Object%20System.Net.Mail.MailMessage%20%24MailFrom%2C%24MailTo%0A%24Message.IsBodyHTML%20%3D%20%24true%0A%24Message.Subject%20%3D%20%24MessageSubject%0A%24Message.Body%20%3D%20%40'%0A%26lt%3B!DOCTYPE%20html%26gt%3B%0A%26lt%3Bhtml%26gt%3B%0A%26lt%3Bhead%26gt%3B%0A%26lt%3B%2Fhead%26gt%3B%0A%26lt%3Bbody%26gt%3B%0AThis%20is%20a%20test%20message%20to%20trigger%20an%20ETR.%0A%26lt%3B%2Fbody%26gt%3B%0A%26lt%3B%2Fhtml%26gt%3B%0A'%40%0A%0A%23%20Construct%20the%20SMTP%20client%20object%2C%20credentials%2C%20and%20send%0A%24Smtp%20%3D%20New-Object%20Net.Mail.SmtpClient(%24SmtpServer%2C%24SmtpPort)%0A%24Smtp.EnableSsl%20%3D%20%24true%0A%24Smtp.Credentials%20%3D%20New-Object%20System.Net.NetworkCredential(%24Username%2C%24Password)%0A%24Smtp.Send(%24Message)%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1524891%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PowerShell%20to%20create%2015k%20accounts%20and%20email%20account%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1524891%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F717878%22%20target%3D%22_blank%22%3E%40JernejP%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20for%20the%20exchange%20online%20sending%20limits%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22ShellBlazer_0-1594847401082.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F205590iE218A9590A3B4D2B%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22ShellBlazer_0-1594847401082.png%22%20alt%3D%22ShellBlazer_0-1594847401082.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EYou%20can%20read%20up%20in%20more%20details%20in%20the%20docs%20pages%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Foffice365%2Fservicedescriptions%2Fexchange-online-service-description%2Fexchange-online-limits%23receiving-and-sending-limits%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Foffice365%2Fservicedescriptions%2Fexchange-online-service-description%2Fexchange-online-limits%23receiving-and-sending-limits%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1525418%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PowerShell%20to%20create%2015k%20accounts%20and%20email%20account%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1525418%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F717878%22%20target%3D%22_blank%22%3E%40JernejP%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20sure%20'smtp.office365.com'%20is%20the%20correct%20SmtpServer%3F%3C%2FP%3E%3CP%3EI%20got%20the%20exact%20same%20error%20message%20using%20that%20SmtpServer%20with%20an%20on-prem%20Exchange%20mailbox%20for%20the%20Credential%20and%20From%20parameters.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1525502%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PowerShell%20to%20create%2015k%20accounts%20and%20email%20account%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1525502%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F463705%22%20target%3D%22_blank%22%3E%40ShellBlazer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20gives%20me%20the%20following%20error%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EException%20calling%20%22Send%22%20with%20%221%22%20argument(s)%3A%20%22Failure%20sending%20mail.%22%0AAt%20C%3A%5CSmail2.ps1%3A33%20char%3A1%0A%2B%20%24Smtp.Send(%24Message)%0A%2B%20~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20MethodInvocationException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20SmtpException%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1525654%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PowerShell%20to%20create%2015k%20accounts%20and%20email%20account%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1525654%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F717878%22%20target%3D%22_blank%22%3E%40JernejP%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20verify%20if%20the%20port%20is%20open%20with%20telnet.%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20windows%2010%20telnet%20is%20not%20by%20default%20available.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E1.%20You%20can%20do%20this%20with%20Putty%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22ShellBlazer_0-1594881999349.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F205743i27298ECDF58B0CE8%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22ShellBlazer_0-1594881999349.png%22%20alt%3D%22ShellBlazer_0-1594881999349.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3D%26gt%3B%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E220%20AM0PR06CA0073.outlook.office365.com%20Microsoft%20ESMTP%20MAIL%20Service%20ready%20at%20Thu%2C%2016%20Jul%202020%2006%3A47%3A41%20%2B0000%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E2.%20Use%20the%20telnet%20command%20in%20WSL%20(subsystem%20for%20linux).%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Etelnet%20smtp.office365.com%20587%0ATrying%2040.101.81.162...%0AConnected%20to%20ams-efz.ms-acdc.office.com.%0AEscape%20character%20is%20'%5E%5D'.%0A220%20AM0PR06CA0118.outlook.office365.com%20Microsoft%20ESMTP%20MAIL%20Service%20ready%20at%20Thu%2C%2016%20Jul%202020%2006%3A44%3A07%20%2B0000%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20connection%20is%20not%20working%2C%20you%20probably%20have%20to%20look%20at%20the%20outbound%20connection%20first.%20And%20than%20my%20first%20guess%20is%20Firewall%2C%20second%20Policy.%20And%20maybe%20this%20connection%20attempt%20will%20get%20you%20a%20more%20descriptive%20error.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1525771%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PowerShell%20to%20create%2015k%20accounts%20and%20email%20account%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1525771%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F466927%22%20target%3D%22_blank%22%3E%40Ja4ed%3C%2FA%3E%26nbsp%3B%20-%20You're%20right%2C%20I%20checked%20the%20domain's%20MX%20records%20and%20saw%20that%20it's%20an%20on-prem%20server%20(mail.thedomain.com)%2C%20and%20when%20I%20tried%20telnet%2C%20I%20get%20an%20error%20%22Could%20not%20open%20connection%20to%20the%20host%2C%20on%20port%2025%3A%20Connect%20failed%22.%20The%20same%20happens%20for%20port%20587.%3C%2FP%3E%0A%3CP%3EHow%20would%20I%20go%20about%20debugging%20this%3F%20To%20see%20why%20it%20doesn't%20connect%3F%20Whereas%20telnet%20to%20smtp.office365.com%20works%20fine.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1525830%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PowerShell%20to%20create%2015k%20accounts%20and%20email%20account%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1525830%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F717878%22%20target%3D%22_blank%22%3E%40JernejP%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20don't%20think%20you%20can%20use%20smtp.office365.com%20as%20outgoing%20server%20unless%20the%20user%20has%20has%20a%20mailbox%20and%2For%20license%20in%20exchange%20online.%20If%20the%20mailbox%20is%20on-premises%2C%20than%20use%20the%20on-prem%20exchange%20to%20send.%20But%20chances%20are%20security%20has%20made%20this%20impossible%20by%20blocking%20this%20kind%20of%20traffic%20in%20the%20firewall.%20It%20is%20a%20best%20practice%20to%20protect%20your%20exchange%20infrastructure%20from%20being%20used%20as%20a%20relay%20for%20spamming%20ppl.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
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.