滑动验证 及Java 发送邮箱及激活

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>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值