首页 > 其他分享 >邮箱工具类

邮箱工具类

时间:2023-02-16 12:44:04浏览次数:27  
标签:mail InternetAddress new 邮箱 工具 properties 邮件 String

导入依赖

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

工具类 

public class SmtpEmailUtils {
    /**
     * 1发送邮件(单条发送纯文本邮件)
     * @param mailSend 发件人
     * @param mailSendAuthCode 发件人邮箱授权码
     * @param mailReceive 收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @throws AddressException
     */
    public static void sendMail(String mailSend, String mailSendAuthCode, String mailReceive, String subject, String content) throws MessagingException {
        /**
         * 配置发送邮件的属性
         */
        Properties properties = new Properties();

        /**
         * 是否需要身份验证
         */
        properties.put("mail.smtp.auth","true");

        /**
         * 服务器地址
         */
        properties.put("mail.smtp.host","smtp.qq.com");

        /**
         * mailSend(设置发件人邮箱,需开启smtp)
         */
        properties.put("mail.user",mailSend);

        /**
         * 设置smtp提供的授权码(非密码)
         */
        properties.put("mail.password",mailSendAuthCode);

        /**
         * 构建授权信息,用于进行SMTP身份验证
         */
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                String userName = properties.getProperty("mail.user");
                String password = properties.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };

        /**
         * 使用环境属性和授权信息,创建邮件会话
         */
        Session session = Session.getInstance(properties, authenticator);

        /**
         * 创建邮件消息
         */
        MimeMessage mimeMessage = new MimeMessage(session);

        /**
         * 设置发件人
         */
        InternetAddress from = new InternetAddress(properties.getProperty("mail.user"));
        mimeMessage.setFrom(from);

        /**
         * 设置收件人
         */
        InternetAddress to = new InternetAddress(mailReceive);
        mimeMessage.setRecipient(Message.RecipientType.TO,to);

        /**
         * 设置抄送
         */
//        InternetAddress cc = new InternetAddress("[email protected]");
//        mimeMessage.setRecipient(Message.RecipientType.CC,cc);

        /**
         * 设置秘密抄送,其他收件人不能看到密送的邮件地址
         */
//        InternetAddress bcc = new InternetAddress("[email protected]");
//        mimeMessage.setRecipient(Message.RecipientType.BCC,bcc);

        /**
         * 设置邮件标题
         */
        mimeMessage.setSubject(subject);

        /**
         * 邮件内容
         */
        mimeMessage.setContent(content,"text/html;charset = UTF-8");

        /**
         * 发送邮件
         */
        Transport.send(mimeMessage);
    }

    /**
     * 2发送邮件(群发1)
     * @param mailSend 发件人
     * @param mailSendAuthCode 发件人邮箱授权码
     * @param mailReceiveList 收件人列表(多个邮箱)
     * @param subject 邮件主题
     * @param content 邮件内容
     * @throws AddressException
     */
    public static void sendMails(String mailSend, String mailSendAuthCode, List mailReceiveList, String subject, String content) throws MessagingException {
        /**
         * 配置发送邮件的属性
         */
        Properties properties = new Properties();

        /**
         * 是否需要身份验证
         */
        properties.put("mail.smtp.auth","true");

        /**
         * 服务器地址
         */
        properties.put("mail.smtp.host","smtp.qq.com");

        /**
         * mailSend(设置发件人邮箱,需开启smtp)
         */
        properties.put("mail.user",mailSend);

        /**
         * 设置smtp提供的授权码(非密码)
         */
        properties.put("mail.password",mailSendAuthCode);

        /**
         * 构建授权信息,用于进行SMTP身份验证
         */
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                String userName = properties.getProperty("mail.user");
                String password = properties.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };

        /**
         * 使用环境属性和授权信息,创建邮件会话
         */
        Session session = Session.getInstance(properties, authenticator);

        /**
         * 创建邮件消息
         */
        MimeMessage mimeMessage = new MimeMessage(session);

        /**
         * 设置发件人
         */
        InternetAddress from = new InternetAddress(properties.getProperty("mail.user"));
        mimeMessage.setFrom(from);

        /**
         * 设置收件人
         */
        InternetAddress[] tos = new InternetAddress[mailReceiveList.size()];
        for (int i = 0; i < mailReceiveList.size(); i++) {
            tos[i] = new InternetAddress(mailReceiveList.get(i).toString());
        }
        mimeMessage.setRecipients(Message.RecipientType.TO,tos);

        /**
         * 设置抄送
         */
//        InternetAddress cc = new InternetAddress("[email protected]");
//        mimeMessage.setRecipient(Message.RecipientType.CC,cc);

        /**
         * 设置秘密抄送,其他收件人不能看到密送的邮件地址
         */
