用ANT来实现邮件发送
下面是我用ANT来实现邮件发送的代码:
Code
<?xml version = "1.0" encoding = "utf-8"?>
<project default = "sendmail">
<target name = "sendmail" description = "send mail">
<mail mailhost="smtp.126.com" mailport="25" subject="The Build Test" user="×××" password="***">
<from address=***@126.com name="Wentao Sun" />
<to address=***@126.com name="Wentao Sun" />
<message>Send email by ANT ! </message>
</mail>
</target>
</project>
<?xml version = "1.0" encoding = "utf-8"?>
<project default = "sendmail">
<target name = "sendmail" description = "send mail">
<mail mailhost="smtp.126.com" mailport="25" subject="The Build Test" user="×××" password="***">
<from address=***@126.com name="Wentao Sun" />
<to address=***@126.com name="Wentao Sun" />
<message>Send email by ANT ! </message>
</mail>
</target>
</project>
Code
1 <?xml version="1.0"?>
2 <project name="springproj" basedir="." default="mail">
3 <property name="pro.name" value="springproj" />
4 <property name="pro.author" value="Danlley Wei" />
5 <property name="src.dir" value="src/main/java" />
6 <property name="pro.web.root" value="war" />
7 <property name="pro.web.source" value="${pro.web.root}/WEB-INF" />
8 <property name="pro.build.path" value="${pro.web.source}/classes" />
9 <property name="user.dir" value="${pro.build.path}" />
10 <target name="mail" depends="mkzip">
11 <mail mailhost="smtp.126.com" mailport="25" subject="The Build Test" user="用户名" password="邮箱密码">
12 <from address="发送地址" name="Danlley Wei" />
13 <fileset dir="report/html">
14 <include name="**/test-result${ant.project.name}.zip" />
15 </fileset>
16 <to address="邮件接收地址" name="Danlley Wei" />
17 <message>The ${pro.name}--${pro.author} has been tested ! </message>
18 </mail>
19 </target>
20 <target name="mkzip" depends="report">
21 <jar destfile="report/html/test-result${ant.project.name}.zip">
22 <fileset dir="report/html">
23 <include name="**/*.html" />
24 <include name="**/*.css" />
25 <include name="**/*.txt" />
26 </fileset>
27 </jar>
28 </target>
29 <target name="report" depends="junit">
30 <junitreport id="myJUnitReport" taskname="reported" todir="report" description="Junit Report">
31 <fileset dir="report">
32 <include name="TEST-*.xml" />
33 </fileset>
34 <report todir="report/html" />
35 </junitreport>
36 </target>
37 <target name="junit" depends="build">
38 <mkdir dir="report/html" />
39 <junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">
40 <formatter type="plain" usefile="false" />
41 <formatter type="xml" />
42 <test name="org.danlley.hibernate.dao.DeptDAOImplTest" todir="report" />
43 <classpath refid="master-classpath" />
44 </junit>
45 </target>
46 <target name="build" depends="prepare">
47 <javac destdir="${pro.build.path}" target="1.5">
48 <src path="${src.dir}" />
49 <classpath refid="master-classpath" />
50 </javac>
51 <javac destdir="${pro.build.path}" target="1.5">
52 <src path="src/main/test" />
53 <classpath refid="master-classpath" />
54 </javac>
55 </target>
56 <target name="prepare" depends="clean">
57 <copy todir="${pro.build.path}">
58 <fileset dir="${src.dir}">
59 <include name="**/*.properties" />
60 <include name="**/*.xml" />
61 </fileset>
62 </copy>
63 </target>
64 <target name="clean">
65 <delete>
66 <fileset dir="${pro.build.path}">
67 <include name="**/*.*" />
68 </fileset>
69 <fileset dir="report">
70 <include name="**/*.*" />
71 </fileset>
72 </delete>
73 </target>
74 <path id="master-classpath">
75 <fileset dir="lib">
76 <include name="*.jar" />
77 </fileset>
78 <pathelement path="${pro.build.path}" />
79 </path>
80 </project>
81
1 <?xml version="1.0"?>
2 <project name="springproj" basedir="." default="mail">
3 <property name="pro.name" value="springproj" />
4 <property name="pro.author" value="Danlley Wei" />
5 <property name="src.dir" value="src/main/java" />
6 <property name="pro.web.root" value="war" />
7 <property name="pro.web.source" value="${pro.web.root}/WEB-INF" />
8 <property name="pro.build.path" value="${pro.web.source}/classes" />
9 <property name="user.dir" value="${pro.build.path}" />
10 <target name="mail" depends="mkzip">
11 <mail mailhost="smtp.126.com" mailport="25" subject="The Build Test" user="用户名" password="邮箱密码">
12 <from address="发送地址" name="Danlley Wei" />
13 <fileset dir="report/html">
14 <include name="**/test-result${ant.project.name}.zip" />
15 </fileset>
16 <to address="邮件接收地址" name="Danlley Wei" />
17 <message>The ${pro.name}--${pro.author} has been tested ! </message>
18 </mail>
19 </target>
20 <target name="mkzip" depends="report">
21 <jar destfile="report/html/test-result${ant.project.name}.zip">
22 <fileset dir="report/html">
23 <include name="**/*.html" />
24 <include name="**/*.css" />
25 <include name="**/*.txt" />
26 </fileset>
27 </jar>
28 </target>
29 <target name="report" depends="junit">
30 <junitreport id="myJUnitReport" taskname="reported" todir="report" description="Junit Report">
31 <fileset dir="report">
32 <include name="TEST-*.xml" />
33 </fileset>
34 <report todir="report/html" />
35 </junitreport>
36 </target>
37 <target name="junit" depends="build">
38 <mkdir dir="report/html" />
39 <junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">
40 <formatter type="plain" usefile="false" />
41 <formatter type="xml" />
42 <test name="org.danlley.hibernate.dao.DeptDAOImplTest" todir="report" />
43 <classpath refid="master-classpath" />
44 </junit>
45 </target>
46 <target name="build" depends="prepare">
47 <javac destdir="${pro.build.path}" target="1.5">
48 <src path="${src.dir}" />
49 <classpath refid="master-classpath" />
50 </javac>
51 <javac destdir="${pro.build.path}" target="1.5">
52 <src path="src/main/test" />
53 <classpath refid="master-classpath" />
54 </javac>
55 </target>
56 <target name="prepare" depends="clean">
57 <copy todir="${pro.build.path}">
58 <fileset dir="${src.dir}">
59 <include name="**/*.properties" />
60 <include name="**/*.xml" />
61 </fileset>
62 </copy>
63 </target>
64 <target name="clean">
65 <delete>
66 <fileset dir="${pro.build.path}">
67 <include name="**/*.*" />
68 </fileset>
69 <fileset dir="report">
70 <include name="**/*.*" />
71 </fileset>
72 </delete>
73 </target>
74 <path id="master-classpath">
75 <fileset dir="lib">
76 <include name="*.jar" />
77 </fileset>
78 <pathelement path="${pro.build.path}" />
79 </path>
80 </project>
81
target依赖关系:
mail→mkzip→report→junit→build→prepare→clean
工程classpath:master-classpath
clean节点:
删除测试和编译过程中生成的所有文件
prepare节点:
将工程编译打包所需资源文件全部拷贝到编译路径下
build节点:
生成工程源文件和测试代码源文件的二进制版本
junit节点:
运行测试用例
report节点:
生成测试报告
mkzip节点:
将生成的测试报告以zip格式进行打包
mail节点:
将测试结果发送到开发小组邮箱
1. 我在模板 中使用的126邮箱,如果你的邮箱是新申请的126邮箱,上述模板可能没办法发送你的邮件,原因是126邮箱对于新申请的用户暂时不开通smtp和pop3服务 2.ant并不会自带安装发送邮件所需相关资源包,因此需要我们手动到sun官方网站下载,然后把资源包放到ant的lib下面,我的位置是:D:\apache-ant-1.7.0\lib 可以从下面地址下载ant发邮件所需的两个资源(mail.jar和activation.jar): http://java.sun.com/products/javamail http://java.sun.com/products/javabeans/glasgow/jaf.html