1

My script for sending emails using Office365 with an admin credentials. The script worked about 6 month, ago but recently it has not been working although it has not been changed.

I have tried to remove UseSsl but it did not work.

try {

    Send-MailMessage -To "myemail@domain.com" -Subject "test" `
        -From "support@domain.com" `
        -SmtpServer 'smtp.office365.com' -Port 587 -UseSsl:$true `
        -Credential $Cred -Body "testbody"
    Write-Host "Message sent to me." -BackgroundColor Black -ForegroundColor Green
}
catch [System.Exception] {
    throw $_
} 

Here is the exception I get:

Send-MailMessage : Transaction failed. The server response was: 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message.

Dalian
  • 51
  • 1
  • 5

1 Answers1

4

Found the solution. The credential used does not have permissions to send email as support@domain.com in -From "support@domain.com"

Dalian
  • 51
  • 1
  • 5
  • Nice job solving that on your own. – Tim Liston Jan 15 '19 at 20:00
  • How did you actually find the problem? – Michael Hampton Jan 15 '19 at 21:12
  • Well done you solved the issue :) – Kelvin_D Jan 16 '19 at 02:11
  • The following link might be helpful to others with the same issue: https://docs.microsoft.com/en-us/exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-office-3 – Joe Jan 16 '19 at 02:15
  • @MichaelHampton I just tried the admin email (the credential used to connect to exchange in the above powershell script) for the `-From` line instead of `support@domain.com` and that worked. Admin credentials did not have permissions to send email on behalf of (or as) `support@domain.com`. – Dalian Jan 16 '19 at 10:34