发送邮箱验证码(带html格式)

发布时间:2025-05-30 点击:8
场景:用于用户注册、密码重置等。
1 . 邮件html模板 emailtemplet.html
<!doctype html> <html> <head> <meta http-equiv=”content-type” content=”text/html; charset=utf-8″ /> <title></title> <meta charset=”utf-8″ /> </head> <body> <div class=”qmbox qm_con_body_content qqmail_webmail_only” id=”mailcontentcontainer” style=””> <style type=”text/css”> .qmbox body { margin: 0; padding: 0; background: #fff; font-family: “verdana, arial, helvetica, sans-serif”; font-size: 14px; line-height: 24px; } .qmbox div, .qmbox p, .qmbox span, .qmbox img { margin: 0; padding: 0; } .qmbox img { border: none; } .qmbox .contaner { margin: 0 auto; } .qmbox .title { margin: 0 auto; background: url() #ccc repeat-x; height: 30px; text-align: center; font-weight: bold; padding-top: 12px; font-size: 16px; } .qmbox .content { margin: 4px; } .qmbox .biaoti { padding: 6px; color: #000; } .qmbox .xtop, .qmbox .xbottom { display: block; font-size: 1px; } .qmbox .xb1, .qmbox .xb2, .qmbox .xb3, .qmbox .xb4 { display: block; overflow: hidden; } .qmbox .xb1, .qmbox .xb2, .qmbox .xb3 { height: 1px; } .qmbox .xb2, .qmbox .xb3, .qmbox .xb4 { border-left: 1px solid #bcbcbc; border-right: 1px solid #bcbcbc; } .qmbox .xb1 { margin: 0 5px; background: #bcbcbc; } .qmbox .xb2 { margin: 0 3px; border-width: 0 2px; } .qmbox .xb3 { margin: 0 2px; } .qmbox .xb4 { height: 2px; margin: 0 1px; } .qmbox .xboxcontent { display: block; border: 0 solid #bcbcbc; border-width: 0 1px; } .qmbox .line { margin-top: 6px; border-top: 1px dashed #b9b9b9; padding: 4px; } .qmbox .neirong { padding: 6px; color: #666666; } .qmbox .foot { padding: 6px; color: #777; } .qmbox .font_darkblue { color: #006699; font-weight: bold; } .qmbox .font_lightblue { color: #008bd1; font-weight: bold; } .qmbox .font_gray { color: #888; font-size: 12px; } </style> <div class=”contaner”> <div class=”title”>$(title)</div> <div class=”content”> <p class=”biaoti”><b>亲爱的用户,你好!</b></p> <b class=”xtop”><b class=”xb1″></b><b class=”xb2″></b><b class=”xb3″></b><b class=”xb4″></b></b> <div class=”xboxcontent”> <div class=”neirong”> <p><b>请核对你的用户名:</b><span id=”username” class=”font_darkblue”>$(username)</span></p> <p><b>$(type)的验证码:</b><span class=”font_lightblue”><span id=”yzm” data=”$(captcha)” onclick=”return false;” t=”7″ style=”border-bottom: 1px dashed rgb(204, 204, 204); z-index: 1; position: static;”>$(captcha)</span></span><br><span class=”font_gray”>(请输入该验证码完成$(type),验证码30分钟内有效!)</span></p> <div class=”line”>如果你未申请$(type)服务,请忽略该邮件。</div> </div> </div> <b class=”xbottom”><b class=”xb4″></b><b class=”xb3″></b><b class=”xb2″></b><b class=”xb1″></b></b> <p class=”foot”>如果仍有问题,请拨打我们的会员服务专线: <span data=”800-820-5100″ onclick=”return false;” t=”7″ style=”border-bottom: 1px dashed rgb(204, 204, 204); z-index: 1; position: static;”>021-51875288 </span></p> </div> </div> <style type=”text/css”> .qmbox style, .qmbox script, .qmbox head, .qmbox link, .qmbox meta { display: none !important; } </style> </div> </body> </html>
2 . 验证码生成工具类 captchautils.java
import java.util.random; public class captchautils { / * description:生成验证码 * @return * @return string * @author name:yuruixin <br>email: ruixin_yu@asdc.com.cn / public static string creatcaptcha(){ string captcha = captcha(6); return captcha; } / * description:生成随机数 * @param charcount * @return * @return string * @author name:yuruixin <br>email: ruixin_yu@asdc.com.cn / public static string captcha(int charcount){ string charvalue = “”; for (int i = 0; i < charcount; i ) { char c = (char) (randomint(0, 10) \\\’0\\\’); charvalue = string.valueof(c); } return charvalue; } public static int randomint(int from, int to) { random r = new random(); return from r.nextint(to – from); } }
3 . 邮件详情实体类 emailmailinfo.java
/ * classname: emailmailinfo.java <br> * description: <br> * create by: name:yuxin <br>email: yuruixin_china@163.com <br> * create time: 2017年6月15日<br> */ public class emailmailinfo { //邮箱服务器 如smtp.163.com private string host ; //用户邮箱 如@163 private string formname ; //用户授权码 不是用户名密码 可以自行查看相关邮件服务器怎么查看 private string formpassword ; //消息回复邮箱 private string replayaddress ; //发送地址 private string toaddress ; //发送主题 private string subject ; //发送内容 private string content ; public string gethost() { return host; } public void sethost(string host) { this.host = host; } public string getformname() { return formname; } public void setformname(string formname) { this.formname = formname; } public string getformpassword() { return formpassword; } public void setformpassword(string formpassword) { this.formpassword = formpassword; } public string getreplayaddress() { return replayaddress; } public void setreplayaddress(string replayaddress) { this.replayaddress = replayaddress; } public string gettoaddress() { return toaddress; } public void settoaddress(string toaddress) { this.toaddress = toaddress; } public string getsubject() { return subject; } public void setsubject(string subject) { this.subject = subject; } public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } }
4 . email模板加载与获取
/ * classname: emailtemplet.java <br> * description: <br> * create by: name:yuxin <br>email: yuruixin_china@163.com <br> * create time: 2017年6月15日<br> */ public class emailtemplet { public static string gethtml(string title,string username,string type,string captcha) { string emailtemplet = system.getproperty(“emailtemplet”); emailtemplet = emailtemplet.replace(“$(title)”, title); emailtemplet = emailtemplet.replace(“$(username)”, username); emailtemplet = emailtemplet.replace(“$(type)”, type); emailtemplet = emailtemplet.replace(“$(captcha)”, captcha); return emailtemplet; } / * description:容器初始化是加载 * @return void * @author name:yuxin <br>email: yuruixin_china@163.com / public static void initemailtemplet() { string url = hospitalutils.class.getresource(“/emailtemplet.html”) .getfile(); url = url.substring(1, url.length()); try { string encoding = “utf-8”; file file = new file(url); if (file.isfile() && file.exists()) { // 判断文件是否存在 inputstreamreader read = new inputstreamreader( new fileinputstream(file), encoding);// 考虑到编码格式 bufferedreader bufferedreader = new bufferedreader(read); string linetxt = null; stringbuilder sb = new stringbuilder(); while ((linetxt = bufferedreader.readline()) != null) { system.out.println(linetxt); sb.append(linetxt); } system.setproperty(“emailtemplet”, sb.tostring()); read.close(); } else { system.out.println(“找不到指定的文件”); } } catch (exception e) { system.out.println(“读取文件内容出错”); e.printstacktrace(); } } }
5 . 邮件发送工具类 emailsendutil.java
import java.util.date; import java.util.properties; import javax.mail.authenticator; import javax.mail.bodypart; import javax.mail.message; import javax.mail.multipart; import javax.mail.passwordauthentication; 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; public class emailsendutil { private final static string host = “smtp.exmail.qq.com”; //163的服务器 private final static string formname = “support@allin-hr.com”;//你的邮箱 private final static string password = “”; //授权码 private final static string replayaddress = “support@allin-hr.com”; //你的邮箱 public static void sendhtmlmail(emailmailinfo info)throws exception{ info.sethost(host); info.setformname(formname); info.setformpassword(password); //网易邮箱的授权码~不一定是密码 info.setreplayaddress(replayaddress); message message = getmessage(info); // minimultipart类是一个容器类,包含mimebodypart类型的对象 multipart mainpart = new mimemultipart(); // 创建一个包含html内容的mimebodypart bodypart html = new mimebodypart(); // 设置html内容 html.setcontent(info.getcontent(), “text/html; charset=utf-8”); mainpart.addbodypart(html); // 将minimultipart对象设置为邮件内容 message.setcontent(mainpart); transport.send(message); } public static void sendtextmail(emailmailinfo info) throws exception { info.sethost(host); info.setformname(formname); info.setformpassword(password); //网易邮箱的授权码~不一定是密码 info.setreplayaddress(replayaddress); message message = getmessage(info); //消息发送的内容 message.settext(info.getcontent()); transport.send(message); } private static message getmessage(emailmailinfo info) throws exception{ final properties p = system.getproperties() ; p.setproperty(“mail.smtp.host”, info.gethost()); p.setproperty(“mail.smtp.auth”, “true”); p.setproperty(“mail.smtp.user”, info.getformname()); p.setproperty(“mail.smtp.pass”, info.getformpassword()); // 根据邮件会话属性和密码验证器构造一个发送邮件的session session session = session.getinstance(p, new authenticator(){ protected passwordauthentication getpasswordauthentication() { return new passwordauthentication(p.getproperty(“mail.smtp.user”),p.getproperty(“mail.smtp.pass”)); } }); session.setdebug(true); message message = new mimemessage(session); //消息发送的主题 message.setsubject(info.getsubject()); //接受消息的人 message.setreplyto(internetaddress.parse(info.getreplayaddress())); //消息的发送者 message.setfrom(new internetaddress(p.getproperty(“mail.smtp.user”),”神鲸系统”)); // 创建邮件的接收者地址,并设置到邮件消息中 message.setrecipient(message.recipienttype.to,new internetaddress(info.gettoaddress())); // 消息发送的时间 message.setsentdate(new date()); return message ; } }
6 . 验证码发送邮件工具类 emailutil.java
import javax.servlet.http.httpsession; / * classname: emailutil.java <br> * description: <br> * create by: name:yuxin <br>email: yuruixin_china@163.com <br> * create time: 2017年6月15日<br> */ public class emailutil { / * description:发送注册邮件 * @param email * @param session * @return * @return string * @author name:yuxin <br>email: yuruixin_china@163.com / public static string sendforreg(string email,httpsession session) { string captcha = captchautils.creatcaptcha(); session.setattribute(“reg:” email, captcha); string title = “用户注册”; string content = emailtemplet.gethtml(title, email, title, captcha); emailmailinfo info = new emailmailinfo(); info.settoaddress(email); info.setsubject(title); info.setcontent(content); try { // mailsendutil.sendtextmail(info); emailsendutil.sendhtmlmail(info); } catch (exception e) { system.out.print(“\\\'” title “\\\’的邮件发送失败!”); e.printstacktrace(); } return captcha; } / * description:发送密码重置邮件 * @param email * @param session * @return * @return string * @author name:yuxin <br>email: yuruixin_china@163.com / public static string sendforresetpassword(string email,httpsession session) { string captcha = captchautils.creatcaptcha(); session.setattribute(“rest” email, captcha); string title = “密码重置”; string content = emailtemplet.gethtml(title, email, title, captcha); emailmailinfo info = new emailmailinfo(); info.settoaddress(email); info.setsubject(title); info.setcontent(content); try { // mailsendutil.sendtextmail(info); emailsendutil.sendhtmlmail(info); } catch (exception e) { system.out.print(“\\\'” title “\\\’的邮件发送失败!”); e.printstacktrace(); } return captcha; } / * description:校验验证码 * @param email * @param captcha * @param session * @return * @return boolean * @author name:yuxin <br>email: yuruixin_china@163.com / public static boolean checkcaptcha(string email,string type,string captcha,httpsession session){ string captchacache = null; switch (type) { case “reg”: captchacache = (string) session.getattribute(“reg:” email); break; case “restpassword”: captchacache = (string) session.getattribute(“rest” email); break; default: break; } if(captchacache != null){ return captchacache.equals(captcha); } return false; } }
如果,你对上面的内容还有疑问,推荐选择西部数码企业云邮箱!有专人协助您解答邮箱疑问。
西部数码是专业企业邮箱的官方正规提供商,21年行业经验,提供安全稳定,简单易用,高性价比的企业邮箱,按需自由定制,不限空间,极速收发,能够满足用户对企业邮箱的不同需求。可以通过以下几种方式注册、申请、购买、试用、开通企业邮箱:
1、登录https://www.west.cn/services/mail/在线咨询申请试用或购买;


彻底解决网站存在的内容重复度的方法
酷我音乐缓存文件在哪?打开酷我音乐缓存文件的方法
腾讯云服务器优惠信息
网站收录量下降什么原因?怎么提高网站收录量?
一个云服务器公司要多少钱一个月
想自己买阿里云服务器
为什么要做域名分析?小白怎么玩转域名投资?
香港主机哪家好