1:需要mail.jar包,这个可以去下载
2:java类
(1)重要文件配置(发短信方的账号密码和host等)
public class ParamConfig {
//发邮件
public final static String EMAIL_FORM="[email protected]";
public final static String EMAIL_HOST="abc.qq.com";
public final static String EMAIL_USERNAME="[email protected]";
public final static String EMAIL_PWD="zdx123";
}
(2)发短信的工具类
public class MailUtil {
/**
* 发送html邮件
*
* @author geloin
* @date 2012-5-8 上午11:38:44
* @param toEmail
* @param subject
* @param htmlContent
*/
public static void sendMail(String toEmail, String subject,
String htmlContent) {
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
// 发送邮箱的邮件服务器
senderImpl.setHost(ParamConfig.EMAIL_HOST);
// 建立邮件消息,发送简单邮件和html邮件的区别
MimeMessage mailMessage = senderImpl.createMimeMessage();
// 为防止乱码,添加编码集设置
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
"UTF-8");
try {
// 接收方邮箱
messageHelper.setTo(toEmail);
} catch (MessagingException e) {
throw new RuntimeException("收件人邮箱地址出错!");
}
try {
// 发送方邮箱
messageHelper.setFrom(ParamConfig.EMAIL_FORM);
} catch (MessagingException e) {
throw new RuntimeException("发件人邮箱地址出错!");
}
try {
messageHelper.setSubject(subject);
} catch (MessagingException e) {
throw new RuntimeException("邮件主题出错!");
}
try {
// true 表示启动HTML格式的邮件
messageHelper.setText(htmlContent, true);
} catch (MessagingException e) {
throw new RuntimeException("邮件内容出错!");
}
Properties prop = new Properties();
// 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
prop.put("mail.smtp.auth", "true");
// 超时时间
prop.put("mail.smtp.timeout", "25000");
// 添加验证
MyAuthenticator auth = new MyAuthenticator(ParamConfig.EMAIL_USERNAME,
ParamConfig.EMAIL_PWD);
Session session = Session.getDefaultInstance(prop, auth);
senderImpl.setSession(session);
// senderImpl.setJavaMailProperties(prop);
// 发送邮件
senderImpl.send(mailMessage);
}
}
(3)额外的验证合法性的工具类
/**
* 验证合法性
* @author Administrator
*
*/
public class MyAuthenticator extends Authenticator {
private String username;
private String password;
/**
*
* @author panliang
* @param username
* @param password
*/
public MyAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
3:测试
/**
* 发送邮箱验证码
* @param request
* @param response
* @throws IOException
*/
@RequestMapping(value="/send_message.htm")
public void send_message(HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setContentType("text/plain; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
JSONObject jsonObj = new JSONObject();
String checkCode=request.getParameter("code");//验证码
String email=request.getParameter("phone");//邮箱
// 主题
String subject = "注册验证码";
StringBuilder builder = new StringBuilder();
builder.append("<html><head>");
builder.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
builder.append("</head><body>");
builder.append("尊敬的");
builder.append(email);
builder.append("<br /><br />");
builder.append("\t注册的验证码为:");
builder.append(checkCode);
builder.append("<br /><br />");
builder.append("------------------------------");
builder.append("<br /><br />");
builder.append("邮编:100039<br />电话:400-665-1987<br />传真:010-51727345<br />" +
"网址:www.daxinpj.com<br />公司地址:北京市海淀区中关村南大街2号 数码大厦A座");
builder.append("</body></html>");
String htmlContent = builder.toString();
final HttpSession session=request.getSession();
String code=(String) session.getAttribute("validateCode");
if(code==null){
code=checkCode;
}else{
code=code+checkCode;
}
session.setAttribute("validateCode", code);//把验证码放入session
MailUtil.sendMail(email, subject, htmlContent);//调用发送邮箱的方法,给用户发送邮箱
final Timer timer=new Timer();//定时器,验证码10分钟之内没有输入并注册就失效
timer.schedule(new TimerTask() {
public void run() {
session.removeAttribute("validateCode");
timer.cancel();
}
}, 1000*60*10);// 设定指定的时间time,此处为10分钟
PrintWriter out = response.getWriter();
jsonObj.put("statue", "SUCCESS");
out.println(jsonObj);
out.flush();
out.close();
}