spring-email/src/main/java/com/xiongyingqi/email/service/impl/EmailService.java
@Override
    public void sendEmail(EmailVo emailVo) throws MessagingException {
        Assert.notNull(emailVo.getTo(), "接收人不能为空");

        MimeMessage msg = javaMailSenderFactory.createMimeMessage();

        MimeMessageHelper msgHelper = null;
        if ((emailVo.getInlineImageVos() != null && emailVo.getInlineImageVos().size() > 0) || (emailVo.getAttachmentVos() != null && emailVo.getAttachmentVos().size() > 0)) {
            msgHelper = new MimeMessageHelper(msg, true, "utf-8");
        } else {
            msgHelper = new MimeMessageHelper(msg, "utf-8");
        }


        if (emailVo.getFrom() == null || "".equals(emailVo.getFrom().trim())) {
            emailVo.setFrom(javaMailSenderFactory.getSystemEmail());
        }
        if ((emailVo.getCc() == null || "".equals(emailVo.getCc().length == 0)) && javaMailSenderFactory.getDefaultCc() != null && !javaMailSenderFactory.getDefaultCc().equals("")) {
            emailVo.setCc(javaMailSenderFactory.getDefaultCc().split(","));
        }
        if ((emailVo.getBcc() == null || "".equals(emailVo.getBcc().length == 0)) && javaMailSenderFactory.getDefaultBcc() != null && !javaMailSenderFactory.getDefaultBcc().equals("")) {
            emailVo.setBcc(javaMailSenderFactory.getDefaultBcc().split(","));
        }


        if (emailVo.getMessageDate() == null) {
            emailVo.setMessageDate(new Date());
        }
        if (emailVo.getCc() != null) {
            msgHelper.setCc(emailVo.getCc());// 抄送
        }
        if (emailVo.getBcc() != null) {
            msgHelper.setBcc(emailVo.getBcc());// 密送
        }
        if (emailVo.getSubject() != null) {
            msgHelper.setSubject(emailVo.getSubject());
        }


        handlerAttachments(emailVo, msgHelper);

        String from = null;
        if (emailVo.getFrom() != null && !"".equals(emailVo.getFrom())) {
            from = emailVo.getFrom();
        } else {
            if (javaMailSenderFactory.getEmailAccount().getFrom() != null && !"".equals(javaMailSenderFactory.getEmailAccount().getFrom())) {
                from = javaMailSenderFactory.getEmailAccount().getFrom();
            } else {
                from = javaMailSenderFactory.getEmailAccount().getUsername();
            }
        }

        try {
            if (javaMailSenderFactory.getEmailAccount().getNickName() != null) {
                msgHelper.setFrom(from, javaMailSenderFactory.getEmailAccount().getNickName());
            } else {
                msgHelper.setFrom(from);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            msgHelper.setFrom(from);
        }


        msgHelper.setTo(emailVo.getTo());// 接收人
        Collection<Inline> inlines = handlerInlineImages(emailVo, msgHelper);

        if (emailVo.getHtml() != null) {
            msgHelper.setText(emailVo.getHtml(), emailVo.isHtml());

            if (inlines != null) {// 添加inline
                for (Inline inline : inlines) {
                    msgHelper.addInline(inline.getContentId(), inline.getFile());
                }
            }
        }
        javaMailSenderFactory.send(msg);
    }
@Override
    public void sendEmail(EmailVo emailVo) throws MessagingException {
        Assert.notNull(emailVo.getTo(), "接收人不能为空");

        MimeMessage msg = javaMailSenderFactory.createMimeMessage();

        MimeMessageHelper msgHelper = null;
        if ((emailVo.getInlineImageVos() != null && emailVo.getInlineImageVos().size() > 0) || (emailVo.getAttachmentVos() != null && emailVo.getAttachmentVos().size() > 0)) {
            msgHelper = new MimeMessageHelper(msg, true, "utf-8");
        } else {
            msgHelper = new MimeMessageHelper(msg, "utf-8");
        }


        if (emailVo.getFrom() == null || "".equals(emailVo.getFrom().trim())) {
            emailVo.setFrom(javaMailSenderFactory.getSystemEmail());
        }
        if ((emailVo.getCc() == null || "".equals(emailVo.getCc().length == 0)) && javaMailSenderFactory.getDefaultCc() != null && !javaMailSenderFactory.getDefaultCc().equals("")) {
            emailVo.setCc(javaMailSenderFactory.getDefaultCc().split(","));
        }
        if ((emailVo.getBcc() == null || "".equals(emailVo.getBcc().length == 0)) && javaMailSenderFactory.getDefaultBcc() != null && !javaMailSenderFactory.getDefaultBcc().equals("")) {
            emailVo.setBcc(javaMailSenderFactory.getDefaultBcc().split(","));
        }


        if (emailVo.getMessageDate() == null) {
            emailVo.setMessageDate(new Date());
        }
        if (emailVo.getCc() != null) {
            msgHelper.setCc(emailVo.getCc());// 抄送
        }
        if (emailVo.getBcc() != null) {
            msgHelper.setBcc(emailVo.getBcc());// 密送
        }
        if (emailVo.getSubject() != null) {
            msgHelper.setSubject(emailVo.getSubject());
        }


        handlerAttachments(emailVo, msgHelper);

        String from = null;
        if (emailVo.getFrom() != null && !"".equals(emailVo.getFrom())) {
            from = emailVo.getFrom();
        } else {
            if (javaMailSenderFactory.getEmailAccount().getFrom() != null && !"".equals(javaMailSenderFactory.getEmailAccount().getFrom())) {
                from = javaMailSenderFactory.getEmailAccount().getFrom();
            } else {
                from = javaMailSenderFactory.getEmailAccount().getUsername();
            }
        }

        try {
            if (javaMailSenderFactory.getEmailAccount().getNickName() != null) {
                msgHelper.setFrom(from, javaMailSenderFactory.getEmailAccount().getNickName());
            } else {
                msgHelper.setFrom(from);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            msgHelper.setFrom(from);
        }


        msgHelper.setTo(emailVo.getTo());// 接收人
        Collection<Inline> inlines = handlerInlineImages(emailVo, msgHelper);

        if (emailVo.getHtml() != null) {
            msgHelper.setText(emailVo.getHtml(), emailVo.isHtml());

            if (inlines != null) {// 添加inline
                for (Inline inline : inlines) {
                    msgHelper.addInline(inline.getContentId(), inline.getFile());
                }
            }
        }
        javaMailSenderFactory.send(msg);
    }
@ Override public void sendEmail ( EmailVo emailVo ) throws MessagingException { Assert . notNull ( emailVo . getTo ( ) , "接收人不能为空"); MimeMessage msg = javaMailSenderFactory . createMimeMessage ( ) ; MimeMessageHelper msgHelper = null ; if ( ( emailVo . getInlineImageVos ( ) != null && emailVo . getInlineImageVos ( ) . size ( ) > 0 ) || ( emailVo . getAttachmentVos ( ) != null && emailVo . getAttachmentVos ( ) . size ( ) > 0 ) ) { msgHelper = new MimeMessageHelper ( msg , true , "utf-8" ) ; } else { msgHelper = new MimeMessageHelper ( msg , "utf-8" ) ; } if ( emailVo . getFrom ( ) == null || "" . equals ( emailVo . getFrom ( ) . trim ( ) ) ) { emailVo . setFrom ( javaMailSenderFactory . getSystemEmail ( ) ) ; } if ( ( emailVo . getCc ( ) == null || "" . equals ( emailVo . getCc ( ) . length == 0 ) ) && javaMailSenderFactory . getDefaultCc ( ) != null && ! javaMailSenderFactory . getDefaultCc ( ) . equals ( "" ) ) { emailVo . setCc ( javaMailSenderFactory . getDefaultCc ( ) . split ( "," ) ) ; } if ( ( emailVo . getBcc ( ) == null || "" . equals ( emailVo . getBcc ( ) . length == 0 ) ) && javaMailSenderFactory . getDefaultBcc ( ) != null && ! javaMailSenderFactory . getDefaultBcc ( ) . equals ( "" ) ) { emailVo . setBcc ( javaMailSenderFactory . getDefaultBcc ( ) . split ( "," ) ) ; } if ( emailVo . getMessageDate ( ) == null ) { emailVo . setMessageDate ( new Date ( ) ) ; } if ( emailVo . getCc ( ) != null ) { msgHelper . setCc ( emailVo . getCc ( ) ) ; // 抄送 } if ( emailVo . getBcc ( ) != null ) { msgHelper . setBcc ( emailVo . getBcc ( ) ) ; // 密送 } if ( emailVo . getSubject ( ) != null ) { msgHelper . setSubject ( emailVo . getSubject ( ) ) ; } handlerAttachments ( emailVo , msgHelper ) ; String from = null ; if ( emailVo . getFrom ( ) != null && ! "" . equals ( emailVo . getFrom ( ) ) ) { from = emailVo . getFrom ( ) ; } else { if ( javaMailSenderFactory . getEmailAccount ( ) . getFrom ( ) != null && ! "" . equals ( javaMailSenderFactory . getEmailAccount ( ) . getFrom ( ) ) ) { from = javaMailSenderFactory . getEmailAccount ( ) . getFrom ( ) ; } else { from = javaMailSenderFactory . getEmailAccount ( ) . getUsername ( ) ; } } try { if ( javaMailSenderFactory . getEmailAccount ( ) . getNickName ( ) != null ) { msgHelper . setFrom ( from , javaMailSenderFactory . getEmailAccount ( ) . getNickName ( ) ) ; } else { msgHelper . setFrom ( from ) ; } } catch ( UnsupportedEncodingException e ) { e . printStackTrace ( ) ; msgHelper . setFrom ( from ) ; } msgHelper . setTo ( emailVo . getTo ( ) ) ; // 接收人 Collection < Inline > inlines = handlerInlineImages ( emailVo , msgHelper ) ; if ( emailVo . getHtml ( ) != null ) { msgHelper . setText ( emailVo . getHtml ( ) , emailVo . isHtml ( ) ) ; if ( inlines != null ) { // 添加inline for ( Inline inline : inlines ) { msgHelper . addInline ( inline . getContentId ( ) , inline . getFile ( ) ) ; } } } javaMailSenderFactory . send ( msg ) ; }
发送邮件

@param emailVo
发送邮件