Controller
/**
* 发送邮箱邮件
*
* @param email
* @return
*/
@RequireLogin
@RequestMapping("sendEmail")
@ResponseBody
public JSONResult sendEmail(String email) {
JSONResult json = new JSONResult();
try {
this.userinfoService.sendVerifyEmail(email);
} catch (RuntimeException re) {
json.setSuccess(false);
json.setMsg(re.getMessage());
}
return json;
}
ServiceImpl
import com.zl.bean.Mailverify;
import com.zl.bean.Userinfo;
import com.zl.bean.util.UserContext;
import com.zl.dao.MailVerifyDao;
import com.zl.dao.UserinfoDao;
import com.zl.service.IMailService;
import com.zl.service.IVerifyCodeService;
import com.zl.service.UserinfoService;
import com.zl.util.BidConst;
import com.zl.util.BitStatesUtils;
import com.zl.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.UUID;
@Service
public class UserinfoServiceImpl implements UserinfoService {
public void sendVerifyEmail(String email) {
// 当前用户没有绑定邮箱
Userinfo current = this.getCurrent();
if (!current.getIsBindEmail()) {
String uuid = UUID.randomUUID().toString();
// 构造一份要发送的邮件
StringBuilder content = new StringBuilder(100)
.append("点击<a href='")
.append(this.hostUrl)
.append("bindEmail.do?key=").append(uuid)
.append("'>http://localhost:18081/bindEmail.do</a>完成邮箱绑定,有效期为")
.append(BidConst.VERIFYEMAIL_VAILDATE_DAY).append("天");
try {http://localhost:18081/personal.do
// System.out.println("发送邮件:" + email + "邮件内容:" + content);
mailService.sendMail(email, "邮箱验证邮件", content.toString());
// 构造一个MailVerify对象;
Mailverify mv = new Mailverify();
mv.setEmail(email);
mv.setSendDate(new Date());
mv.setUserinfoId(current.getId());
mv.setUuid(uuid);
this.mailVerifyDao.insert(mv);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("验证邮件发送失败!");
}
}
}
MailServiceImpl
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import com.zl.service.IMailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class MailServiceImpl implements IMailService {
// @Value("${mail.username}")
//输入你的邮箱账号
private static String username="";
// @Value("${mail.password}")
//输入你的邮箱密码,注意如果使用网易邮箱或者QQ邮箱这里应该输邮箱授权码,授权码可自行百度
private static String password="";
//授权码对应的SMTP服务器地址
public static String myEmailSMTPHost = "smtp.163.com";
@Override
public void sendMail(String target, String title, String content) {
try {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
// 设置SMTP服务器地址
sender.setHost(myEmailSMTPHost);
// 创建好一个邮件对象
MimeMessage message = sender.createMimeMessage();
// 创建一个邮件助手
MimeMessageHelper helper = new MimeMessageHelper(message,"UTF-8");
// 通过heloper设置邮件相关内容
// 设置目标
helper.setTo(target);
// 设置from
// helper.setFrom("Admin@xmg.com"); // 这个地方一定要和properties中的username一样
// 这里有两个参数,第一个form是用来规定发件人的邮箱地址的,第二个是发件人的名字,可以自定义
helper.setFrom("输入你的邮箱账号", "系统管理员");
// 设置邮件标题
helper.setSubject(title);
// 设置邮件内容
helper.setText(content, true);
// 设置发送邮件的账号和密码
sender.setUsername(username);
sender.setPassword(password);
//
Properties prop = new Properties();
prop.put("mail.smtp.auth", "true");// 设置发送邮件需要身份认证
prop.put("mail.smtp.timeout", "25000");// 设置发送超时时间
prop.setProperty("mail.smtp.host", myEmailSMTPHost);
sender.setJavaMailProperties(prop);
// 发送邮件
sender.send(message);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("发送邮件失败!");
}
}
}