我正在使用Grails 3.0.12,我正在使用Quartz执行作业,我现在尝试做的是每次(在本例中是每5秒)发送一封电子邮件。我的服务文件夹中有电子邮件服务。这是我的代码:
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 "xxxxxx@gmail.com"
subject "hello"
body "hello"
}
}
}
} 我试图创建一个服务类的实例来调用mailService.sendMail,但没有成功。
非常感谢你的帮助。:)
发布于 2016-04-15 23:50:41
看起来你正在尝试在你的工作中使用邮件插件,但是你还没有将邮件服务注入到你的工作中。
添加:
def mailService添加到您的类,它将被注入并可用。有关服务注入的更多信息,可以在这里找到https://grails.github.io/grails-doc/latest/guide/single.html#dependencyInjectionServices
有关配置和使用邮件插件的更多信息在此处- https://grails.org/plugins.html#plugin/mail
发布于 2018-03-17 14:09:39
下面是我如何做类似的事情,使用@Scheduled注释从目录中的文件发送电子邮件。
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++++++++++"
}
}然后我有我的emailService来做剩下的事情。希望这能帮助到别人。
https://stackoverflow.com/questions/36651292
复制相似问题