Reputation: 59
I'm working with Grails 3.0.12, I'm using Quartz to execute a job, what I try to do now is to send an email every time (In this case every 5 seconds). I have email Service in my Services folder. This is my code:
class EnviaCorreosJob{
NotifierService notificar
Integer diasParaCorreo = 30
static triggers =
{
cron name: 'myTrigger', cronExpression: "*/5 * * * * ?"
}
def group = "MyGroup"
def description = "Example job with Cron Trigger"
def fechaHoy = new Date()
def execute()
{
println "------------------ Running every 5 seconds -------------------"
def queryAgenda = Agenda.where
{
inicio_cita <= (fechaHoy + diasParaCorreo)
}
def listaAgenda = queryAgenda.list()
println "----------------------Dates list : " + listaAgenda
log.info "listaAgenda: " + listaAgenda
log.info "listaAgendaTamaño: " + listaAgenda.size()
listaAgenda.each
{
agenda ->
println "it's inside"
mailService.sendMail
{
to "[email protected]"
subject "hello"
body "hello"
}
}
}
}
I tried to make an instance of Service class to call mailService.sendMail but didn't work.
Thanks so much for your help. :)
Upvotes: 0
Views: 141
Reputation: 51
Here is how I have done something similar, sending emails from files within a directory using the @Scheduled annotation.
EmailService emailService
static boolean lazyInit = false
@Scheduled(fixedDelay = 3000L, initialDelay = 2000L)
void executeEveryFourtyFive() {
dire = new File(configfilepath)
dire.eachFileRecurse (FileType.FILES) { fileEmail ->
listEmail << fileEmail
}
listEmail.each {
filepath = it.path
sourceFolder = new File(filepath)
if (sourceFolder.exists()){
String fileContents = new File(filepath).text
def emailaddress = emailaddress
def emailmessage = message
def emailtitle = title
def emailsubject = subject
def emailfrom = emailfrom
emailService.send(emailaddress,emailmessage,emailsubject...)
}
}
}catch(Exception exc){
exc.printStackTrace()
}
}else{
println "++++++++++email service off++++++++++"
}
}
And then I have my emailService to do the rest. Hope it helps someone.
Upvotes: 0
Reputation: 2340
It looks like you are trying to use the mail plugin in your job but you haven't injected the mail service into your Job.
Add:
def mailService
To your class and it will be injected and useable. More on service injection can be found here https://grails.github.io/grails-doc/latest/guide/single.html#dependencyInjectionServices
More info on configuring and using the mail plugin is here - https://grails.org/plugins.html#plugin/mail
Upvotes: 2