//        InternetAddress bcc = new InternetAddress("[email protected]");
//        mimeMessage.setRecipient(Message.RecipientType.BCC,bcc);

        /**
         * 设置邮件标题
         */
        mimeMessage.setSubject(subject);

        /**
         * 邮件内容
         */
        mimeMessage.setContent(content,"text/html;charset = UTF-8");

        /**
         * 发送邮件
         */
        Transport.send(mimeMessage);
    }

    /**
     * 3发送邮件(群发2)
     * @param mailSend 发件人
     * @param mailSendAuthCode 发件人邮箱授权码
     * @param mailReceives 收件人(群发,多个邮箱以逗号隔开)
     * @param subject 邮件主题
     * @param content 邮件内容
     * @throws AddressException
     */
    public static void sendMails(String mailSend, String mailSendAuthCode, String mailReceives, String subject, String content) throws MessagingException {
        /**
         * 配置发送邮件的属性
         */
        Properties properties = new Properties();

        /**
         * 是否需要身份验证
         */
        properties.put("mail.smtp.auth","true");

        /**
         * 服务器地址
         */
        properties.put("mail.smtp.host","smtp.qq.com");

        /**
         * mailSend(设置发件人邮箱,需开启smtp)
         */
        properties.put("mail.user",mailSend);

        /**
         * 设置smtp提供的授权码(非密码)
         */
        properties.put("mail.password",mailSendAuthCode);

        /**
         * 构建授权信息,用于进行SMTP身份验证
         */
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                String userName = properties.getProperty("mail.user");
                String password = properties.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };

        /**
         * 使用环境属性和授权信息,创建邮件会话
         */
        Session session = Session.getInstance(properties, authenticator);

        /**
         * 创建邮件消息
         */
        MimeMessage mimeMessage = new MimeMessage(session);

        /**
         * 设置发件人
         */
        InternetAddress from = new InternetAddress(properties.getProperty("mail.user"));
        mimeMessage.setFrom(from);

        /**
         * 设置收件人(多个收件人,群发,收件人之间以逗号隔开)
         */
//        InternetAddress tos = new InternetAddress(mailReceives);
        mimeMessage.setRecipients(Message.RecipientType.TO,mailReceives);

        /**
         * 设置抄送
         */
//        InternetAddress cc = new InternetAddress("[email protected]");
//        mimeMessage.setRecipient(Message.RecipientType.CC,cc);

        /**
         * 设置秘密抄送,其他收件人不能看到密送的邮件地址
         */
//        InternetAddress bcc = new InternetAddress("[email protected]");
//        mimeMessage.setRecipient(Message.RecipientType.BCC,bcc);

        /**
         * 设置邮件标题
         */
        mimeMessage.setSubject(subject);

        /**
         * 邮件内容
         */
        mimeMessage.setContent(content,"text/html;charset = UTF-8");

        /**
         * 发送邮件
         */
        Transport.send(mimeMessage);
    }

    /**
     * 适配服务器
     * 开启ssl安全机制,全局打印邮件发送者和内容相关信息
     * 该方法开启ssl并改端口为465后,解决了阿里云服务器中运行发送邮件报错问题(连接超时)
     * @param sender 发件人
     * @param senderAuthCode 发件人授权码
     * @param receivers 收件人(单个收件人或多个收件人,多个邮箱则用逗号隔开)
     * @param emailSubject 邮件主题
     * @param emailContent 邮件内容
     * @throws MessagingException
     */
    public static void sendSslEmail(String sender, String senderAuthCode, String receivers, String emailSubject, String emailContent) throws MessagingException {
        /**
         * 配置发送邮件配置的属性环境
         */
        Properties properties = new Properties();

        /**
         * 配置邮件服务器地址
         */
        properties.setProperty("mail.smtp.host", "smtp.qq.com");

        /**
         * 开启授权
         */
        properties.setProperty("mail.smtp.auth","true");

        /**
         * 配置端口号
         */
        properties.setProperty("mail.smtp.port","465");

        /**
         * 开启ssl安全机制
         */
        properties.setProperty("mail.smtp.ssl.enable","true");

        /**
         * 构建授权信息,用于smtp身份验证
         */
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(sender,senderAuthCode);
            }
        };

        /**
         * 使用邮件属性和授权信息,创建会话
         */
        Session session = Session.getDefaultInstance(properties, authenticator);

        /**
         * 开启debug模式,将信息打印控制台
         */
        session.setDebug(true);

        /**
         * 构建邮件消息体
         */
        Message message = new MimeMessage(session);

        /**
         * 设置发件人
         */
        message.setFrom(new InternetAddress(sender));

        /**
         * 发送邮件的方式(直发,抄送,秘密抄送等)收件人如果为多个,可在收件人邮箱与邮箱之间用逗号隔开
         */
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(receivers));

        /**
         * 设置邮件主题
         */
        message.setSubject(emailSubject);

        /**
         * 设置邮件内容
         */
        message.setContent(emailContent,"text/html;charset=UTF-8");

        /**
         * 发送邮件
         */
        Transport.send(message);
    }
}

标签:mail,InternetAddress,new,邮箱,工具,properties,邮件,String
From: https://www.cnblogs.com/Cha7N/p/17126259.html

相关文章