100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > JavaWeb邮箱注册激活账号

JavaWeb邮箱注册激活账号

时间:2023-06-21 03:00:13

相关推荐

JavaWeb邮箱注册激活账号

JavaWeb邮箱注册激活账号

邮箱工具类MailUtil用法注意事项 激活码工具类UuidUtil用法

邮箱工具类MailUtil

/*** 发邮件工具类*/public final class MailUtils {private static final String USER = "xxxxxxxx@"; // 发件人称号,同邮箱地址private static final String PASSWORD = "xxxxxxxxxx"; // 如果是qq邮箱可以使户端授权码,或者登录密码/**** @param to 收件人邮箱* @param text 邮件正文* @param title 标题*//* 发送验证信息的邮件 */public static boolean sendMail(String to, String text, String title){try {final Properties props = new Properties();props.put("mail.smtp.auth", "true");props.put("mail.smtp.host", "");// 发件人的账号props.put("mail.user", USER);//发件人的密码props.put("mail.password", PASSWORD);// 构建授权信息,用于进行SMTP进行身份验证Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// 用户名、密码String userName = props.getProperty("mail.user");String password = props.getProperty("mail.password");return new PasswordAuthentication(userName, password);}};// 使用环境属性和授权信息,创建邮件会话Session mailSession = Session.getInstance(props, authenticator);// 创建邮件消息MimeMessage message = new MimeMessage(mailSession);// 设置发件人String username = props.getProperty("mail.user");InternetAddress form = new InternetAddress(username);message.setFrom(form);// 设置收件人InternetAddress toAddress = new InternetAddress(to);message.setRecipient(Message.RecipientType.TO, toAddress);// 设置邮件标题message.setSubject(title);// 设置邮件的内容体message.setContent(text, "text/html;charset=UTF-8");// 发送邮件Transport.send(message);return true;}catch (Exception e){e.printStackTrace();}return false;}}

用法

1.更改工具类中的参数,设置发送邮件的账号信息。

2.邮件内容可用超链接指定url同时传递通过工具类获得的唯一激活码(获取方法在后文),激活Servlet可根据获取的激活码进行查询用户,若存在则注册状态为成功。

所以在注册的时候,要调用工具类获取激活码存入数据库,以便后面验证

String text ="邮件内容";MailUtils.sendMail(user.getEmail(),text,"邮件主题");

注意事项

1.QQ邮箱要获取授权码,并传递到password成员变量上。

2.QQ邮箱会把发送的验证邮件里的连接提示危险,于是就不能进行正常转跳,可复制URL连同激活码参数,重新在地址栏粘贴(暂时不知道怎么解决);

激活码工具类UuidUtil

/*** 产生UUID随机字符串工具类*/public final class UuidUtil {private UuidUtil(){}public static String getUuid(){return UUID.randomUUID().toString().replace("-","");}/*** 测试*/public static void main(String[] args) {System.out.println(UuidUtil.getUuid());System.out.println(UuidUtil.getUuid());System.out.println(UuidUtil.getUuid());System.out.println(UuidUtil.getUuid());}}

用法

1.该类会创建全球唯一的激活码,(重复的概率微乎其微,中500万奖差不多概率)

2.静态调用

// 生成唯一的激活码String code = UuidUtil.getUuid();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。