Java实现发送邮箱验证码注册验证链接本⽂以163邮箱为例
1、准备(邮箱账号,邮箱必须设置POP3/SMTP/IMAP,设置步骤如下:)
》》步骤:1 开启授权码服务
》》步骤:2 ⼿机获取验证码
》》步骤:3 设置授权码
》》步骤:4 开启服务smtp/pop3/imap
》》步骤:5 开启成功,忘记授权码可重设
2、发送邮件的jar包(以下使⽤的为maven的pom坐标)
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
3、代码实现
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
感恩母亲节的话SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
//发送邮件
//发送邮件
public class test {
//配置信息
private static final String MAIL_TRANSPORT_PROTOCOL ="ansport.protocol";//邮件的传输协议
private static final String MAIL_TRANSPORT_PROTOCOL_VALUE ="smtp";//使⽤smtp协议
private static final String MAIL_HOST ="mail.host";//发送邮件的主机
private static final String MAIL_HOST_VALUE ="smtp.163"; //发送邮件的服务器地址
private static final String MAIL_DEBUG ="mail.debug"; //调试模式
private static final String MAIL_SMTP_AUTH ="mail.smtp.auth";//邮件smtp作者确认
private static final String CONFIRM ="true";//确认
private static final String SEND_ENCODING_LAYOUT ="text/html;charset=utf-8";//发送邮件的编码格式
//邮件编辑信息(仅需写上⾃⼰的)
private static final String MAIL_FROM =xxx@163";//邮件发送⼈
private static final String MAIL_FROM_PASSWORD ="xxxxx";//邮件发送⼈授权码
private static final String MAIL_SUBJECT_REGISTER ="xxxx【注册链接】";//邮件主题(注册链接)
private static final String MAIL_SUBJECT_VERIFY_CODE ="xxxx【邮箱验证码】";//邮件主题(邮箱验证码)
private static final String MAIL_ORGANIZATION ="xxxx";//邮件组织
private static final String MAIL_WEBSITE ="/";//组织主页
private static final String MAIL_ORGANIZATION_LOGO ="wwwxxxx/xxxx/xxxlogo.jpg";//⽹站logo
private static final String MAIL_ORGANIZATION_QRCODE ="/xxxx/xxx.jpg";//⽹站⼆维码图⽚
public static void main(String[] args) throws Exception{
sendEmail(MAIL_FROM,
"xxxx@qq",
MAIL_SUBJECT_REGISTER,
prettyLayout(MAIL_ORGANIZATION,MAIL_WEBSITE,
MAIL_ORGANIZATION_LOGO,
prettyRegisterLayout("2⼩时",MAIL_ORGANIZATION,"/user/register/8asc824jnd741n2c"),
MAIL_ORGANIZATION_QRCODE),
MAIL_FROM_PASSWORD);
sendEmail(MAIL_FROM,
"xxxx@qq",
MAIL_SUBJECT_VERIFY_CODE,
prettyLayout(MAIL_ORGANIZATION,MAIL_WEBSITE,
MAIL_ORGANIZATION_LOGO,
prettyQrCodeLayout("2分钟","580123"),
MAIL_ORGANIZATION_QRCODE),
青春有你2什么时候播出MAIL_FROM_PASSWORD);
}
/**
* 发送邮件
* @param from 发送⼈(邮箱地址)
* @param to 接收⼈ (邮箱地址)
* @param subject 主题
* @param content 内容
* @param password 密码(密码为授权码不是邮箱的登录密码)
eva地垫* @throws Exception
*/
public static void sendEmail(String from,String to,String subject,String content,String password) throws Exception{
Properties props = new Properties();//key value:配置参数。真正发送邮件时再配置
props.setProperty(MAIL_TRANSPORT_PROTOCOL, MAIL_TRANSPORT_PROTOCOL_VALUE);//指定邮件发送的协议,参数是规范规定的 props.setProperty(MAIL_HOST, MAIL_HOST_VALUE);//指定发件服务器的地址,参数是规范规定的
// props.setProperty(MAIL_DEBUG, "true");//邮件发送的调试模式,参数是规范规定的
props.setProperty(MAIL_SMTP_AUTH, CONFIRM);//请求服务器进⾏⾝份认证。参数与具体的JavaMail实现有关
Session session = Instance(props);//发送邮件时使⽤的环境配置
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
//设置邮件的头
关于端午节的对联message.setFrom(new InternetAddress( from )); //谁发送的
message.setRecipients(Message.RecipientType.TO, to );//发送给谁
message.setSubject( subject );
//设置正⽂
message.setContent( content ,SEND_ENCODING_LAYOUT);
message.setContent( content ,SEND_ENCODING_LAYOUT);
// message.setText("<h1>hello</h1>");//纯⽂本
message.saveChanges();
//发送邮件
Transport ts = Transport();
ts.sendMessage(message, AllRecipients());//对象,⽤实例⽅法}
}
/**
* 邮件内容头部
* @param websiteName ⽹站名称
* @param principal ⽹站主页
* @param principalImage ⽹站logo图⽚超链接
* @param logoWidth logo图⽚宽度
* @param logoHeight logo图⽚⾼度
* @return
*/
private static String mailHead(String websiteName,String principal,String principalImage,String logoWidth,String logoHeight){ return "<div style=\"margin: 0 auto; text-align: left;\n" +
"padding: 38px 50px; width: 560px; font-size: 14px;\n" +
"color: #606060; background: #fff; border-radius:\n" +
"2px; font-family:Tahoma,Helvetica,'microsoft\n" +
"yahei','Hiragino Sans GB',Simsun,sans-serif;\n" +
"box-shadow: 0 0 4px rgba(0,0,0,0.2);\">\n" +
" <table style=\"margin: 0 auto;\n" +
"text-align: left; font-size: 14px; color: #606060;\n" +
"background: #fff; font-family: inherit;\n" +
"font-family:Tahoma,Helvetica,'microsoft\n" +
"yahei','Hiragino Sans GB',Simsun,sans-serif;\" width=\"560\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n" +
" <tbody><tr>\n" +
" <th colspan=\"2\"><a href=\""+principal+"\" rel=\"noopener\" target=\"_blank\"><img src=\""+principalImage+"\" width=\""+logoWidth+"\" height=\""+logoHeight+"\"></a></th>\n" +
" </tr>\n" +
" <tr>\n" +
" <td colspan=\"2\" style=\"font-size:\n" +
"18px; padding: 30px 0\n" +
"18px;\">尊敬的"+websiteName+"⽤户:</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td colspan=\"2\" style=\"line-height: 1.8;\">\n" +
" \n" +
"<div>欢迎使⽤"+websiteName+"!</div>\n";
}
/**
* 邮件内容脚部
* @param websiteName ⽹站名称
* @param principal ⽹站主页
* @param qrCode ⼆维码图⽚链接(可不填写/仅⽀持第⼀张)
* @return
*/
private static String mailFoot(String websiteName,String qrCode){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM⽉dd⽇");
String nowDate =sdf.format(new Date());
return " <tr>\n" +
" <td colspan=\"2\" style=\"text-align:\n" +
蓝梅种植"right; line-height: 1.8; padding-bottom: 18px;\">\n" +
" <div>"+websiteName+"团队</div>\n" +
" <div style=\"color:\n" +
"#909090;\">"+nowDate+"</div>\n" +
" </td>\n" +
" </tr>\n" +
(qrCode!=null&&qrCode.length!=0?setQrCode(websiteName,qrCode[0]):"")+
" <tr>\n" +
" <tr>\n" +
" <td colspan=\"2\" style=\"padding-top: 20px; border-top: 1px solid\n" +
"#e7e7e7; line-height: 1.8; font-size: 12px; color:\n" +
"#909090;\">\n" +
" <div>温馨提⽰:</div>\n" +
" <div>1.\n" +websiteName+
"官⽅⽹址为:<a href=\""+principal+"\" rel=\"noopener\" target=\"_blank\">"+principal+"</a>,请注意⽹址,防⽌钓鱼。</div>\n" + " <div>2.\n" +
"本邮件为系统⾃动发出,请勿回复。</div>\n" +
" </td>\n" +
" </tr>\n" +
" </tbody></table>\n" +
" </div>";
}
/**
* 输出漂亮格式(带logo,可带⼆维码)
* @param websiteName ⽹站名称
* @param principal 主页地址
* @param principalImage 主页logo 323 x 48 拉伸宽度为 (超链接形式)
* @param business 业务
* @param qrCode ⼆维码 (超链接形式)
* @return
*/
private static String prettyLayout(String websiteName,String principal,String principalImage,String qrCode){
return mailHead(websiteName,principal,principalImage,"","")+
business +
mailFoot(websiteName,principal,qrCode);
}
/**
* ⼆维码样式A
* @param websiteName //⽹站名称
* @param qrCode //⼆维码图⽚链接
* @return
*/
private static String setQrCode(String websiteName,String qrCode){
return " <tr>\n" +
口字旁的字有哪些字" <td style=\"padding-bottom: 20px;\" width=\"145\">\n" +
" <img src=\""+qrCode+"\" width=\"110\" height=\"112\">\n" +
" </td>\n" +
" <td style=\"line-height: 2;\n" +
"padding-bottom: 20px;\">\n" +
" <div style=\"font-size: 18px;\n" +
"color:\n" +
"#03c5ff;\">扫描关注"+websiteName+"</div>\n" +
" </td>\n" +
" </tr>\n" ;
}
/**
* 发送邮箱验证码
* @param validity 有效期/单位⾃⾏填写
* @param verifyCode 验证码
* @return
*/
private static String prettyQrCodeLayout(String validity,String verifyCode){
return "<div>邮箱验证码的有效期为:"+validity+"邮箱验证码如下:</div>\n" +
" </td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td colspan=\"2\" style=\"font-size:\n" +
"12px; line-height: 20px; padding-top: 14px;\n" +
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论