You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Math_Learning/src/Base/Email_settings.java

87 lines
2.6 KiB

package Base;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Email_settings {
private static final String CONFIG_FILE = "config/email.properties";
private static Properties properties;
private static long lastModified = 0;
static {
loadConfig();
}
private static void loadConfig() {
properties = new Properties();
setDefaultProperties();
File configFile = new File(CONFIG_FILE);
if (configFile.exists()) {
try (FileInputStream input = new FileInputStream(configFile)) {
properties.load(input);
lastModified = configFile.lastModified();
} catch (IOException e) {
System.err.println("加载邮箱配置文件失败,使用默认配置: " + e.getMessage());
}
} else {
createDefaultConfigFile();
}
}
private static void setDefaultProperties() {
properties.setProperty("smtp.host", "smtp.qq.com");
properties.setProperty("smtp.port", "587");
properties.setProperty("from.email", "835981889@qq.com");
properties.setProperty("email.password", "fpqfprqznbvdbcdf");
properties.setProperty("ssl.enable", "true");
}
private static void createDefaultConfigFile() {
File configDir = new File("config");
if (!configDir.exists()) {
configDir.mkdirs();
}
try (FileOutputStream output = new FileOutputStream(CONFIG_FILE)) {
properties.store(output, "Settings");
} catch (IOException e) {
System.err.println("创建默认配置文件失败: " + e.getMessage());
}
}
private static void checkForUpdates() {
File configFile = new File(CONFIG_FILE);
if (configFile.exists() && configFile.lastModified() > lastModified) {
loadConfig();
}
}
public static String getSmtpHost() {
checkForUpdates();
return properties.getProperty("smtp.host");
}
public static String getSmtpPort() {
checkForUpdates();
return properties.getProperty("smtp.port");
}
public static String getFromEmail() {
checkForUpdates();
return properties.getProperty("from.email");
}
public static String getEmailPassword() {
checkForUpdates();
return properties.getProperty("email.password");
}
public static boolean isSslEnable() {
checkForUpdates();
return Boolean.parseBoolean(properties.getProperty("ssl.enable"));
}
}