1. 发送邮件生成验证码的类
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
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 java.util.Date;
import java.util.Properties;
import java.util.UUID;
public class mailRegister {
//生成随机验证码
publicstatic String[] chars = new String[] { "a", "b","c", "d", "e", "f",
"g", "h", "i", "j","k", "l", "m", "n", "o","p", "q", "r", "s",
"t", "u", "v", "w","x", "y", "z", "0", "1","2", "3", "4", "5",
"6", "7", "8", "9","A", "B", "C", "D", "E","F", "G", "H", "I",
"J", "K", "L", "M","N", "O", "P", "Q", "R","S", "T", "U", "V",
"W", "X", "Y", "Z" };
//发件人的 邮箱 和 密码(替换为自己的邮箱和密码)
//PS: 某些邮箱服务器为了增加邮箱本身密码的安全性,给 SMTP 客户端设置了独立密码(有的邮箱称为“授权码”),
//对于开启了独立密码的邮箱, 这里的邮箱密码必需使用这个独立密码(授权码)。
publicstatic String myEmailAccount = "XXXXXXXX@163.com";
publicstatic String myEmailPassword = "XXXXXXXX";
//发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为:smtp.xxx.com
//网易163邮箱的 SMTP 服务器地址为: smtp.163.com
publicstatic String myEmailSMTPHost = "smtp.163.com";
publicboolean SendMail(String receiveMailAccount) throws Exception {
//1. 创建参数配置, 用于连接邮件服务器的参数配置
Propertiesprops = new Properties(); // 参数配置
props.setProperty("mail.transport.protocol","smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host",myEmailSMTPHost); // 发件人的邮箱的 SMTP
//服务器地址
props.setProperty("mail.smtp.auth","true"); // 需要请求认证
//2. 根据配置创建会话对象, 用于和邮件服务器交互
Sessionsession = Session.getDefaultInstance(props);
session.setDebug(true);// 设置为debug模式, 可以查看详细的发送 log
//3. 创建一封邮件
/**
* 创建一封只包含文本的简单邮件
*
* @param session
* 和服务器交互的会话
* @param sendMail
* 发件人邮箱
* @param receiveMail
* 收件人邮箱
* @return
* @throws Exception
*/
// 创建一封邮件
MimeMessagemessage = new MimeMessage(session);
// From: 发件人
message.setFrom(newInternetAddress(myEmailAccount, "中文测试","UTF-8"));
// To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receiveMailAccount, "亲爱的用户","UTF-8"));
// Subject: 邮件主题
message.setSubject("验证码", "UTF-8");
// Content: 邮件正文(可以使用html标签)
StringRandomCode=generateShortUuid();
this.setMessageContent(message,"",RandomCode);
message.setSentDate(newDate());
// 保存设置
message.saveChanges();
//4. 根据 Session 获取邮件传输对象
Transporttransport = session.getTransport();
//5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
transport.connect(myEmailAccount,myEmailPassword);
//6. 发送邮件, 发到所有的收件地址,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人,
//抄送人, 密送人
transport.sendMessage(message,message.getAllRecipients());
//8. 关闭连接
transport.close();
returntrue;
}
/*
@Description:生成八位验证码
*/
publicstatic String generateShortUuid() {
StringBuffer shortBuffer = newStringBuffer();
String uuid =UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i *4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x %0x3E]);
}
return shortBuffer.toString();
}
/*
@解决邮件正文乱码问题
*/
publicvoid setMessageContent(Message message, String ContentType,String RandomCode)throws MessagingException {
StringContent="尊敬的用户,您好!您的验证码是: "+RandomCode;
MimeMultipartmmt = new MimeMultipart();
MimeBodyPartmbp = new MimeBodyPart();
if(ContentType == null || ContentType.equals("")) // ContentType为编码类型,如GBK等
mbp.setText(Content);// 由JavaMail来决定编码
else
mbp.setContent(Content,ContentType); // 指定编码格式
mmt.addBodyPart(mbp);
message.setContent(mmt);
}
}
2. 测试类
publicclass TestMail {
public static void main(String args[]){
//收件人信息
StringreceiveMailAccount="XXXXXXXX@qq.com";
mailRegister mr=new mailRegister();
try {
System.out.println(mr.SendMail(receiveMailAccount));
} catch (Exception e) {
// TODO Auto-generated catchblock
e.printStackTrace();
}
}
}