手机号发验证码太贵了,邮箱无论发多少条都是免费的,接上篇文章,在接口类上增加了一个发送邮箱验证码的的接口,后期我还会继续完善
引入Maven
1 2 3 4 5
| <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.5.0-b01</version> </dependency>
|
代码
邮箱账号这里还以搜狐邮箱为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| package com.lzc.mail.utlis;
import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.io.UnsupportedEncodingException; import java.util.Properties;
public class MailUtli { private final static String userName = "xxxxx@sohu.com"; private final static String password = "xxxxxx"; private final static String host = "smtp.sohu.com"; private final static String port = "465";
private final static String name = "小李同志"; private final static Properties properties = new Properties();
static { properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", port); properties.put("mail.smtp.auth", true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
}
private static Message getMessage(String to, String subject) { Session session = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(userName, password); } });
Message message = new MimeMessage(session); try { InternetAddress form = new InternetAddress(userName, name); message.setFrom(form); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); } catch (UnsupportedEncodingException | MessagingException e) { throw new RuntimeException(e); } return message; }
public static void sendAMessage(String to, String subject, String body) { Message message = getMessage(to, subject); try { message.setText(body); Transport.send(message); } catch (MessagingException e) { throw new RuntimeException(e); }
}
public static void sendAVerificationCode(String to, String subject, String code, String time) { Message message = getMessage(to, subject);
Multipart multipart = new MimeMultipart();
String html = "<div style=\"max-width:600px;margin:0 auto;background-color:#fff;padding:30px;border-radius:8px;box-shadow:0 4px 8px rgba(0,0,0,0.1);\">" + " <div style=\"text-align:center;margin-bottom:20px;\">" + " <a href=\"https://xlzcx.top\" style=\"border-radius:50%;overflow:hidden;display:inline-block;height:100px;\">" + " <img src=\"cid:logo\" alt=\"小李同志\" style=\"width:100px;\">" + " </a>" + " </div>" + " <h1 style=\"color:#333;font-size:18px;\">尊敬的用户:</h1>" + " <p>" + " 您的验证码为<span style=\"font-weight:bold;color:#007bff;\">"+code+"</span>" + " ,验证码将在" + " <span style=\"font-weight:bold;color:#007bff;\">"+time+"</span>" + " 后失效,如果此验证码不是您本人申请,请无视此邮件,切勿与他人分享。" + " </p>" + " <p>此邮箱为系统自动发出,请不要直接回复,如有问题请联系QQ:xxxxxxxxx,谢谢!</p>" + "</div>"; try { BodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(html, "text/html;charset=utf-8"); multipart.addBodyPart(htmlPart);
MimeBodyPart imagePart = new MimeBodyPart(); FileDataSource fds = new FileDataSource("D:\\os\\Desktop\\avatar.jpg"); imagePart.setDataHandler(new DataHandler(fds)); imagePart.setHeader("Content-ID", "<logo>");
multipart.addBodyPart(imagePart);
message.setContent(multipart); Transport.send(message); } catch (MessagingException e) { throw new RuntimeException(e); } }
}
|