fix 修复 邮件工具失效问题

master
疯狂的狮子Li 3 years ago
parent 2ac0468288
commit 9c22f7a0d0

@ -26,6 +26,10 @@
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>jakarta.mail</artifactId>
</dependency>
</dependencies>
</project>

@ -1,7 +1,7 @@
package org.dromara.common.mail.config;
import cn.hutool.extra.mail.MailAccount;
import org.dromara.common.mail.config.properties.MailProperties;
import org.dromara.common.mail.utils.MailAccount;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@ -0,0 +1,47 @@
package org.dromara.common.mail.utils;
import cn.hutool.core.io.IORuntimeException;
/**
* {@link MailAccount#MAIL_SETTING_PATHS}
*
* @author looly
*
*/
public enum GlobalMailAccount {
INSTANCE;
private final MailAccount mailAccount;
/**
*
*/
GlobalMailAccount() {
mailAccount = createDefaultAccount();
}
/**
*
*
* @return
*/
public MailAccount getAccount() {
return this.mailAccount;
}
/**
*
*
* @return MailAccount
*/
private MailAccount createDefaultAccount() {
for (String mailSettingPath : MailAccount.MAIL_SETTING_PATHS) {
try {
return new MailAccount(mailSettingPath);
} catch (IORuntimeException ignore) {
//ignore
}
}
return null;
}
}

@ -1,7 +1,6 @@
package org.dromara.common.mail.utils;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.extra.mail.MailException;
import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeUtility;

@ -7,7 +7,6 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.*;
import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta.activation.FileDataSource;

