1.javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed的异常:
解决方案:
在执行邮件发送前,添加下面这一行代码,亲测有效
设置当前线程的上下文类加载器为 javax.mail.Message 类的类加载器。 在某些情况下,特别是在使用 JavaMail API 或其他扩展类库时,设置上下文类加载器可能是必要的,以确保正确加载所需的类和资源。
Thread.currentThread().setContextClassLoader(javax.mail.Message.class.getClassLoader());
2. Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.dm.aliyun.com, port: 465;的异常:
解决方案:
修改邮件发送时SSL的protocol就可以了
MailSSLSocketFactory sf = new MailSSLSocketFactory();
//设置信任所有的主机
sf.setTrustAllHosts(true);
//连接邮件服务器的参数配置
Properties props = new Properties();
//开启tls
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);