一、先在QQ邮箱中开启POP3/SMTP服务,然后会得到一个授权码,这个邮箱和授权码将用作登陆认证。
二、导入相关jar包:javax.mail.jar
三、引入配置文件:applicationContext-mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
<!--注册扫描器:扫描发送Email的工具类所在目录-->
<context:component-scan base-package="com.hcz.util"/>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!--发送邮件的服务器地址:下面地址可以查看
https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=167-->
<property name="host" value="smtp.qq.com" />
<property name="port" value="587" />
<!--发送者账号-->
<property name="username" value="xxxxx@qq.com" />
<!--密钥-->
<property name="password" value="第一步获取到的授权码" />
<property name="protocol" value="smtp" />
<property name="defaultEncoding" value="utf-8" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<!--<bean id="mail" class="com.hcz.service.mail.MailServiceImpl" p:sender-ref="mailSender"/>-->
</beans>
四、创建工具类
@Component
public class EmailUtil {
//发送邮件的组件,从Spring容器中获取进来
@Autowired
private JavaMailSender sender;
public void sendMail(String to,String context,String subject){
//邮件消息对象
SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo(to);
msg.setFrom("xxxxx@qq.com");
msg.setSubject(subject);//标题
msg.setText(context);//邮件内容
sender.send(msg);//发送
System.out.println("发送邮件成功");
}
}
五、在service层调用发送Email的工具类
@Service
public class WzServiceImpl implements WzService {
/**
* 业务层需要组装和加工,可能会用到几张表,这时需要导入多个dao
*/
@Autowired
private CarDao carDao;
@Autowired
private WzDao wzDao;
@Autowired
private RuleDao ruleDao;
@Autowired
private EmailUtil emailUtil;
@Override
public int add(String carno, Integer ruleid, String wzaddress,
Date wztime, String phone, String remark, Integer adminid) {
//页面输入是车牌号码,数据库先根据号码查询车牌完整信息,包括id和车主电话等
System.out.println("车牌号:"+carno);
Car car = carDao.selectByNo(carno);
System.out.println("car信息"+car);
if (car == null){
//如果找不到,说明应该是录错号码,返回0
return 0;
}
//新增时候也要插入扣分和罚款,所以也要查询Rule的完整信息
Rule rule = ruleDao.selectById(ruleid);
System.out.println("rule信息"+rule);
//插入违章信息
int n = wzDao.insert(car.getId(),rule.getId(),wzaddress,wztime,
rule.getPay(),rule.getScore(),phone,remark, adminid);
System.out.println("插入信息"+n);
//发送短信给车主
//SmsUtil.sendSms(car.getPhone(),"尊敬的"+car.getUserName()+":你有一笔违章记录,请您及时确认违章内容:"+rule.getName());
//发送邮件【需要注入进来EmailUtil】
emailUtil.sendMail(car.getUserEmail(),car.getNo()+":违章通知","尊敬的"
+car.getUserName()+":你有一笔违章记录,请您及时确认违章内容:"+rule.getName());
System.out.println(car.getUserEmail());
return n;
}
}
版权声明:本文为hcz666原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。