下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
首先在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() } } }
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。