JavaMailSender发送邮件 附件未能发送问题

最近要做一个面试通过发放offer的功能,笔者想到了用JavaMailSender来实现,途中遇到了发送邮件的坑,记录一下这个对api不熟悉引发的弱智问题╰(‵□′)╯

话不多说先上错误代码

public class MailUtil {
    /**
     * 企业微信邮箱用户名
     */
    private static final String USERNAME = "你的企业微信邮箱用户名";
    /**
     * 企业微信邮箱密码
     */
    private static final String PASSWORD = "企业微信邮箱密码";
    /**
     * 企业微信邮箱主机地址
     */
    private static final String HOST = "smtp.exmail.qq.com";
    /**
     * 端口
     */
    private static final int PORT = 465;
    /**
     * 超时时间
     */
    private static final int TIMEOUT = 5000;
    /**
     * ssl
     */
    private static final boolean SSL_ENABLE = true;
    /**
     * ttl
     */
    private static final boolean STARTTLS_ENABLE = true;
    /**
     * 身份认证
     */
    private static final boolean AUTH = true;
    /**
     *
     */
    private static final String SOCKET_FACTORY_CLASS = "javax.net.ssl.SSLSocketFactory";



    /**
     * 发送邮件
     *
     * @param to        收件人
     * @param cc        抄送人列表
     * @param subject   主体
     * @param content   内容
     * @param filePaths 文件路径列表
     * @throws IOException
     * @throws MessagingException
     */
    public static void sendMail(String to, List<String> cc, String subject, String content, List<String> filePaths) throws IOException, MessagingException {
        if (to == null) {
            throw new ServiceException("收件人不能为空!");
        }
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(HOST);
        mailSender.setUsername(USERNAME);
        mailSender.setPort(PORT);
        mailSender.setPassword(PASSWORD);
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", String.valueOf(AUTH));
        properties.setProperty("mail.smtp.starttls.enable", String.valueOf(STARTTLS_ENABLE));
        properties.setProperty("mail.smtp.starttls.required", "true");
        properties.setProperty("mail.smtp.ssl.enable", String.valueOf(SSL_ENABLE));
        properties.setProperty("mail.smtp.timeout", String.valueOf(TIMEOUT));
        properties.put("mail.smtp.socketFactory.port", PORT);
        properties.put("mail.smtp.socketFactory.class", SOCKET_FACTORY_CLASS);
        mailSender.setJavaMailProperties(properties);
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(USERNAME);
        helper.setTo(to);
        // 设置抄送人列表
        if (cc != null) {
            helper.setCc(cc.toArray(new String[0]));
        }
        helper.setSubject(subject);

        if (filePaths != null && !filePaths.isEmpty()) {
            for (String filePath : filePaths) {
                FileDataSource file = new FileDataSource(filePath);
                helper.addAttachment(file.getName(), file);
            }
        }
        helper.setText(content, true);
        // 罪魁祸首
        message.setText(content, "utf-8", "html");
        mailSender.send(message);
    }
}

因为初始的发送会产生乱码,为了避免出现乱码的问题,笔者设置了文件的格式以及编码,效果立竿见影

message.setText(content, "utf-8", "html"); 

没使用之前 

 

 使用后

 乱码问题得到了解决

之前一直没有上传文件和设置抄送人,既然可以正常发送 抄送和附件也一并测试

重头戏来了

试了N遍之后,发现只能抄送没有附件信息!

 

 做了好多无用的操作后,才发现带附件的设置编码格式需要在↓设置!都是不熟悉api吃的亏啊!

MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); 

正确的代码如下

/**
     * 发送邮件
     *
     * @param to        收件人
     * @param cc        抄送人列表
     * @param subject   主体
     * @param content   内容
     * @param filePaths 文件路径列表
     * @throws IOException
     * @throws MessagingException
     */
    public static void sendMail(String to, List<String> cc, String subject, String content, List<String> filePaths) throws IOException, MessagingException {
        if (to == null) {
            throw new ServiceException("收件人不能为空!");
        }
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(HOST);
        mailSender.setUsername(USERNAME);
        mailSender.setPort(PORT);
        mailSender.setPassword(PASSWORD);
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", String.valueOf(AUTH));
        properties.setProperty("mail.smtp.starttls.enable", String.valueOf(STARTTLS_ENABLE));
        properties.setProperty("mail.smtp.starttls.required", "true");
        properties.setProperty("mail.smtp.ssl.enable", String.valueOf(SSL_ENABLE));
        properties.setProperty("mail.smtp.timeout", String.valueOf(TIMEOUT));
        properties.put("mail.smtp.socketFactory.port", PORT);
        properties.put("mail.smtp.socketFactory.class", SOCKET_FACTORY_CLASS);
        mailSender.setJavaMailProperties(properties);
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setFrom(USERNAME);
        helper.setTo(to);
        // 设置抄送人列表
        if (cc != null) {
            helper.setCc(cc.toArray(new String[0]));
        }
        helper.setSubject(subject);

        if (filePaths != null && !filePaths.isEmpty()) {
            for (String filePath : filePaths) {
                File file = new File(filePath);
                FileSystemResource fileSystemResource = new FileSystemResource(file);
                helper.addAttachment(file.getName(), fileSystemResource);
            }
        }
        helper.setText(content, true);
        mailSender.send(message);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值