1 . 用到2個包 , 一個包對象SimpleMailMessage存儲郵件信息 , 一個對象MailSender對象發送simplemailmessage
javax.mail.jar
2 . 配置 , springmvc配置文件中
<!--郵件配置-->
<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="[email protected]"/>
<property name="subject" value="[XXXX項目]請您激活賬戶!"/>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com"/>
<property name="port" value="25"/>
<property name="username" value="[email protected]"/>
<property name="password" value="*****"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
3 . 實現類
@Service
public class MailServiceImpl implements MailService{
@Resource
private SimpleMailMessage simpleMailMessage;
@Resource
private MailSender mailSender;
@Override
public void sendMail(String mailTo, String activationCode) {
simpleMailMessage.setTo(mailTo);
simpleMailMessage.setText("請點擊激活碼 : http://192.168.1.99/auth/api/doactivate?mail="+mailTo+"&activationCode="+activationCode);
mailSender.send(simpleMailMessage);
}
}