`

groovy ant 发送邮件

阅读更多

在日常的工作处理中,常常会要求我们对一项任务快速完成,且这项任务以后可能就不会出现,如果我们靠写java代码去完成,那可能就太复杂了, 解决的方法是请出我们的脚本语言, 脚本语言具有快速开发的优势。 作为java开发人员,groovy是我们的首选的脚本语言,其优点我就不多说了,大家可以去google下. ant作为一个构建工具,已给我们提供了很多的日常工作常见的任务处理。所以整合groovy 与 ant 将是我们今天日常工作处理的一大利器。

 

ant 发送邮件要依赖mail.jar和activation.jar(如果你的jdk>=6,则可以不要它),它们可以在spring/lib/j2ee目录下找到。把它们放到 ant_home/lib下。

接下来是写ant任务

<target name="send-mail">  
  <mail mailhost="smtp.163.com" user="your_name" password="your_password" subject="邮件主题">  
     <from address="your_name@126.com"/>  
     <to address="your_other_name@yahoo.com"/>  
     <message>邮件内容</message>  
   </mail>  
</target>

 

如果要用groovy 来整合,则我们要把mail.jar和activation.jar及ant_home/lib/ant-javamail.jar 复制到groovy_home/lib下, 接下来写如下groovy代码

ant = new AntBuilder()
 
 def mail(subject, body, attachment = [dir:".",files:[]]) {
     ant.mail(mailhost:"mail.com", mailport:"1025", user:"mailer", password:"123", subject:"${subject}") {
         from(address:"nobody@mail.com")
         to(address:"nobody@mail.com")
         message("${body}")
        attachments() {
             if (attachment.files) {
                 fileset(dir:"${attachment.dir}") {
                     attachment.files.each {
                         include(name:it)
                     }
                 }
             }
         }
     }
 }
 
attachment = [dir:"/tmp", files:["some.properties","some.sql"]]
mail("Test mail message at ${new Date()}", "This is a test message.", attachment)
 

 

关于groovy整合ant,大家可以参考

 

http://groovy.codehaus.org/Using+Ant+from+Groovy

http://docs.codehaus.org/display/GROOVY/Using+Ant+Libraries+with+AntBuilder

http://www.onjava.com/pub/a/onjava/2007/03/23/using-groovy-to-send-emails.html?page=3

http://memo.feedlr.com/?p=5

整合spring与邮件发送
http://grails.org/Sending+SMTP+Authenticated+Email,+Html+content+with+GroovyTemplates+and+Spring+integration

 

1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics