0

I need to send an email report from a script. If I do this from one of our mail relays the email appears as [EXTERNAL]. Since this report includes links, I want users to trust it as coming internally.

I therefore want to use the domain-com.mail.protection.outlook.com SMTP connector, but when I do this I get Send-MailMessage: The operation has timed out.

This is the send mail line: Send-MailMessage -SmtpServer domain-com.mail.protection.outlook.com -Port 587 -Credential $credential -From my.user@domain.com -To other.user@domain.com -Subject "External Test" -Body Test

The $credential variable holds the output of Get-Credential which I logged in with my user account.

I've tried previously to get this to work, but gave up. I have also tried port 25 and Windows Powershell as well as Powershell 7.2.6.

SMTP Authentication is enabled for my user and companywide. I also tried smtp.office365.com as the server.

neildeadman
  • 684
  • 4
  • 20
  • 34
  • 1
    I would recommend to check if Send-MailMessage uses TLS/STARTTLS by default (because you have not added the `-UseSsl` option). Plain text connections are often disabled today so this could be a reason for the time out. And if TLS is used make sure the expected version is supported by your OS and is used https://stackoverflow.com/a/53785663/150978 – Robert Sep 13 '22 at 11:27
  • I did miss the `-UseSsl` flag, but it made no difference. I always for get to specify the TLS version so I was hopeful that was my issue, however the results are the same :( – neildeadman Sep 13 '22 at 13:30
  • I've managed to get it working and it seems it was all of these things. The TLS version needed to be specified, I needed to add the `-UseSsl` option and switch to connecting to `smtp.office365.com` - WIthout all 3 it wouldn't work. – neildeadman Sep 14 '22 at 13:15

1 Answers1

0

Check that the server you're running this script on is actually allowed by your network to connect to outside systemes on TCP port 587. You can use Test-NetConnection for this.

Also, as Robert said, add the -UseSsl switch to the command and make sure to use TLS 1.2.

Massimo
  • 70,200
  • 57
  • 200
  • 323
  • I cannot connect to port 587 from my corporate network, so I guess there is a firewall rule required here. However, I can also not connect from my home network where I don't have anythin gblocking me. I did miss the `-UseSsl` flag, but it made no difference. I always for get to specify the TLS version so I was hopeful that was my issue, however the results are the same :( – neildeadman Sep 13 '22 at 13:32
  • You should use`smtp.office365.com`. – Massimo Sep 13 '22 at 14:13
  • 1
    I've managed to get it working and it seems it was all of these things. The TLS version needed to be specified, I needed to add the `-UseSsl` option and switch to connecting to `smtp.office365.com` - WIthout all 3 it wouldn't work. – neildeadman Sep 14 '22 at 13:15