package com.qs.pojo;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
- 发件人账号密码
- @author zhangdi
*/
public class MailAuthenticator extends Authenticator{
public static String USERNAME = "";
public static String PASSWORD = "";
public MailAuthenticator() {
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
}
package com.qs.pojo;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.junit.Test;
/**
- 邮件发送操作类
- @author zhangdi
*/
public class MailOperation {
/**
* 发送邮件
*
* @param user
* 发件人邮箱
* @param password
* 授权码(注意不是邮箱登录密码)
* @param host
* @param from
* 发件人
* @param to
* 接收者邮箱
* @param subject
* 邮件主题
* @param content
* 邮件内容
* @return success 发送成功 failure 发送失败
* @throws Exception
*/
public String sendMail(String user, String password, String host, String from, String to, String subject,
String content) throws Exception {
if (to != null) {
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
MailAuthenticator auth = new MailAuthenticator();
MailAuthenticator.USERNAME = user;
MailAuthenticator.PASSWORD = password;
Session session = Session.getInstance(props, auth);
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
if (!to.trim().equals(""))
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to.trim()));
message.setSubject(subject);
MimeBodyPart mbp1 = new MimeBodyPart(); // 正文
mbp1.setContent(content, "text/html;charset=utf-8");
Multipart mp = new MimeMultipart(); // 整个邮件:正文+附件
mp.addBodyPart(mbp1);
// mp.addBodyPart(mbp2);
message.setContent(mp);
message.setSentDate(new Date());
message.saveChanges();
Transport trans = session.getTransport("smtp");
trans.send(message);
System.out.println(message.toString());
} catch (Exception e) {
e.printStackTrace();
return "failure";
}
return "success";
} else {
return "failure";
}
}
public static void main(String[] args) {
}
// @Test
public void textDemo(String emailAddress,String code){
MailOperation operation = new MailOperation();
String user = “邮箱地址”;
String password = “注意这里是授权码不是密码”;
String host = “smtp.163.com”;
String from = “邮箱地址”;
String to = 收件人的邮箱地址;// 收件人
String subject = “帅哥学堂”;
// 邮箱内容
StringBuffer sb = new StringBuffer();
ThreadLocalRandom random = ThreadLocalRandom.current();
int nextInt = random.nextInt(100000, 200000);
String yzm = nextInt+"";
sb.append("发送的内容");
try {
String res = operation.sendMail(user, password, host, from, to, subject, sb.toString());
System.out.println(res);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
需要的jar包坐标
controller 层的代码
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qs.pojo.MailOperation;
import com.qs.pojo.User;
import com.qs.service.ProductService;
import com.qs.utils.ImageUtil;
import com.qs.utils.UUidutils;
@Controller
public class DemoController {
@Autowired
private ProductService service;
@RequestMapping(value = "demo2")
public String demo2() {
return "demo2";
}
@RequestMapping(value = "code")
public String getCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("image/jpeg");
// 禁止图像缓存
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession session = request.getSession();
ImageUtil imageUtil = new ImageUtil(120, 30, 5, 30);
session.setAttribute("code", imageUtil.getCode());
imageUtil.write(response.getOutputStream());
return null;
}
@RequestMapping(value = "cedeY")
@ResponseBody
public String cedeY(String text, HttpSession sess) {
String img = (String) sess.getAttribute("code");
if (text.equalsIgnoreCase(img)) {
return "1";
} else {
return "0";
}
}
/*@RequestMapping(value = "emailDemo", produces = "text/plain;charset=utf-8")
@ResponseBody
public String emailDemo(String email1) {
String code = UUidutils.getId();
MailOperation operation = new MailOperation();
operation.textDemo(email1, code);
return "{\"msg\":\"发送成功!\"}";
}*/
@RequestMapping(value="activation")
public String success(String code) {
boolean result = service.queryUserByUid(code);
if(result)return "success";
return "Erro";
}
}
jsp页面
<form id="addForm" action="">
<table border="0" align="center">
<tr>
<td align="center">姓名:<input id="name" type="text" name="username">
</td>
</tr>
<tr>
<td align="center">密码:<input id="password" type="text" name="password">
</td>
</tr>
<tr>
<td>请输入邮箱:<input id="email" name="email" type="email">
</td>
</tr>
<tr>
<td>
<div id="slider2" class="slider"></div>
</td>
</tr>
<tr>
<td align="center">
<button id="zhuce" style="width: 100px" type="button"
disabled="disabled">注册</button>
<button id="reset1" style="width: 100px">还原</button>
</td>
</tr>
</table>
</form>