标签:ase 1.0 user ring scope snapshot aging javamail 项目打包
groupId:项目 通常用域名的反向来表示如:com.help18.hello artifactId:Mavne项目模块 hello-mvn01 version:版本 packaging: jar或war 即打包方式 classifier:定义构建输出一些附属构件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.help18.mvn.account</groupId> <artifactId>account-email</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>account-email</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.5.0-b01</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>com.icegreen</groupId> <artifactId>greenmail</artifactId> <version>1.3.1b</version> <scope>test</scope> </dependency> </dependencies> </project>
<build> <defaultGoal>compile</defaultGoal> </build>
三、编写程序代码
1.在src/main/java中创建AccountEmailService接口
package com.help18.account.email; public interface AccountEmailService { public void sendMail(String to,String subject,String htmlText) throws AccountEmailException; }
2.创建异常处理类AccountEmailException ,此类继承了Exception类
package com.help18.account.email; public class AccountEmailException extends Exception { private static final long serialVersionUID = -2541187591129155636L; public AccountEmailException(String message){ super(message); } public AccountEmailException(String message,Throwable throwable){ super(message,throwable); } }
package com.help18.account.email; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; public class AccountEmailServiceImpl implements AccountEmailService { private JavaMailSender javaMailSender; private String systemEmail; public void sendMail(String to, String subject, String htmlText) throws AccountEmailException { // TODO Auto-generated method stub try{ MimeMessage msg = javaMailSender.createMimeMessage(); MimeMessageHelper msgHelper = new MimeMessageHelper(msg); msgHelper.setFrom(systemEmail); msgHelper.setTo(to); msgHelper.setSubject(subject); msgHelper.setText(htmlText,true); /*发送邮件*/ javaMailSender.send(msg); } catch(MessagingException e){ throw new AccountEmailException("Faild to send mail.",e); } } public JavaMailSender getJavaMailSender() { return javaMailSender; } public void setJavaMailSender(JavaMailSender javaMailSender) { this.javaMailSender = javaMailSender; } public String getSystemEmail() { return systemEmail; } public void setSystemEmail(String systemEmail) { this.systemEmail = systemEmail; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:service.properties"></property> </bean> <bean id="javaMailServer" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="protocol" value="${email.protocol}"/> <property name="host" value="${email.host}"/> <property name="port" value="${email.port}"/> <property name="username" value="${email.username}"/> <property name="password" value="${email.password}"/> <property name="javaMailProperties"> <props> <prop key="mail.${email.protocol}.auth">${email.auth}</prop> </props> </property> </bean> <bean id="accountEmailService" class="com.help18.account.email.AccountEmailServiceImpl"> <property name="javaMailSender" ref="javaMailServer"></property> <property name="systemEmail" value="${email.systemEmail}"/> </bean> </beans>
五、创建properties属性文件
email.protocol=smtp email.host=smtp.163.com email.port=25 email.username=XXXXX@163.com email.password=password email.auth=true email.systemEmail=email
六、创建单元测试类
package com.help18.account.email; import static junit.framework.Assert.assertEquals; import javax.mail.Message; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.icegreen.greenmail.util.GreenMail; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.ServerSetup; public class AccountEmailServiceTest { private GreenMail greenMail; @Before public void startMailServer() throws Exception { greenMail = new GreenMail(ServerSetup.SMTP); greenMail.setUser("xxxx@163.com", "xxxxx"); greenMail.start(); } @Test public void testSendMail() throws Exception{ @SuppressWarnings("resource") ApplicationContext ctx = new ClassPathXmlApplicationContext("account-email.xml"); AccountEmailService accountEmailService = (AccountEmailService)ctx.getBean("accountEmailService"); String subject = "Test Subject 20170401"; String htmlText = "<H3>Test mail.163.com </H3>"; System.out.println("test-------"); accountEmailService.sendMail("xxxx@163.com", subject, htmlText); System.out.println("------test send mail finished-------"); greenMail.waitForIncomingEmail(2000,1); Message[] msgs = greenMail.getReceivedMessages(); System.out.println("msgs.lengh="+msgs.length); assertEquals(1,msgs.length); assertEquals(subject,msgs[0].getSubject()); assertEquals(htmlText,GreenMailUtil.getBody(msgs[0]).trim()); } @After public void stopMailServer() throws Exception{ greenMail.stop(); } }
<exclusions> <exclusion> <groudId></groupId> <artifactId></artifactId> </exclusion> </exclusions>
2.归类依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <springframework.version>4.2.4.RELEASE</springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> </dependencies>
总结:
maven坐标与依赖是maven中一个比较重要的知识点,它标识了你的jar包或war包的位置,以及依赖的各个包,由于依赖的关系,无需手动添加这个项目中所用到的资源,配置好pom文件后会自动从私服或中央仓库下载包到本地,这样会省去很多的麻烦,这也是maven构建项目最大的优点。
创建一个maven项目,主要按照maven要求的目录结构进行创建,最重要的是pom.xml文件的构建。
上面的例子是基于spring框架实现的,之前没有用maven构建项目时,需要一个个找jar包下载,再编辑项目把包添进来,太麻烦了,现在非常简单,执行了mvn compile可以自动下载依赖的包。
标签:ase 1.0 user ring scope snapshot aging javamail 项目打包
原文地址:http://www.cnblogs.com/rico2017/p/6674891.html