1

I've got a single multi-role Exchange 2010 server and a need to execute a Powershell script on monthly schedule.

And i really would like to send a report via email to some local company's mailbox by the end of the task.

I created a service account with required permissions and wrote a Powershell script that's doing everything fine when run as service account - except the email report part.

Adding my Exchange server's IP to anonymous-allowed Receive connector doesn't sound right to me, maybe there is kind of best practice to do it?

Any opinion is much appreciated!

UPDATE:

As for the mail part of the script, usually i use a .NET Net.Mail.MailMessage object like this:

$SmtpServer = "exchangeserver.corp.company.com" $Msg = New-Object Net.Mail.MailMessage $Smtp = New-Object Net.Mail.SmtpClient($SmtpServer) $Msg.From = "[email protected]" $Msg.ReplyTo = "[email protected]" $Msg.To.Add("[email protected]") $Msg.Subject = "Monthly script execution report" $Msg.Body = "Everything's ok, some statistics etc." $Smtp.Send($Msg) 

When i use this code the Exchange server answers with "Service unavailable". To me it makes sense since there are no Receive connectors configured. Standard Send-MailMessage Powershell command gives the same result so the problem is not with the code, actually it works fine on non-Exchange servers.

Currently i've got three Receive connectors:

  • one for external mail with FQDN mail.company.com accepting connections on port 25
  • one for internal Exchange clients with FQDN exchangeserver.corp.company.com on standard SSL port 587
  • and one for list of local servers who send mail via Exchange without authentication with FQDN exchangeserver.corp.company.com on port 25 (not a perfect solution and i would prefer not to add my Exchange server's IP to the list)

So i'm looking for a nice-and-secure way to make it work, thanks in advance!

2
  • Can you show us the part where you send the e-mail? Commented Apr 1, 2015 at 10:45
  • Johan, updated the question, please see above. Commented Apr 1, 2015 at 12:07

1 Answer 1

0

The solution is quite simple, having a password for ExchangeServiceAccount as SecureString $ExchangeServicePassword variable:

# Mail server $SmtpServer = "mail.company.com" # .NET object MailMessage $Msg = New-Object Net.Mail.MailMessage # .NET object SMTP server and SMTP authentication parameters $Smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $Smtp.Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "ExchangeServiceAccount", $ExchangeServicePassword $Smtp.EnableSsl = $true # Email structure $Msg.From = "[email protected]" $Msg.ReplyTo = "[email protected]" $Msg.To.Add("[email protected]") $Msg.Subject = "Subject" $Msg.Body = "Some text" $Msg.SubjectEncoding = [System.Text.Encoding]::UTF8 $Msg.BodyEncoding = [System.Text.Encoding]::UTF8 # Send email $Smtp.Send($Msg)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.