zoukankan      html  css  js  c++  java
  • 发送激活码到邮箱激活账户

    package com.ceobai.utils;
    
    import java.util.Properties;
    
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMessage.RecipientType;
    
    public class MailUtils {
    
        public static void sendMail(String email, String emailMsg)
                throws AddressException, MessagingException {
            // 1.创建一个程序与邮件服务器会话对象 Session
            Properties props = new Properties();
            //设置发送的协议   *可能需要改的地方*
            props.setProperty("mail.transport.protocol", "SMTP");
            
            //设置发送邮件的服务器     *localhost需要根据实际情况更改,如163的为 smtp.163.com*
            props.setProperty("mail.host", "localhost");
            props.setProperty("mail.smtp.auth", "true");// 指定验证为true
    
            // 创建验证器
            Authenticator auth = new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    //设置发送人的帐号和密码       *需要改的地方*
                    return new PasswordAuthentication("root", "123");
                }
            };
    
            Session session = Session.getInstance(props, auth);
    
            // 2.创建一个Message,它相当于是邮件内容
            Message message = new MimeMessage(session);
    
            //设置发送者       *需要改的地方,如ceobaidu@163.com*
            message.setFrom(new InternetAddress("root@dege.com"));
    
            //设置发送方式与接收者(mail目的地)
            message.setRecipient(RecipientType.TO, new InternetAddress(email)); 
    
            //设置邮件主题    *根据实际情况添加主题*
            message.setSubject("来自德哥会所的激活邮件");
            // message.setText("这是一封激活邮件,请<a href='#'>点击</a>");
    
            //设置邮件内容  
            message.setContent(emailMsg, "text/html;charset=utf-8");
    
            // 3.创建 Transport用于将邮件发送
            Transport.send(message);
        }
    }

    调用该工具类的代码:

    //发送邮件(email收件人地址)(emailMsg邮件的内容)(localhost网站的域名)
       String emailMsg = "欢迎来到德哥会所,<a href='http://localhost:8089/store_v2.0/userServlet?method=active&code="+user.getCode()+"'>请点击激活</a>";
       MailUtils.sendMail(user.getEmail(), emailMsg);
  • 相关阅读:
    染色问题的算法(转)
    函数的定义域和定义区间的区别(转)
    详解c++指针的指针和指针的引用(转)
    母函数(转)
    别人整理的DP大全(转)
    HDU 1430 魔板(康托展开+BFS+预处理)
    ZZULIOJ 1726 迷宫(BFS+小坑)
    NBUT 1635 Explosion(最小顶点覆盖)
    NBUT 1602 Mod Three(线段树单点更新区间查询)
    NBUT 1028 该减肥了(简单递推)
  • 原文地址:https://www.cnblogs.com/Jansens520/p/9257485.html
Copyright © 2011-2022 走看看