Insurance 项目——邮件验证
这是一个java初学者在独立开发一个项目时做的笔记,很多内容仅仅是为了解决当前需求,并未很深入的研究。
xml文件和properties文件在:建立一个SSM-Maven项目——整合SSM
在这个项目中,这个功能主要是作用于找回密码功能。相对的,可以拓展注册验证,群发Email等功能。
1、所需的jar包。
javax.mail
javax.activation 不确定是否需要
2、XML文件。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!--設置發件人屬性 --> 2 <bean id="javaMailSender" 3 class="org.springframework.mail.javamail.JavaMailSenderImpl"> 4 <!-- SMTP发送邮件的服务器的IP和端口 --> 5 <property name="host" value="${email.host}" /> 6 <property name="port" value="${email.port}" /> 7 8 <!-- 登陆SMTP邮件发送服务器的用户名和密码 --> 9 <property name="username" value="${email.username}" /> 10 <property name="password" value="${email.password}" /> 11 12 <!-- 获得邮件会话属性,验证登录邮件服务器是否成功--> 13 <property name="javaMailProperties"> 14 <props> 15 <prop key="mail.transport.protocol">smtp</prop> 16 <prop key="mail.smtp.auth">true</prop> 17 <prop key="mail.smtp.starttls.enable">true</prop> 18 </props> 19 </property> 20 </bean> 21 22 <!-- 發送郵件 --> 23 <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"> 24 <property name="from" value="${mail.from}" /> 25 <property name="subject" value="${mail.subject}" /> 26 <property name="to" value="${mail.to}" /> 27 <property name="text" value="${mail.text}" /> 28 29 30 </bean>
记得引入配置文件,若你不使用配置文件做出对应的修改。
3、发送邮件代码。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.util.Date; 2 3 import javax.annotation.Resource; 4 import javax.mail.MessagingException; 5 import javax.mail.internet.MimeMessage; 6 7 import org.springframework.mail.SimpleMailMessage; 8 import org.springframework.mail.javamail.JavaMailSender; 9 import org.springframework.mail.javamail.MimeMessageHelper; 10 import org.springframework.stereotype.Service; 11 12 import com.ic.service.EmailService; 13 14 @Service("mail") 15 public class Mailservice implements EmailService { 16 @Resource 17 private JavaMailSender javaMailSender; 18 @Resource 19 private SimpleMailMessage simpleMailMessage; 20 21 @Override 22 public int sendEmail(String toEmails, String subject, String content) { 23 MimeMessage message = this.javaMailSender.createMimeMessage(); 24 try 25 { 26 MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8"); 27 28 messageHelper.setTo(toEmails); 29 if (subject != null) { 30 messageHelper.setSubject(subject); 31 } else { 32 messageHelper.setSubject(this.simpleMailMessage.getSubject()); 33 } 34 messageHelper.setText(content, true); 35 36 messageHelper.setSentDate(new Date()); 37 messageHelper.setFrom("xxxxx@gmail.com"); 38 } 39 catch (MessagingException e) 40 { 41 e.printStackTrace(); 42 } 43 this.javaMailSender.send(message); 44 return 200; 45 } 46 47 48 }
4、调用mailservice。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //忘记密码 2 public String forgetpw(String email) { 3 //生成密匙 4 String secretKey= (int)(Math.floor(Math.random()*899999 + 100000))+""; 5 //生成失效事件,这里是30分钟 6 long outDate = new Date(System.currentTimeMillis()+30*60*1000).getTime(); 7 //把生成的密匙和失效时间存入对象 8 SP sp = new SP(); 9 sp.setSecretkey(secretKey); 10 sp.setOutdate(outDate); 11 //把拥有密匙和失效时间的对象存入数据库 12 int f = adminService.updateSO(sp); 13 if(f == 1) { 14 String toEmails = email; 15 String subject = "Is your verification code"; 16 String content = "verification code:"+secretKey; 17 int code = emailService.sendEmail(toEmails,subject,content); 18 19 } 20 }
到这里发送邮件就完成了。
别忘记开启你邮件的第三方权限,各个邮箱不同。
5、验证。
这里代码就不写了,思路如下
查询数据库得到密匙和失效时间(outDate)
首先 if( new Date().getTime() <outDate。)
满足条件,对比密匙。
对比成功进行修改密码。
这里完整的验证功能就实现了