充电宝什么牌子最好是Greenmail

加花瓣为微信好友1252人阅读
1、自定义异常类AccountEmailException
public&class&AccountEmailException
&&&&extends&Exception
&&&&private&static&final&long&serialVersionUID&=&-4501672L;
&&&&public&AccountEmailException(&String&message&)
&&&&&&&&super(&message&);
&&&&public&AccountEmailException(&String&message,&Throwable&throwable&)
&&&&&&&&super(&message,&throwable&);
2、发邮件的接口类
public&interface&AccountEmailService
&&&&&*&&&&&&&&&发送邮件
&&&&&*&&&&&&&&&&to为接收地址,subject为邮件主题,htmlText为邮件内容
&&&&&&*&&&@author&lanjh&上午10:51:45
&&&&&&*&&&@return&void
&&&&&&*&&&@throws&&抛出异常说明
&&&&void&sendMail(&String&to,&String&subject,&String&htmlText&)
&&&&&&&&throws&AccountEmailE
3、接口实现类
public&class&AccountEmailServiceImpl
&&&&implements&AccountEmailService
&&&&//spring&framework中帮助邮件发送工具类
&&&&private&JavaMailSender&javaMailS
&&&&private&String&systemE
&&&&public&void&sendMail(&String&to,&String&subject,&String&htmlText&)
&&&&&&&&throws&AccountEmailException
&&&&&&&&try
&&&&&&&&&&&&//msg对应着将要发送的邮件
&&&&&&&&&&&&MimeMessage&msg&=&javaMailSender.createMimeMessage();
&&&&&&&&&&&&//帮助设置邮件msg相关信息
&&&&&&&&&&&&MimeMessageHelper&msgHelper&=&new&MimeMessageHelper(&msg&);
&&&&&&&&&&&&//systemEmail&系统邮箱设置
&&&&&&&&&&&&msgHelper.setFrom(&systemEmail&);
&&&&&&&&&&&&msgHelper.setTo(&to&);
&&&&&&&&&&&&msgHelper.setSubject(&subject&);
&&&&&&&&&&&&//true表示邮件内容为html格式
&&&&&&&&&&&&msgHelper.setText(&htmlText,&true&);
&&&&&&&&&&&&javaMailSender.send(&msg&);
&&&&&&&&catch&(&MessagingException&e&)
&&&&&&&&&&&&throw&new&AccountEmailException(&&Faild&to&send&mail.&,&e&);
&&&&public&JavaMailSender&getJavaMailSender()
&&&&&&&&return&javaMailS
&&&&public&void&setJavaMailSender(&JavaMailSender&javaMailSender&)
&&&&&&&&this.javaMailSender&=&javaMailS
&&&&public&String&getSystemEmail()
&&&&&&&&return&systemE
&&&&public&void&setSystemEmail(&String&systemEmail&)
&&&&&&&&this.systemEmail&=&systemE
public&class&AccountEmailServiceTest
&&&&private&GreenMail&greenM
&&&&@Before
&&&&public&void&startMailServer()
&&&&&&&&throws&Exception
&&&&&&&&greenMail&=&new&GreenMail(&ServerSetup.SMTP&);
&&&&&&&&greenMail.setUser(&&lanjh&,&&******&&);
&&&&&&&&greenMail.start();
&&&&public&void&testSendMail()
&&&&&&&&throws&Exception
&&&&&&&&ApplicationContext&ctx&=&new&ClassPathXmlApplicationContext(&&account-email.xml&&);
&&&&&&&&AccountEmailService&accountEmailService&=&(AccountEmailService)&ctx.getBean(&&accountEmailService&&);
&&&&&&&&String&subject&=&&Test&Subject&;
&&&&&&&&String&htmlText&=&&&h3&Test&/h3&&;
&&&&&&&&accountEmailService.sendMail(&&&,&subject,&htmlText&);
&&&&&&&&greenMail.waitForIncomingEmail(&2000,&1&);
&&&&&&&&Message[]&msgs&=&greenMail.getReceivedMessages();
&&&&&&&&assertEquals(&1,&msgs.length&);
&&&&&&&&assertEquals(&&&,&msgs[0].getFrom()[0].toString()&);
&&&&&&&&assertEquals(&subject,&msgs[0].getSubject()&);
&&&&&&&&assertEquals(&htmlText,&GreenMailUtil.getBody(&msgs[0]&).trim()&);
&&&&@After
&&&&public&void&stopMailServer()
&&&&&&&&throws&Exception
&&&&&&&&greenMail.stop();
5、相关配置文件
account-mail.xml
&?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-2.5.xsd&&
&&&&&bean&id=&propertyConfigurer&
&&&&&&&&class=&org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&&
&&&&&&&&&property&name=&location&&value=&classpath:account-service.properties&&/&
&&&&&/bean&
&&&&&bean&id=&javaMailSender&&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.juvenxu.mvnbook.account.email.AccountEmailServiceImpl&&
&&&&&&&&&property&name=&javaMailSender&&ref=&javaMailSender&&/&
&&&&&&&&&property&name=&systemEmail&&value=&${email.systemEmail}&&/&
&&&&&/bean&
account-service.properties
email.protocol=smtp
email.host=localhost
email.port=25
email.username=lanjh
email.password=*****
email.auth=true
greenmail& api参考
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:54818次
排名:千里之外
原创:27篇
转载:92篇
评论:11条
(3)(4)(48)(22)(27)(15)}

我要回帖

更多关于 什么值得买 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信