Would it not be better to refactor out the logic into its own service and then both the controller and scheduled job call the service ?
I got this to work...kind of. I now have what I think is a good reference to MailMessaging and ExampleMailer and this does run when calling a controller and when calling the mailer inside the Execute method. I hade to add this part to inject.
public class TestEpiJob(EmailMessaging emailMessaging, ExamplesMailer examplesMailer) :
BaseScheduledTask(emailMessaging, examplesMailer)
It however does not auctuall send an email so I need to look into this. I can get it to send emails using the sameExampleMailer outside of epi.
Code removed
Hi Eric,
It sounds like your trying to use the Razor view as a template to sending the email, correct me if you think I am wrong here.
If this is the case then there is a good blog post on how to do this in pure MVC on Rick Strahl's site https://weblog.west-wind.com/posts/2022/Jun/21/Back-to-Basics-Rendering-Razor-Views-to-String-in-ASPNET-Core.
If I get a chance to over the weekend I will attempt to see if i can get a working solution of this blog post up and running.
There is also an example here that uses T4 templates to send an email replacing parameters https://mahdi-karimipour.medium.com/message-templates-with-t4-and-c-partial-classes-946b0ccda6fe
Again i will try and get something running so you can see how it should work in both cases.
Paul
Hi Eric,
I have pushed up some code that will help you with sending emails on github here https://github.com/paulmcgann/playground.
I created an email interface and then implemented that interface with an email service you can see this in the following folder:
https://github.com/paulmcgann/playground/tree/main/src/playground/Features/Email
Then in the startup.cs I registered some services one was for the FluentEmail library and the other is for the email interface and email service you can see that here:
https://github.com/paulmcgann/playground/blob/main/src/playground/Startup.cs
You will need to enter you own details for the FluentEmail to make this work you will need a host, username and password.
Finally I injected the email service into the StartPageController:
https://github.com/paulmcgann/playground/blob/main/src/playground/Controllers/StartPageController.cs
And a scheduled job:
This way you can inject your email service across various parts of your code and only write the code for it once and re-use and your system.
Hopefully this will get you going in the right direction.
If you need more help let us know and we can see where we can help you.
Paul
I need to be able to call a controller method from an EPiServer scheduled job and send email from the controller . The controller method work perfect if called normally and send the email, but not if called from the scheduled job.
In Startup I have this but it did not work: services.AddScoped<BaseScheduledTask, BaseScheduledTask>();