Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4e6ee17
fix: align portable Hitool behavior and tests with 3.0.x
loong10k Oct 31, 2026
1558f2a
fix: align portable Hitool behavior and tests with 3.0.x
loong10k Oct 31, 2026
2692497
fix: align portable Hitool behavior and tests with 3.0.x
loong10k Oct 31, 2026
af33389
fix: align portable Hitool behavior and tests with 3.0.x
loong10k Oct 31, 2026
95625d8
fix: align portable Hitool behavior and tests with 3.0.x
loong10k Oct 31, 2026
3beb179
fix: align portable Hitool behavior and tests with 3.0.x
loong10k Oct 31, 2026
c0f1ee6
fix: align portable Hitool behavior and tests with 3.0.x
loong10k Oct 31, 2026
0cc98d7
fix: align portable Hitool behavior and tests with 3.0.x
loong10k Oct 31, 2026
70a6c0d
fix: align portable Hitool behavior and tests with 3.0.x
loong10k Oct 31, 2026
c3d2d1c
build: package file type signatures as runtime resource
loong10k Oct 31, 2026
68464b7
build: remove unbundled file type signatures
loong10k Oct 31, 2026
8123971
test: add loopback SMTP server for mail client tests
loong10k Oct 31, 2026
71d94eb
test: verify mail delivery against local SMTP server
loong10k Oct 31, 2026
5d16556
test: verify mail delivery against local SMTP server
loong10k Oct 31, 2026
0e6094b
build: retain Spring Mail runtime dependencies
loong10k Oct 31, 2026
d76fe3a
fix: avoid passing empty recipient lists to Commons Email
loong10k Oct 31, 2026
b6fbb89
build: provide Jakarta Activation implementation for MIME mail
loong10k Oct 31, 2026
fade9c7
test: stop mutating system mail settings and use local SMTP
loong10k Oct 31, 2026
161f3a8
test: set Jakarta Mail SMTP transport port
loong10k Oct 31, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,36 +244,25 @@ public static void setProperty(Object bean, String name, Object value,
String name3 = matcher.group(1);
int index = Integer.parseInt(matcher.group(2));
System.out.println(matcher.group(0));
Object value3 = BeanUtils.getProperty(propertyVlaue, name3);
if (value3 != null) {
if (value3 instanceof Collection) {
ArrayList<Object> values = new ArrayList<Object>();
Iterator<Object> items = ((Collection<Object>) value).iterator();
while (items.hasNext()) {
Object item = items.next();
if (item == null) {
values.add(null);
} else {
values.add(item);
}
}
BeanUtils.setProperty(propertyVlaue, name3,values);
} else if (value.getClass().isArray()) {
int length = Array.getLength(value);
Object[] results = new Object[length];
for (int i = 0; i < length; i++) {
Object item = Array.get(value, i);
if (item == null) {
results[i] = null;
} else {
results[i] = item;
}
}
BeanUtils.setProperty(propertyVlaue, name3,results);
} else {
BeanUtils.setProperty(propertyVlaue, name3,value);
Object current = getPropertyUtils().getProperty(propertyVlaue, name3);
Class<?> fieldType = getPropertyUtils().getPropertyType(propertyVlaue, name3);
if (fieldType != null && fieldType.isArray()) {
int length = current == null ? 0 : Array.getLength(current);
Object array = Array.newInstance(fieldType.getComponentType(), Math.max(length, index + 1));
if (length > 0) {
System.arraycopy(current, 0, array, 0, length);
}
}
Array.set(array, index, value);
getPropertyUtils().setProperty(propertyVlaue, name3, array);
} else if (current instanceof List) {
List<Object> list = (List<Object>) current;
while (list.size() <= index) {
list.add(null);
}
list.set(index, value);
} else {
BeanUtils.setProperty(propertyVlaue, name2, value);
}
} else {
BeanUtils.setProperty(propertyVlaue,name2,value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static void compress(File file) throws IOException {
* @throws Exception
*/
public static void compress(File srcFile, boolean delete) throws IOException {
File destFile = new File(srcFile.getParentFile(), FilenameUtils.getBaseName(srcFile.getName()) + EXT);
File destFile = new File(srcFile.getParentFile(), srcFile.getName() + EXT);
// 压缩
BZip2Utils.compress(srcFile, destFile);
if (delete) {
Expand Down Expand Up @@ -175,7 +175,7 @@ public static void decompress(File srcFile, boolean delete) throws IOException {
}

public static void decompress(File srcFile, File destDir) throws IOException {
File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.getName()) + EXT);
File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.getName()));
try(InputStream input = new FileInputStream(srcFile);
OutputStream output = new FileOutputStream(destFile);) {
BZip2Utils.decompress(input, output);
Expand Down
4 changes: 2 additions & 2 deletions hitool-core/src/main/java/hitool/core/compress/GZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void compress(File file) throws IOException {
* @throws Exception
*/
public static void compress(File srcFile, boolean delete) throws IOException {
File destFile = new File(srcFile.getParentFile(), FilenameUtils.getBaseName(srcFile.getName()) + EXT);
File destFile = new File(srcFile.getParentFile(), srcFile.getName() + EXT);
// 压缩
GZipUtils.compress(srcFile, destFile);
if (delete) {
Expand Down Expand Up @@ -193,7 +193,7 @@ public static void decompress(File srcFile, boolean delete) throws IOException {
}

public static void decompress(File srcFile, File destDir) throws IOException {
File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.getName()) + EXT);
File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.getName()));
try (InputStream input = new FileInputStream(srcFile);
OutputStream output = new FileOutputStream(destFile);){
GZipUtils.decompress(input, output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ public static String getHostMacAddress(String host) {
// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
byte[] mac = null;
try {
mac = NetworkInterface.getByInetAddress(InetAddress.getByName(host)).getHardwareAddress();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getByName(host));
if (networkInterface == null) {
return null;
}
mac = networkInterface.getHardwareAddress();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void testUtils() {
BeanPropertyUtils.populate(entity, source, true);
// System.out.println(entity.getMethodName());
System.out.println("===========================RESULT================================");
System.out.println(entity.getProxyBean().getArray()[0]);
System.out.println("proxyBean.proxyBean.methodName:"+entity.getProxyBean().getProxyBean().getMethodName());
org.junit.Assert.assertEquals("测试名称1", entity.getProxyBean().getArray()[1]);
org.junit.Assert.assertEquals("测试名称1", entity.getProxyBean().getProxyBean().getMethodName());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ public final void testDataCompress() throws Exception {
@Test
public final void testFileCompress() throws Exception {

FileOutputStream fos = new FileOutputStream("d:/f.txt");
File inputFile = File.createTempFile("hitool-compress-", ".txt");
inputFile.deleteOnExit();
new File(inputFile.getAbsolutePath() + ".bz2").deleteOnExit();
FileOutputStream fos = new FileOutputStream(inputFile);

fos.write(inputStr.getBytes());
fos.flush();
fos.close();

BZip2Utils.compress("d:/f.txt");
BZip2Utils.compress(inputFile.getAbsolutePath());

BZip2Utils.decompress("d:/f.txt.bz2");
BZip2Utils.decompress(inputFile.getAbsolutePath() + ".bz2");

File file = new File("d:/f.txt");
File file = new File(inputFile.getAbsolutePath());

FileInputStream fis = new FileInputStream(file);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ public final void testDataCompress() throws Exception {
@Test
public final void testFileCompress() throws Exception {

FileOutputStream fos = new FileOutputStream("d:/f.txt");
File inputFile = File.createTempFile("hitool-compress-", ".txt");
inputFile.deleteOnExit();
new File(inputFile.getAbsolutePath() + ".gz").deleteOnExit();
FileOutputStream fos = new FileOutputStream(inputFile);

fos.write(inputStr.getBytes());
fos.flush();
fos.close();

GZipUtils.compress("d:/f.txt", false);
GZipUtils.compress(inputFile.getAbsolutePath(), false);

GZipUtils.decompress("d:/f.txt.gz", false);
GZipUtils.decompress(inputFile.getAbsolutePath() + ".gz", false);

File file = new File("d:/f.txt");
File file = new File(inputFile.getAbsolutePath());

FileInputStream fis = new FileInputStream(file);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class FiletypeUtils_Test extends TestCase {
public void testname() throws Exception {


assertSame(FiletypeUtils.getFileType(new File("D://cos.jar")), "jar");
byte[] jarHeader = new byte[] {0x50, 0x4B, 0x03, 0x04, 0x0A, 0, 0, 0, 0, 0, 0};
assertEquals("jar", FiletypeUtils.getFileType(jarHeader));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void testRemoteMacAddr_4() {
}

public void testRemoteMacAddr_3() {
System.out.println("HostMacAddress is : " + MacAddressUtils.getHostMacAddress("192.168.31.1"));
assertNull(MacAddressUtils.getHostMacAddress("127.0.0.1"));
}

}
16 changes: 6 additions & 10 deletions hitool-mail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-activation</artifactId>
<version>2.0.2</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.mail/jakarta.mail -->
<dependency>
<groupId>com.sun.mail</groupId>
Expand Down Expand Up @@ -81,16 +87,6 @@
<artifactId>spring-context-support</artifactId>
<optional>true</optional>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected void sendMail(EmailBody email,boolean isHtml) throws Exception{
}

// 设置收件人的名片和地址
if(email.getMailto() != null){
if(email.getMailto() != null && !email.getMailto().isEmpty()){
List<String> mailtoList = new ArrayList<String>();
for (String mailto : email.getMailto().keySet()) {
// 添加一个收件人
Expand All @@ -138,7 +138,7 @@ protected void sendMail(EmailBody email,boolean isHtml) throws Exception{
}

// 设置抄送的名片和地址
if(email.getMailcc() != null){
if(email.getMailcc() != null && !email.getMailcc().isEmpty()){
List<String> mailccList = new ArrayList<String>();
for (String mailcc : email.getMailcc().keySet()) {
// 添加一个抄送
Expand All @@ -149,7 +149,7 @@ protected void sendMail(EmailBody email,boolean isHtml) throws Exception{
}

// 设置密送的名片和地址
if(email.getMailBcc() != null){
if(email.getMailBcc() != null && !email.getMailBcc().isEmpty()){
List<String> mailBccList = new ArrayList<String>();
for (String mailcc : email.getMailBcc().keySet()) {
// 添加一个密送
Expand Down
128 changes: 32 additions & 96 deletions hitool-mail/src/test/java/hitool/mail/CommonsMailClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,47 @@

import hitool.mail.conf.EmailBody;
import hitool.mail.def.CommonsMailClientImpl;
import hitool.mail.provider.EmailPropertiesProvider;
import hitool.mail.provider.def.DefaultEmailPropertiesProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Properties;

import static org.junit.Assert.assertTrue;

public class CommonsMailClientTest {
private LocalSmtpServer smtp;
private CommonsMailClientImpl mailClient;

protected Properties props = System.getProperties();
protected EmailPropertiesProvider propsProvider;
protected JavaMailClientAdapter mailClient;

@Before
public void setup(){


/*
* 我们是没办法使用javamail通过一般的代理服务器发送邮件的,比如下面的代码是没有效果的:
*/
/*Properties props = System.getProperties();
props.setProperty("proxySet","true");
props.setProperty("ProxyHost","192.168.155.1");
props.setProperty("ProxyPort","1080");*/
//或者这样,也是没用
/*Properties props = System.getProperties();
props.setProperty("proxySet","true");
props.setProperty("http.proxyHost","192.168.155.1");
props.setProperty("http.proxyPort","808");*/
@Before
public void setup() throws Exception {
smtp = new LocalSmtpServer();
Properties props = new Properties();
props.setProperty(JavaMailKey.MAIL_HOST, "127.0.0.1");
props.setProperty(JavaMailKey.MAIL_PORT, Integer.toString(smtp.port()));
props.setProperty(JavaMailKey.MAIL_USER, "sender@example.test");
props.setProperty(JavaMailKey.MAIL_SMTP_AUTH, "false");
props.setProperty(JavaMailKey.MAIL_SMTP_TIMEOUT, "5");
DefaultEmailPropertiesProvider provider = new DefaultEmailPropertiesProvider();
provider.setProps(props);
mailClient = new CommonsMailClientImpl();
mailClient.setPropsProvider(provider);
}

/*
* 不过可以通过socks网关来访问外网的email服务器,当然,前提是你安装了一个socks服务器
* 此处使用ccproxy代理测试
*/

// 设置代理服务器
props.setProperty("proxySet", "true");
props.setProperty("socksProxyHost", "10.71.19.215");
props.setProperty("socksProxyPort", "1080");

//props.setProperty("mail.smtp.host", "smtp.163.com");
//props.setProperty("mail.smtp.port", "465");


props.put(JavaMailKey.MAIL_DEBUG, "true");
props.setProperty(JavaMailKey.MAIL_HOST, "smtp.163.com");
props.setProperty(JavaMailKey.MAIL_PORT, "25");
//props.setProperty(JavaMailKey.MAIL_USER, "xxx@163.com");
//props.setProperty(JavaMailKey.MAIL_PASSWORD, "xxx");
props.setProperty(JavaMailKey.MAIL_USER, "xxx@163.com");
props.setProperty(JavaMailKey.MAIL_PASSWORD, "xxx");
//commons-mail需要设置mail.smtp.user和mail.smtp.password,才能正常发送邮件
props.put(JavaMailKey.MAIL_SMTP_USER, "xxx@163.com");
props.put(JavaMailKey.MAIL_SMTP_PASSWORD, "xxx");
props.put(JavaMailKey.MAIL_SMTP_AUTH, "true");


props.put(JavaMailKey.MAIL_SMTP_AUTH, "true");
// props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
// props.setProperty("mail.smtp.socketFactory.fallback", "false");
// props.setProperty("mail.smtp.socketFactory.port", "465");

// props.put("mail.store.protocol", "pop3");
// props.put("mail.transport.protocol", "smtp");

mailClient = new CommonsMailClientImpl();


propsProvider = new DefaultEmailPropertiesProvider();

propsProvider.setProps(props);

mailClient.setPropsProvider(propsProvider);


}

@Test
public void testDelete() throws Exception{

//构建邮件对象
EmailBody email = new EmailBody();

// 设置发件人信息
email.setFrom("xxx-消息中心", props.getProperty(JavaMailKey.MAIL_USER), false);
@Test
public void sendsMimeMessageToLocalServer() throws Exception {
EmailBody email = new EmailBody();
email.setFrom("Sender", "sender@example.test", false);
email.setMailto("Recipient", "recipient@example.test");
email.setSubject("Mail client test");
email.setContent("Hello from Hitool");
assertTrue(mailClient.sendMime(email));
assertTrue(smtp.awaitMessage().contains("recipient@example.test"));
}

// 设置收件人的邮箱
String sendTo = "xxx@qq.com";
email.setMailto(sendTo, sendTo);

// 设置邮件标题
email.setSubject("邮件客户端测试");
// 设置邮件的内容体
String content = "如果您收到此邮件说明测试成功。";
email.setContent(content);

mailClient.sendMime(email);


}

@After
public void shutdown(){

}

@After
public void shutdown() throws Exception {
smtp.close();
}
}
Loading
Loading