3

I have a script which is fetching the latest event log from the remote machine. Send an event log details via outlook to specific group of people. The script is working fine on running through Powershell ISE but not sending email using task scheduler. Any help would be appreciated. Thanks

Script As below:

$Recipients="xyz@outlook.com","abc@outlook.com"

Foreach ($name in $Recipients) {
    $Outlook = New-Object -ComObject Outlook.Application

    $Mail = $Outlook.CreateItem(0)
    $Mail.Recipients.Add($name)
    $Mail.Subject ="Testing"
    $Mail.Body ="Demo"
    Write-Host "Sending Email"
    $Mail.Send()
}
n01d
  • 1,047
  • 8
  • 22
Vishal
  • 168
  • 1
  • 16
  • What error message do you get (if any) if you run the script manually through cmd `powershell -file '.\your-script.ps1'`? – n01d Dec 20 '16 at 11:36
  • The task scheduler probably uses a different user than you as commandline user. Make sure that the scheduler user has a proper Outlook mail profile. – Axel Kemper Dec 20 '16 at 11:37
  • @n01d No error showing it is working fine.But at the time of task scheduler it is not sending email – Vishal Dec 20 '16 at 11:43
  • @AxelKemper My Outlook has 3 accounts how can i specify from which account it should send email. – Vishal Dec 20 '16 at 11:44
  • When you start an `Outlook.Application` object, it connects to the default `Outlook` mail profile of the current user. You could create a technical user for mail scheduling purposes and configure the desired mail account as default. Tell the scheduler to use this technical user for your scheduled tasks. As alternative the `Namespace.Logon` method can be used to connect to a specified account. Ask Google for details. – Axel Kemper Dec 20 '16 at 11:56
  • @AxelKemper While scheduling the task i have provided my credentails to run by this username and password. But still unable to send email. May be i am messing with Actions tab with scheduler task?? – Vishal Dec 20 '16 at 12:00
  • I am running out of ideas. This [article](https://social.technet.microsoft.com/Forums/exchange/en-US/919bb65d-c739-485e-bbf5-9149b366be21/scheduled-automatic-email-send-out-exchange-2010-or-outlook-2010?forum=exchange2010) might contain some viable hints. Please share your findings here as answer. – Axel Kemper Dec 20 '16 at 12:10

3 Answers3

3

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

 $From = "YourEmail@gmail.com"
 $To = "AnotherEmail@YourDomain.com"
 $Cc = "YourBoss@YourDomain.com"
 $Attachment = "C:\temp\Some random file.txt"
 $Subject = "Email Subject"
 $Body = "Insert body text here"
 $SMTPServer = "smtp.gmail.com"
 $SMTPPort = "587"
 Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
 -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
 -Credential (Get-Credential) -Attachments $Attachment

Also you may consider using EWS, see EWS Managed API, EWS, and web services in Exchange for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
2

Task Scheduler runs as a service - and no Office app (Outlook included) can be used in a service.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • You mean to say i can't send email from outlook..while run powershell script from the task scheduler – Vishal Dec 20 '16 at 13:02
  • You should use Send-MailMessage, the .NET MailMessage class OR the EWS API (if you use an Exchange server) to use this in a scheduled task. To be honest, using the Outlook COM Object for this is really the wrong approach compared to the other options that are available to reach this goal. – bluuf Dec 20 '16 at 14:23
  • @Vishal - yes, that is what I mean. – Dmitry Streblechenko Dec 20 '16 at 15:39
0

You should just use Send-MailMessage instead of trying to use the Outlook COM object for this. This way you don't rely on a mail profile and other settings (which need to be set in Outlook under the user which you use to run the task). I also see some weird things in your code : for each recipient you create a new COM Outlook COM object, and then you try to send a mail : moving the For Loop to trigger After the creation of the Outlook COM object seems more logical.

bluuf
  • 936
  • 1
  • 6
  • 14