@ -0,0 +1,657 @@
package org.dromara.common.mail.utils;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.Setting;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
*
*
* @author Luxiaolei
*/
public class MailAccount implements Serializable {
private static final long serialVersionUID = -6937313421815719204L;
private static final String MAIL_PROTOCOL = "mail.transport.protocol";
private static final String SMTP_HOST = "mail.smtp.host";
private static final String SMTP_PORT = "mail.smtp.port";
private static final String SMTP_AUTH = "mail.smtp.auth";
private static final String SMTP_TIMEOUT = "mail.smtp.timeout";
private static final String SMTP_CONNECTION_TIMEOUT = "mail.smtp.connectiontimeout";
private static final String SMTP_WRITE_TIMEOUT = "mail.smtp.writetimeout";
// SSL
private static final String STARTTLS_ENABLE = "mail.smtp.starttls.enable";
private static final String SSL_ENABLE = "mail.smtp.ssl.enable";
private static final String SSL_PROTOCOLS = "mail.smtp.ssl.protocols";
private static final String SOCKET_FACTORY = "mail.smtp.socketFactory.class";
private static final String SOCKET_FACTORY_FALLBACK = "mail.smtp.socketFactory.fallback";
private static final String SOCKET_FACTORY_PORT = "smtp.socketFactory.port";
// System Properties
private static final String SPLIT_LONG_PARAMS = "mail.mime.splitlongparameters";
//private static final String ENCODE_FILE_NAME = "mail.mime.encodefilename";
//private static final String CHARSET = "mail.mime.charset";
// 其他
private static final String MAIL_DEBUG = "mail.debug";
public static final String[] MAIL_SETTING_PATHS = new String[]{"config/mail.setting", "config/mailAccount.setting", "mail.setting"};
/**
* SMTP
*/
private String host;
/**
* SMTP
*/
private Integer port;
/**
*
*/
private Boolean auth;
/**
*
*/
private String user;
/**
*
*/
private String pass;
/**
* RFC-822
*/
private String from;
/**
*
*/
private boolean debug;
/**
*
*/
private Charset charset = CharsetUtil.CHARSET_UTF_8;
/**
* false
*/
private boolean splitlongparameters = false;
/**
* 使{@link #charset} {@code true}
*/
private boolean encodefilename = true;
/**
* 使 STARTTLSSTARTTLSTLSSSL 使
*/
private boolean starttlsEnable = false;
/**
* 使 SSL
*/
private Boolean sslEnable;
/**
* SSL
*/
private String sslProtocols;
/**
* javax.net.SocketFactory,SMTP
*/
private String socketFactoryClass = "javax.net.ssl.SSLSocketFactory";
/**
* true,使使java.net.Socket, true
*/
private boolean socketFactoryFallback;
/**
* 使,使
*/
private int socketFactoryPort = 465;
/**
* SMTP
*/
private long timeout;
/**
* Socket
*/
private long connectionTimeout;
/**
* Socket
*/
private long writeTimeout;
/**
*
*/
private final Map<String, Object> customProperty = new HashMap<>();
// -------------------------------------------------------------- Constructor start
/**
* ,
*/
public MailAccount() {
}
/**
*
*
* @param settingPath
*/
public MailAccount(String settingPath) {
this(new Setting(settingPath));
}
/**
*
*
* @param setting
*/
public MailAccount(Setting setting) {
setting.toBean(this);
}
// -------------------------------------------------------------- Constructor end
/**
* SMTP
*
* @return SMTP
*/
public String getHost() {
return host;
}
/**
* SMTP
*
* @param host SMTP
* @return this
*/
public MailAccount setHost(String host) {
this.host = host;
return this;
}
/**
* SMTP
*
* @return SMTP
*/
public Integer getPort() {
return port;
}
/**
* SMTP
*
* @param port SMTP
* @return this
*/
public MailAccount setPort(Integer port) {
this.port = port;
return this;
}
/**
*
*
* @return
*/
public Boolean isAuth() {
return auth;
}
/**
*
*
* @param isAuth
* @return this
*/
public MailAccount setAuth(boolean isAuth) {
this.auth = isAuth;
return this;
}
/**
*
*
* @return
*/
public String getUser() {
return user;
}
/**
*
*
* @param user
* @return this
*/
public MailAccount setUser(String user) {
this.user = user;
return this;
}
/**
*
*
* @return
*/
public String getPass() {
return pass;
}
/**
*
*
* @param pass
* @return this
*/
public MailAccount setPass(String pass) {
this.pass = pass;
return this;
}
/**
* RFC-822
*
* @return RFC-822
*/
public String getFrom() {
return from;
}
/**
* RFC-822<br>
*
*
* <pre>
* 1. user@xxx.xx
* 2. name &lt;user@xxx.xx&gt;
* </pre>
*
* @param from RFC-822
* @return this
*/
public MailAccount setFrom(String from) {
this.from = from;
return this;
}
/**
*
*
* @return
* @since 4.0.2
*/
public boolean isDebug() {
return debug;
}
/**
*
*
* @param debug
* @return this
* @since 4.0.2
*/
public MailAccount setDebug(boolean debug) {
this.debug = debug;
return this;
}
/**
*
*
* @return {@code null}
*/
public Charset getCharset() {
return charset;
}
/**
* {@code null}
* <pre>
* System.setProperty("mail.mime.charset", charset);
* </pre>
*
* @param charset {@code null} 使mail.mime.charset
* @return this
*/
public MailAccount setCharset(Charset charset) {
this.charset = charset;
return this;
}
/**
* false
*
* @return
*/
public boolean isSplitlongparameters() {
return splitlongparameters;
}
/**
* false<br>
*
* <pre>
* System.setProperty("mail.mime.splitlongparameters", true)
* </pre>
*
* @param splitlongparameters
*/
public void setSplitlongparameters(boolean splitlongparameters) {
this.splitlongparameters = splitlongparameters;
}
/**
* 使{@link #charset} {@code true}
*
* @return 使{@link #charset} {@code true}
* @since 5.7.16
*/
public boolean isEncodefilename() {
return encodefilename;
}
/**
* 使{@link #charset}<br>
* {@code false}
* <ul>
* <li>mail.mime.encodefilename </li>
* <li>mail.mime.charset </li>
* </ul>
*
* @param encodefilename 使{@link #charset}
* @since 5.7.16
*/
public void setEncodefilename(boolean encodefilename) {
this.encodefilename = encodefilename;
}
/**
* 使 STARTTLSSTARTTLSTLSSSL 使
*
* @return 使 STARTTLS
*/
public boolean isStarttlsEnable() {
return this.starttlsEnable;
}
/**
* 使STARTTLSSTARTTLSTLSSSL 使
*
* @param startttlsEnable 使STARTTLS
* @return this
*/
public MailAccount setStarttlsEnable(boolean startttlsEnable) {
this.starttlsEnable = startttlsEnable;
return this;
}
/**
* 使 SSL
*
* @return 使 SSL
*/
public Boolean isSslEnable() {
return this.sslEnable;
}
/**
* 使SSL
*
* @param sslEnable 使SSL
* @return this
*/
public MailAccount setSslEnable(Boolean sslEnable) {
this.sslEnable = sslEnable;
return this;
}
/**
* SSL
*
* @return SSL
* @since 5.5.7
*/
public String getSslProtocols() {
return sslProtocols;
}
/**
* SSL
*
* @param sslProtocols SSL
* @since 5.5.7
*/
public void setSslProtocols(String sslProtocols) {
this.sslProtocols = sslProtocols;
}
/**
* javax.net.SocketFactory,SMTP
*
* @return javax.net.SocketFactory, SMTP
*/
public String getSocketFactoryClass() {
return socketFactoryClass;
}
/**
* javax.net.SocketFactory,SMTP
*
* @param socketFactoryClass javax.net.SocketFactory,SMTP
* @return this
*/
public MailAccount setSocketFactoryClass(String socketFactoryClass) {
this.socketFactoryClass = socketFactoryClass;
return this;
}
/**
* true,使使java.net.Socket, true
*
* @return true, 使使java.net.Socket, true
*/
public boolean isSocketFactoryFallback() {
return socketFactoryFallback;
}
/**
* true,使使java.net.Socket, true
*
* @param socketFactoryFallback true,使使java.net.Socket, true
* @return this
*/
public MailAccount setSocketFactoryFallback(boolean socketFactoryFallback) {
this.socketFactoryFallback = socketFactoryFallback;
return this;
}
/**
* 使,使
*
* @return 使,使
*/
public int getSocketFactoryPort() {
return socketFactoryPort;
}
/**
* 使,使
*
* @param socketFactoryPort 使,使
* @return this
*/
public MailAccount setSocketFactoryPort(int socketFactoryPort) {
this.socketFactoryPort = socketFactoryPort;
return this;
}
/**
* SMTP
*
* @param timeout SMTP
* @return this
* @since 4.1.17
*/
public MailAccount setTimeout(long timeout) {
this.timeout = timeout;
return this;
}
/**
* Socket
*
* @param connectionTimeout Socket
* @return this
* @since 4.1.17
*/
public MailAccount setConnectionTimeout(long connectionTimeout) {
this.connectionTimeout = connectionTimeout;
return this;
}
/**
* Socket
*
* @param writeTimeout Socket
* @return this
* @since 5.8.3
*/
public MailAccount setWriteTimeout(long writeTimeout) {
this.writeTimeout = writeTimeout;
return this;
}
/**
*
*
* @return
* @since 5.6.4
*/
public Map<String, Object> getCustomProperty() {
return customProperty;
}
/**
* mail.smtp.ssl.socketFactory
*
* @param key
* @param value null
* @return this
* @since 5.6.4
*/
public MailAccount setCustomProperty(String key, Object value) {
if (StrUtil.isNotBlank(key) && ObjectUtil.isNotNull(value)) {
this.customProperty.put(key, value);
}
return this;
}
/**
* SMTP
*
* @return {@link Properties}
*/
public Properties getSmtpProps() {
//全局系统参数
System.setProperty(SPLIT_LONG_PARAMS, String.valueOf(this.splitlongparameters));
final Properties p = new Properties();
p.put(MAIL_PROTOCOL, "smtp");
p.put(SMTP_HOST, this.host);
p.put(SMTP_PORT, String.valueOf(this.port));
p.put(SMTP_AUTH, String.valueOf(this.auth));
if (this.timeout > 0) {
p.put(SMTP_TIMEOUT, String.valueOf(this.timeout));
}
if (this.connectionTimeout > 0) {
p.put(SMTP_CONNECTION_TIMEOUT, String.valueOf(this.connectionTimeout));
}
// issue#2355
if (this.writeTimeout > 0) {
p.put(SMTP_WRITE_TIMEOUT, String.valueOf(this.writeTimeout));
}
p.put(MAIL_DEBUG, String.valueOf(this.debug));
if (this.starttlsEnable) {
//STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接TLS或SSL 而不是使用一个单独的加密通信端口。
p.put(STARTTLS_ENABLE, "true");
if (null == this.sslEnable) {
//为了兼容旧版本当用户没有此项配置时按照starttlsEnable开启状态时对待
this.sslEnable = true;
}
}
// SSL
if (null != this.sslEnable && this.sslEnable) {
p.put(SSL_ENABLE, "true");
p.put(SOCKET_FACTORY, socketFactoryClass);
p.put(SOCKET_FACTORY_FALLBACK, String.valueOf(this.socketFactoryFallback));
p.put(SOCKET_FACTORY_PORT, String.valueOf(this.socketFactoryPort));
// issue#IZN95@Gitee在Linux下需自定义SSL协议版本
if (StrUtil.isNotBlank(this.sslProtocols)) {
p.put(SSL_PROTOCOLS, this.sslProtocols);
}
}
// 补充自定义属性,允许自定属性覆盖已经设置的值
p.putAll(this.customProperty);
return p;
}
/**
* null使
*
* @return this
*/
public MailAccount defaultIfEmpty() {
// 去掉发件人的姓名部分
final String fromAddress = InternalMailUtil.parseFirstAddress(this.from, this.charset).getAddress();
if (StrUtil.isBlank(this.host)) {
// 如果SMTP地址为空默认使用smtp.<发件人邮箱后缀>
this.host = StrUtil.format("smtp.{}", StrUtil.subSuf(fromAddress, fromAddress.indexOf('@') + 1));
}
if (StrUtil.isBlank(user)) {
// 如果用户名为空默认为发件人issue#I4FYVY@Gitee
//this.user = StrUtil.subPre(fromAddress, fromAddress.indexOf('@'));
this.user = fromAddress;
}
if (null == this.auth) {
// 如果密码非空白,则使用认证模式
this.auth = (false == StrUtil.isBlank(this.pass));
}
if (null == this.port) {
// 端口在SSL状态下默认与socketFactoryPort一致非SSL状态下默认为25
this.port = (null != this.sslEnable && this.sslEnable) ? this.socketFactoryPort : 25;
}
if (null == this.charset) {
// 默认UTF-8编码
this.charset = CharsetUtil.CHARSET_UTF_8;
}
return this;
}
@Override
public String toString() {
return "MailAccount [host=" + host + ", port=" + port + ", auth=" + auth + ", user=" + user + ", pass=" + (StrUtil.isEmpty(this.pass) ? "" : "******") + ", from=" + from + ", startttlsEnable="
+ starttlsEnable + ", socketFactoryClass=" + socketFactoryClass + ", socketFactoryFallback=" + socketFactoryFallback + ", socketFactoryPort=" + socketFactoryPort + "]";
}
}

@ -0,0 +1,36 @@
package org.dromara.common.mail.utils;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
/**
*
* @author xiaoleilu
*/
public class MailException extends RuntimeException{
private static final long serialVersionUID = 8247610319171014183L;
public MailException(Throwable e) {
super(ExceptionUtil.getMessage(e), e);
}
public MailException(String message) {
super(message);
}
public MailException(String messageTemplate, Object... params) {
super(StrUtil.format(messageTemplate, params));
}
public MailException(String message, Throwable throwable) {
super(message, throwable);
}
public MailException(String message, Throwable throwable, boolean enableSuppression, boolean writableStackTrace) {
super(message, throwable, enableSuppression, writableStackTrace);
}
public MailException(Throwable throwable, String messageTemplate, Object... params) {
super(StrUtil.format(messageTemplate, params), throwable);
}
}

@ -5,7 +5,6 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.*;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import lombok.AccessLevel;

Loading…
Cancel
Save