邮件和短信验证码

public static boolean sendEmail(String email,String title,String text) {
        //发送标题
        try {
            String typeName = title;
            String from = "[email protected]";        //邮件发送人的邮件地址
            String username = "[email protected]";   //邮件发送人的邮件地址
            String password = "Pool14ok19";             //发件人的邮件密码

            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            // Get a Properties object
            Properties props = new Properties();
            props.setProperty("mail.smtp.host", "smtp.qiye.aliyun.com");
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", "465");
            props.setProperty("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.auth", "true");

            Session session = Session.getDefaultInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
            Message msg = new MimeMessage(session);
            //发送内容
            String mailContent = text;
            // 设置发件人和收件人
            msg.setFrom(new InternetAddress(from));
            List<String> tos = new ArrayList<>();
            tos.add(email);
            Address to[] = new InternetAddress[tos.size()];
            for (int i = 0; i < tos.size(); i++) {
                to[i] = new InternetAddress(tos.get(i));
            }
            // 多个收件人地址
            msg.setRecipients(Message.RecipientType.TO, to);
            msg.setSubject(typeName); // 标题
            // 设置邮件的内容体
            msg.setContent(mailContent, "text/html;charset=UTF-8");
            // 发送邮件
            msg.setSentDate(new Date());
            Transport.send(msg);
            return true;
        }catch (Exception e){
            log.error("######Email error list{}",e);
            return false;
        }
    }
邮箱发送验证码

猜你喜欢

转载自www.cnblogs.com/fengmo2427/p/11225525.html
今日推荐