grails 发送邮件
发布时间:2020-12-14 17:10:19 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 首先在buildConfig中的plugins { compile ":mail:1.0.1" }然后再config中配置grails.mail.default.from = "Xxxx [email?protected]"grails {mail {hos
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 首先在buildConfig中的plugins { compile ":mail:1.0.1" } 然后再config中配置 grails.mail.default.from = "Xxxx [email?protected]>" grails { mail { host = "smtp.gmail.com" username = "[email?protected]" password = "xxxx" port=587 props = ["mail.smtp.auth":"true","mail.smtp.socketFactory.port":"465","mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory","mail.smtp.socketFactory.fallback":"false"] } } 在resources中加入如下 代码: beans = { taskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) { corePoolSize = 10 maxPoolSize = 30 queueCapacity = 6 keepAliveSeconds = 2000 } mail(SendEmailAsynchronously) { taskExecutor = ref('taskExecutor') } } 再建一个 工具类代码如下: import org.codehaus.groovy.grails.commons.ApplicationHolder import org.springframework.core.task.TaskExecutor; class SendEmailAsynchronously { static transactional = true private static TaskExecutor taskExecutor def ctx = ApplicationHolder.getApplication().getMainContext(); def mailService = ctx.getBean("mailService"); def messageSource = ctx.messageSource def fromEmail def toEmail def mailObject def emailBody def attach public SendEmailAsynchronously() { } public SendEmailAsynchronously(def toEmail,def mailObject,def emailBody) { this.fromEmail = 'xxx<[email?protected]>' this.toEmail = toEmail this.mailObject = mailObject this.emailBody = generateContent(emailBody) } public SendEmailAsynchronously(def toEmail,def emailBody,def attach) { this.fromEmail = 'xxx [email?protected]' this.toEmail = toEmail this.mailObject = mailObject this.emailBody = generateContent(emailBody) this.attach = attach } public void sendMail() { taskExecutor.execute( new Runnable(){ @Override public void run() { try { println "start send mail" mailService.sendMail { from fromEmail to toEmail subject mailObject html emailBody } println "end send mail" }catch(Exception e){ println e try{ User.findByEmail(toEmail).delete() }catch (Exception ue){ print "User=="+ue } } } }) } public void setTaskExecutor(TaskExecutor taskExecutor) { this.taskExecutor = taskExecutor } private String generateContent(bodyContent) { if(bodyContent instanceof String) { return bodyContent }else if(bodyContent instanceof Closure) { //bodyContent is a closure def sw = new StringWriter() def mkp = new groovy.xml.MarkupBuilder(new PrintWriter(sw)) mkp.html { body(bodyContent) } return sw.toString() } } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |