Available Now: Buy the new iPhone 14, iPhone 14 Pro and iPhone 14 Pro Max from AT&T!
Protect yourself online
rweber5's profile

Contributor

 • 

11 Messages

Thu, May 13, 2021 12:53 AM

Sending Email via Powershell Renders a Connection Error 10060

I'm testing a new Powershell script which will enventually be a scheduled task once I get it to work. I am trying to send the message via Yahoo mail which uses port 587 and TSL. The smtp server is smtp.mail.yahoo.com and I can successfully telnet the address. When I try to run the code, it eventually hangs up with the following message:

Exception calling "Send" with "1" argument(s): "Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 206.190.49.106 (206.190.49.106:587), connect error 10060" At C:\users\rob\dropbox\PowershellSendEmail.ps1:23 char:1

  • $smtp.Send($message)
  • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
  • FullyQualifiedErrorId : SmtpException

There is no firewall or anti-virus program in the way. Any ideas on what is causing this? The code is attached.


        $toaddress = "[email scrubbed]"
        $Subject = "Testing 4pm Message"
        $body = "this is a test."
        $attachment = "C:\users\text.txt"
        $smtpserver = "smtp.mail.yahoo.com"
        $smtpuser = "[email scrubbed]"
        $smtpPassword = "test1" | ConvertTo-SecureString -AsPlainText -Force
        $smtpport = 587

        $message = new-object System.Net.Mail.MailMessage
        $message.From = $fromaddress
        $message.To.Add($toaddress)
        $message.IsBodyHtml = $False
        $message.Subject = $Subject
        $attach = new-object Net.Mail.Attachment($attachment)
        $message.Attachments.Add($attach)
        $message.body = $body
        $smtp = new-object Net.Mail.SmtpClient($smtpserver, $smtpport)
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        $smtp.Credentials = new-object System.Management.Automation.PSCredential -argumentlist 
            $smtpuser,$smtpPassword
        $smtp.Timeout = 400000
        $smtp.Send($message)

Accepted Solution

Official Solution

JefferMC

ACE - Expert

 • 

29.9K Messages

1年前

Let me ask this: are you using a Secure Mail Key for your $emailpassword value?

ATTHelp

Community Support

 • 

200.4K Messages

1年前

We're here to help, @rweber5 

We recommend reaching out to our ConnecTech Team at 866.294.3464. 

They will be able to assist with your connection error. 

Let us know if you have additional questions. 

Thank you for contacting AT&T Community Forums. 

Deandra, AT&T Community Specialist 

JefferMC

ACE - Expert

 • 

29.9K Messages

1年前

I would suggest:

1) not reaching out to the PAID ConnecTech Team, because they will be of absolutely no help, bu tthey will charge you, and

2) Switch to the Send-MailMessage command instead of trying to use that old .NET object.

Contributor

 • 

11 Messages

1年前

@JefferMC  Thank you for your response!  I purposely didn't use the Send-MailMessage command as I have seen several posts like the following:

Send-mailmessage warning

Is this incorrect?

JefferMC

ACE - Expert

 • 

29.9K Messages

1年前

Well... that link takes you to a page that basically tells you not to use SmtpClient, which is what you were using for a replacement.  The implication is that send-MailMessage is just a wrapper around SmtpClient.  And for some reason Microsoft has bailed on the idean of providing a secure client and now refers you to an open software project.  Grrrrrrr!

I have had some struggles with SmtpClient in certain situations, but have always gotten it to work... eventually.  I also have some sample code that I've been able to use with Send-MailMessage with Outlook 365, but I'll have to go dig it out.  I'll post it, or a link to it, when I do.

Until then, you could take a look at the second answer on this post:

https://stackoverflow.com/questions/1252335/send-mail-via-gmail-with-powershell-v2s-send-mailmessage

(edited)

JefferMC

ACE - Expert

 • 

29.9K Messages

1年前

SendGrid is actually a third-party service (operated by Twillo) that you're using to send your e-mail for you.  It does have an API you can consume.  And people have written command scripts to make it easier to consume that API.

I have explored it only briefly because in my situation there would be transactional costs for using it.  We're currently using Authenticated SSL connections to an Outlook 365 account because, in my situation, those aren't getting billed back to me.

Contributor

 • 

11 Messages

1年前

@JefferMC   understood . . . free is best for me!

Contributor

 • 

11 Messages

1年前

@JefferMC  I have updated the code to utilize Send-MailMessage.  Here is what I have:

$emailSmtpServer = "smtp.mail.yahoo.com"
$emailFrom = "[email scrubbed]"
$emailTo = "[email scrubbed]"
$emailSubject = "Testing 4pm Email"
$emailBody = "Get-Content D:\Rob_Data\Documents\My Webs\roto\index.html"
$emailusername="[email scrubbed]" 
$emailpassword = "password" | ConvertTo-SecureString -AsPlainText -Force
$emailCredential = New-Object System.Management.Automation.PSCredential($emailusername, $emailpassword)
Send-MailMessage -From $emailFrom -To $emailTo -Subject $emailSubject -Body $emailBody -BodyAsHTML -SmtpServer  $emailSmtpServer -Credential $emailCredential -Port 587 -UseSSL -Verbose

The 10060 error message is gone and has been replaced with the following:

Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.
At C:\Users\Rob\dropbox\SendMailMessage.ps1:12 char:1
+ Send-MailMessage -From $emailFrom -To $emailTo -Subject $emailSubject ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
   ion
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

(edited)

Contributor

 • 

11 Messages

1年前

@JefferMC . . . some further info.  The above works fine with Gmail.  I suspect that Yahoo or AT&T need to allow for less secured apps.  That option is not available to me.  See this thread:

https://forums.att.com/conversations/att-internet-email-security/allow-less-secure-apps-option-not-available-to-me/60a0794fb7af171b5e89e5a8

Contributor

 • 

11 Messages

1年前

No, I am not.

JefferMC

ACE - Expert

 • 

29.9K Messages

1年前

I think you'll get more success that way.

Contributor

 • 

11 Messages

1年前

ok . . I will give it a shot.

JefferMC

ACE - Expert

 • 

29.9K Messages

1年前

Your script (with my user information and Secure Email Key) worked for me.

Contributor

 • 

11 Messages

1年前

Excellent!  Thank you!

Need help?
New to AT&T Community?
New to the AT&T Community? Start by visiting the Community How-To.
New to the AT&T Community?
Visit the Community How-To.