Use JavaMailSender in the development project to realize the code of sending mail Email function

1. First, open the POP3/SMTP service in the QQ mailbox, and then you will get an authorization code. This mailbox and authorization code will be used for login authentication.

Insert picture description here

2. Import the relevant jar package: javax.mail.jar
3. Import the configuration file: 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="[email protected]" />
		 <!--密钥-->
		<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>
Fourth, create tools
@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("[email protected]");
        msg.setSubject(subject);//标题
        msg.setText(context);//邮件内容
        sender.send(msg);//发送
        System.out.println("发送邮件成功");
    }
}

Fifth, call the tool class to send Email in the service layer
@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;
    }

    
}

Guess you like

Origin blog.csdn.net/hcz666/article/details/114705528