Java使用邮箱发送验证码
添加依赖
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
创建CptchaUtil
import java.util.Random;
public class CaptchaUtil {
public static String creatCode(int n) {
String code = "";
Random r = new Random();
for (int i = 0; i < n; i++) {
int type = r.nextInt(3);
switch (type) {
case 0:
char ch = (char) (r.nextInt(26) + 65);
code += ch;
break;
case 1:
char ch1 = (char) (r.nextInt(26) + 97);
code += ch1;
break;
case 2:
code += r.nextInt(10);
break;
}
}
return code;
}
}
创建JavaMailUntil工具类
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@Slf4j
public final class JavaMailUntil {
private static final String emailProtocol = "smtp";
private static final String emailSMTPHost = "smtp.exmail.qq.com";
private static final String emailPort = "465";
private static final String emailAccount = "xxxxx@xxxxx.com";
private static final String emailPassword = "***********";
public static boolean sendEmail(Set<String> emails, String title, String content) {
if (emails == null || emails.isEmpty()) return false;
try {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", emailProtocol);
props.setProperty("mail.smtp.host", emailSMTPHost);
props.setProperty("mail.smtp.port", emailPort);
props.setProperty("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.connectiontimeout", "10000");
props.setProperty("mail.smtp.timeout", "10000");
props.setProperty("mail.smtp.writetimeout", "10000");
Session session = Session.getDefaultInstance(props);
session.setDebug(false);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailAccount, "小狐狸", "UTF-8"));
int size = emails.size();
if (size == 1) {
String email = emails.iterator().next();
message.setRecipient(Message.RecipientType.TO, new InternetAddress(email, email, "UTF-8"));
} else {
InternetAddress[] addresses = new InternetAddress[emails.size()];
int i = 0;
for (String email : emails) {
addresses[i++] = new InternetAddress(email, email, "UTF-8");
}
message.setRecipients(MimeMessage.RecipientType.TO, addresses);
}
message.setSubject(title, "UTF-8");
message.setContent(content, "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
Transport transport = session.getTransport();
transport.connect(emailAccount, emailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
} catch (Exception e) {
log.error("发送邮件失败,系统错误!", e);
return false;
}
}
}
测试
String code = CaptchaUtil.creatCode(6);
Set<String> set = new HashSet<>();
set.add("xxxx@xxx.com");
boolean isMail = JavaMailUntil.sendEmail(set, "验证码", "您的验证码为:" + code+"5分钟有效请尽快使用。");