Spring开发发送邮件验证激活:
spring.xml
true true
EmailController.java
package org.jun.controller.email;import java.io.File;import java.util.Date;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;/** * 发送邮件 * * @author xiejunbo * */@Controller@RequestMapping("email")public class EmailController { @Autowired private JavaMailSender mailSender; @Autowired private SimpleMailMessage simpleMsg; /** * 带附件和链接的邮件 * @throws MessagingException */ @ResponseBody @RequestMapping("sendMime") public String sendMime() throws MessagingException{ MimeMessage msg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg,true); helper.setFrom("624664181@qq.com"); helper.setTo("xiejunbo@xx.con"); helper.setText("您刚刚在xx科技公司注册了账号,注册邮箱:447546225@qq.com,请点击以面链接进行验证验证链接
" + " ", true); helper.setSubject("xx科技公司账号激活验证链接"); helper.setSentDate(new Date()); FileSystemResource file = new FileSystemResource(new File("C:\\Users\\Administrator\\Desktop\\jun.jpg")); helper.addAttachment("jun.jpg", file); mailSender.send(msg); return "send successfully"; } /** * 纯文本邮件 */ @ResponseBody @RequestMapping("sendText") public String sendText() { simpleMsg.setText("测试spring发送文本邮件");//邮件内容 // msg.setBcc("");//密送人,可以是一个数组 // msg.setCc("");//抄送人,可以是一个数组 simpleMsg.setSentDate(new Date());//发送时间 simpleMsg.setSubject("这是发送主题"); simpleMsg.setTo("4sdfasdf@qq.com");//接收方,可以是一个数组 mailSender.send(simpleMsg); return "send successfully"; } /** * 邮件激活验证 * * @return */ @ResponseBody @RequestMapping("checkEmail") public String checkEmail(@RequestParam String username, @RequestParam String pwd){ if("xiejunbo".equals(username) && "123456".equals(pwd)){ System.out.println("账号激活成功!"); return "Verify account successfully!"; } System.out.println("账号激活失败!请重新激活!"); return "Verify account fail! Please try again!"; } }
mail.properties配置:
#mail sendermail.host=smtp.qq.commail.from=aaaaaa@qq.commail.user=aaaaaa@qq.commail.pwd=xxxxxxxxxxxxxxx