美文网首页
Java实现邮箱发送邮件验证功能

Java实现邮箱发送邮件验证功能

作者: Scalelength | 来源:发表于2020-01-13 11:08 被阅读0次

    本文是说明springboot+maven完成邮箱发送验证功能。

    参考:https://www.cnblogs.com/zhangdiIT/p/8184293.html

    首先是JavaMail相关依赖

    <!-- JavaMail相关依赖 -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>

    <dependency>
         <groupId>javax.activation</groupId>
         <artifactId>activation</artifactId>
         <version>1.1.1</version>
    </dependency>

    再来是三个类

    一、Mail的实体类

    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;

    /** * TODO.... * * @author liuzhao * @date 2019-12-20 10:00 */
    public class MailAuthenticator extends Authenticator{

        public static String USERNAME = "发送邮件的邮箱";
        public static String PASSWORD = "邮箱的登陆密码";

        public MailAuthenticator() { }

        public PasswordAuthentication getPasswordAuthentication() {

            return new PasswordAuthentication(USERNAME, PASSWORD);

        }

    }

    二、封装Mail类

    public class MailEntity {
    private String user;//你的邮箱地址 
     private String passord;//邮箱授权码 
     private String host = "smtp.163.com"; //服务器地址
     private String from;//发件人 
     private String to;// 收件人
     private String subject;//主题  
     private String sb; //邮箱内容
     private String code;//验证码

    //getter和setter方法省略,除了邮箱内容

    public void setSb(String code){ this.sb="<html><head></head><body><h1>这是一封激活邮件,激活请点击以下链接</h1><h3><a href='http://localhost:8080/register/ActiveServlet?code=" + code + "'>http://localhost:8080/register/ActiveServlet?code=" + code + "</href></h3></body></html>";}
    public String getSb() { return sb;}

    三、发送操作类

    邮件发送操作类

    public class MailOperationimplements Runnable{

        private MailEntitymailEntity;

        public MailOperation(MailEntity mailEntity) {
            this.mailEntity = mailEntity;
        }

    /**

    * 发送邮件

        * @param user 发件人邮箱

        * @param password 授权码(注意不是邮箱登录密码)

        * @param host

        * @param from 发件人

        * @param to 接收者邮箱

        * @param subject 邮件主题

        * @param content 邮件内容

        * @return success 发送成功 failure 发送失败

        * @throws Exception

    */

        public static StringsendMail(String user, String password, String host,

                              String from, String to, String subject, String content)

    throws Exception {

        if (to !=null){

        Properties props = System.getProperties();

                props.put("mail.smtp.host", host);

                props.put("mail.smtp.auth", "true");

                MailAuthenticator auth =new MailAuthenticator();

                MailAuthenticator.USERNAME = user;

                MailAuthenticator.PASSWORD = password;

                Session session = Session.getInstance(props, auth);

                session.setDebug(true);

                try {

                    MimeMessage message =new MimeMessage(session);

                    message.setFrom(new InternetAddress(from));

                    if (!to.trim().equals(""))

                        message.addRecipient(Message.RecipientType.TO,

                                new InternetAddress(to.trim()));

                    message.setSubject(subject);

                    MimeBodyPart mbp1 =new MimeBodyPart(); // 正文

                    mbp1.setContent(content, "text/html;charset=utf-8");

                    Multipart mp =new MimeMultipart(); // 整个邮件:正文+附件

                    mp.addBodyPart(mbp1);

                    // mp.addBodyPart(mbp2);

                    message.setContent(mp);

                    message.setSentDate(new Date());

                    message.saveChanges();

                    Transport trans = session.getTransport("smtp");

                    trans.send(message);

                    System.out.println(message.toString());

                }catch (Exception e){

                    e.printStackTrace();

                    return "failure";

                }

    return "success";

            }else{

    return "failure";

            }

    }

    public static BooleanmailSetting(MailEntity mailEntity) {

    //        MailOperation operation = new MailOperation();

            try {

    //发送验证码

                String res = MailOperation.sendMail(mailEntity.getUser(),

                        mailEntity.getPassword(),

                        mailEntity.getHost(),

                        mailEntity.getFrom(),

                        mailEntity.getTo(),

                        mailEntity.getSubject(),
                        mailEntity.getSb().toString());
                System.out.println(res);
            }catch (Exception e) {
    e.printStackTrace();

    return false;

            }

        return true;

        }

        @Override
        public void run() {

            mailSetting(mailEntity);
        }

    }

    四、发送邮件

    //发送邮件

            MailEntity mailEntity =new MailEntity();

            mailEntity.setSb(user.getCode());

            mailEntity.setTo(user.getMail());

            mailEntity.setCode(user.getCode());

            new Thread(new MailOperation(mailEntity)).start();

    相关文章

      网友评论

          本文标题:Java实现邮箱发送邮件验证功能

          本文链接:https://www.haomeiwen.com/subject/cedgactx.html