使用JavaMail群发邮件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gyx_2110/article/details/76136081

很多时候我们有群发邮件的需求,虽然大多数邮箱都支持群发功能,但是还是有点繁杂,JavaMail提供的API功能强大,可能很方便的解决我们的问题

下面我们就开始来写我们的代码吧
首先需要准备好下面两个jar包

用于获得收件人的方法:

    public String[] getRecipients() {
        File recipientsFile = new File("E:Recipients.txt");
        InputStream in = null;
        BufferedReader br = null;
        try {
            in = new FileInputStream(recipientsFile);
            br = new BufferedReader(new InputStreamReader(in));
            String line = null;
            StringBuilder builder = new StringBuilder();
            // 读入联系人
            while ((line = br.readLine()) != null) {
                builder.append(line);
                builder.append(",");
            }
            // 将联系人分割为数组返回
            return builder.toString().split(",");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

用于发送邮件的方法:

    public void sendGroupMail() throws AddressException, MessagingException {
        // 得到Session
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");
        props.setProperty("mail.smtp.auth", "true");
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //邮箱的用户名和密码
                return new PasswordAuthentication("gyx2110", "********");
            }
        };
        Session session = Session.getInstance(props, authenticator);

        //发送邮件
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        String[] recipients = getRecipients();
        for (String rec : recipients) {
            //设置收件人
            message.setRecipient(RecipientType.TO, new InternetAddress(rec));
            //设置标题
            message.setSubject("JavaMail测试邮件!");
            //设置正文
            message.setContent("群发邮件,如有打扰请见谅!", "text/html;charset=utf-8");
            //发送
            Transport.send(message);
        }
    }

值得一提的是我们群发的时候为了隐私,这里使用的发送方式是逐条发送,而不是批量发送,这样在正文页面就看不到有哪些人收到这封邮件了。


到这里我们只是发送了普通文体信息,大多数时我们的邮件是带有附件的,下面的我们来为邮件中添加附件。

添加附件的方法:

    public void setMultipart(MimeMessage msg) throws MessagingException,
            IOException {
        // 一个Multipart对象包含一个或多个BodyPart对象,来组成邮件的正文部分(包括附件)。
        MimeMultipart multiPart = new MimeMultipart();

        // 添加正文
        MimeBodyPart partText = new MimeBodyPart();
        partText.setContent("这是一封含有附件的群发邮件!", "text/html;charset=utf-8");

        // 添加文件 也就 是附件
        MimeBodyPart partFile = new MimeBodyPart();
        File file = new File("E:mail.jar");
        partFile.attachFile(file);
        // 设置在收件箱中和附件名 并进行编码以防止中文乱码
        partFile.setFileName(MimeUtility.encodeText(file.getName()));

        multiPart.addBodyPart(partText);
        multiPart.addBodyPart(partFile);
        msg.setContent(multiPart);
    }

发送带附件的邮件:

    public void sendMultipartMail() throws AddressException, MessagingException,
            IOException {
        // 得到Session
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");
        props.setProperty("mail.smtp.auth", "true");
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 邮箱的用户名和密码
                return new PasswordAuthentication("gyx2110", "********");
            }
        };
        Session session = Session.getInstance(props, authenticator);

        // 发送邮件
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        // 设置收件人
        message.setRecipient(RecipientType.TO, new InternetAddress(
                "[email protected]"));
        // 设置标题
        message.setSubject("JavaMail带附件的测试邮件!");
        // 设置邮件主体
        setMultipart(message);
        // 发送
        Transport.send(message);
    }

猜你喜欢

转载自blog.csdn.net/gyx_2110/article/details/76136081