利用Spring的JavaMailSenderImpl发送邮件

引入 pom

<!-- 发送邮件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

 

 /**
     * 发送邮件.
     * 
     * @param mailSender
     *            JavaMailSenderImpl对象
     * @param toAddresses
     *            收信人地址列表。使用此方法时,要注意邮件服务器对群发邮件数量的考虑
     * @param subject
     *            邮件主题
     * @param content
     *            邮件内容
     * @return true, if successful
     * @throws Exception
     *             the exception
     * @return
     */
    public static boolean send(JavaMailSenderImpl mailSender, String[] toAddresses, String subject,
                               String content) throws Exception {
        MimeMessage mimeMsg = null;
        try {
            mimeMsg = ((JavaMailSenderImpl) mailSender).createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true, "UTF-8");
            InternetAddress[] address = new InternetAddress[toAddresses.length];
            for (int i = 0; i < address.length; i++) {
                address[i] = new InternetAddress(toAddresses[i]);
            }
            helper.setTo(address);
            String fromAddress = ((JavaMailSenderImpl) mailSender).getUsername();
            helper.setFrom(fromAddress, "xx系统平台");
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(mimeMsg);
        } catch (Exception ex) {
            logger.error("邮件发送异常。", ex);
            throw new Exception("邮件发送失败");
        }
        return true;
    }

    /**
     * 发送邮件并添加附件
     */
    public static boolean send(JavaMailSenderImpl mailSender, String[] toAddresses, String subject,
                               String content, List<File> files) throws Exception {

        MimeMessage mimeMsg = null;
        try {
            mimeMsg = ((JavaMailSenderImpl) mailSender).createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true, "UTF-8");
            InternetAddress[] address = new InternetAddress[toAddresses.length];
            for (int i = 0; i < address.length; i++) {
                address[i] = new InternetAddress(toAddresses[i]);
            }
            helper.setTo(address);
            String fromAddress = ((JavaMailSenderImpl) mailSender).getUsername();
            helper.setFrom(fromAddress, "餐补系统平台");
            helper.setSubject(subject);
            helper.setText(content, true);
            for (File file : files) {
                helper.addAttachment(file.getName(), file);
            }
            ((JavaMailSenderImpl) mailSender).send(mimeMsg);
            return true;
        } catch (Exception ex) {
            logger.error("邮件发送异常。", ex);
            return false;
        }
    }

    /**
     * 单人邮件
     * @param mailSender
     * @param toAddresses
     * @param subject
     * @param content
     * @throws Exception
     */
    public static void send(JavaMailSenderImpl mailSender, String toAddresses, String subject,
                            String content) throws Exception {
        MimeMessage mimeMsg = null;
        try {
            mimeMsg = ((JavaMailSenderImpl) mailSender).createMimeMessage(); //使用JavaMail的MimeMessage,支持更加复杂的邮件格式和内容
            MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true, "UTF-8");//设置utf-8编码,否则邮件会有乱码,
            helper.setTo(toAddresses);
            String fromAddress = ((JavaMailSenderImpl) mailSender).getUsername();
            helper.setFrom(fromAddress, "xx系统平台");
            helper.setSubject(subject);
            helper.setText(content, true);//第二个参数true,表示text的内容为html
            mailSender.send(mimeMsg);
        } catch (Exception ex) {
            logger.error("邮件发送异常。", ex);
            throw new Exception("邮件发送失败");
        }
    }

转载于:https://my.oschina.net/u/3820486/blog/1816211

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值