diff --git a/src/main/java/com/zhangmeng/tools/controller/HomeController.java b/src/main/java/com/zhangmeng/tools/controller/HomeController.java index e46de78..8e0a170 100644 --- a/src/main/java/com/zhangmeng/tools/controller/HomeController.java +++ b/src/main/java/com/zhangmeng/tools/controller/HomeController.java @@ -434,4 +434,9 @@ public class HomeController implements Serializable { ListView listView = (ListView) fx.lookup("#listView"); listView.getSelectionModel().select(index); } + + @FXML + public void mail_menu_item(ActionEvent event) { + load_small_tools(7); + } } \ No newline at end of file diff --git a/src/main/java/com/zhangmeng/tools/controller/MailController.java b/src/main/java/com/zhangmeng/tools/controller/MailController.java new file mode 100644 index 0000000..d14919a --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/controller/MailController.java @@ -0,0 +1,189 @@ +package com.zhangmeng.tools.controller; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.resource.Resource; +import cn.hutool.core.io.resource.ResourceUtil; +import cn.hutool.extra.mail.MailUtil; +import cn.hutool.setting.Setting; +import com.zhangmeng.tools.utils.AlertUtils; +import com.zhangmeng.tools.utils.ImagePath; +import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.scene.control.*; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.stage.FileChooser; +import javafx.stage.Stage; +import lombok.extern.slf4j.Slf4j; + +import java.io.File; + + +/** + * @author : 芊芊墨客 + * @version : 1.0 + * @date : 2023-03-09 16:24 + */ +@Slf4j +public class MailController { + + @FXML + public TextField host; + + @FXML + public TextField port; + + @FXML + public TextField from; + + @FXML + public TextField user; + + @FXML + public TextField pass; + + @FXML + public TextArea content; + + @FXML + public Button send; + + @FXML + public ListView user_list; + + @FXML + public TextField to; + + @FXML + public Button add_user_to; + + @FXML + public TextField file_path; + + @FXML + public Button choose_file; + + public static int mail_port = 25; + + public ObservableList list = FXCollections.observableArrayList(); + + public static final String p = "smtp"; + + private final Setting setting = new Setting("config/mail.setting"); + + private SimpleObjectProperty file_info = new SimpleObjectProperty<>(); + + @FXML + public void initialize() { + + choose_file.setText(null); + ImageView iv = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE))); + iv.setPreserveRatio(true); + iv.setFitWidth(18); + choose_file.setGraphic(iv); + + choose_file.setOnAction(event -> { + Stage stage = new Stage(); + FileChooser fc = new FileChooser(); + fc.setTitle("单选文件"); + fc.getExtensionFilters().addAll( + new FileChooser.ExtensionFilter("所有类型", "*.*") + ); + + File file = fc.showOpenDialog(stage); + + if (file == null) { + return; + } + file_info.setValue(file); + }); + + setting.autoLoad(true); + String main_host = setting.get("host"); + String main_port = setting.get("port"); + String main_from = setting.get("from"); + String main_user = setting.get("user"); + String main_pass = setting.get("pass"); + + if (main_host != null && main_host.length() != 0){ + host.setText(main_host); + } + + if (main_port != null && main_port.length() != 0){ + port.setText(main_port); + } + + if (main_from != null && main_from.length() != 0){ + from.setText(main_from); + } + + if (main_user != null && main_user.length() != 0){ + user.setText(main_user); + } + + if (main_pass != null && main_pass.length() != 0){ + pass.setText(main_pass); + } + + //设置端口默认 + if (port.getText().length() == 0){ + port.setText(String.valueOf(MailController.mail_port)); + } + + user_list.setFixedCellSize(40); + user_list.setItems(list); + user_list.setPlaceholder(new Label("暂无发送人!")); + + from.textProperty().addListener((observable, oldValue, newValue) -> { + if (newValue != null){ + host.setText(p + "." + newValue.split("@")[1]); + user.setText(newValue.split("@")[0]); + } + }); + + send.setOnAction(event -> { + + if (from.getText().length() == 0 ){ + AlertUtils.alert_warning("发件人不能为空!"); + return; + } + + if (pass.getText().length() == 0 ){ + AlertUtils.alert_warning("发件人密码不能为空!"); + return; + } + + if (host.getText().length() == 0 ){ + host.setText(p + "." + from.getText().split("@")[1]); + } + + if (port.getText().length() == 0){ + user.setText(from.getText().split("@")[0]); + } + save_config(); + + //判断附件是否为空 + + }); + + choose_file.setOnAction(event -> { + + + }); + } + + private void save_config(){ + save("host",host.getText()); + save("port",port.getText()); + } + + private void save(String key,String value){ + setting.set(key,value); + String settingPath = setting.getSettingPath(); + setting.store(settingPath); + } +} diff --git a/src/main/java/com/zhangmeng/tools/controller/SmallToolsController.java b/src/main/java/com/zhangmeng/tools/controller/SmallToolsController.java index 3a005d1..51f0788 100644 --- a/src/main/java/com/zhangmeng/tools/controller/SmallToolsController.java +++ b/src/main/java/com/zhangmeng/tools/controller/SmallToolsController.java @@ -32,6 +32,7 @@ import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.geometry.Pos; @@ -72,6 +73,7 @@ public class SmallToolsController { private AnchorPane qr_code; private AnchorPane date_query; private AnchorPane cron; + private AnchorPane mail; @FXML private ListView listView; @@ -335,6 +337,13 @@ public class SmallToolsController { } cron(flag); } + + if (newValue.getIndex() == 7) { + if (mail != null) { + flag = true; + } + mail(flag); + } } }); } @@ -348,6 +357,7 @@ public class SmallToolsController { case Qr_CODE -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE)); case Date_Query -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE)); case Cron -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE)); + case Mail -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE)); }; } @@ -559,4 +569,30 @@ public class SmallToolsController { log.info("newValue.doubleValue():{}", newValue.doubleValue()); }); } + + @FXML + public void mail_menu_item(ActionEvent event) { + boolean flag = false; + if (mail != null){ + flag = true; + } + mail(flag); + } + + private void mail(boolean flag ){ + //默认选择第一个 + listView.getSelectionModel().select(7); + + if (!flag) { + try { + root = FXMLLoader.load(ResourcesUtils.getResource("mail")); + } catch (IOException e) { + e.printStackTrace(); + } + mail = root; + } else { + root = mail; + } + common_method(); + } } diff --git a/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java index cac3557..2dded82 100644 --- a/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java +++ b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java @@ -110,7 +110,6 @@ public class ResourcesUtils { } public enum SmallTools{ - Hex_16("16进制(Hex)",0), Unicode("Unicode和字符串转换",1), JWT_WEB("json-web-token",2), @@ -118,6 +117,7 @@ public class ResourcesUtils { Qr_CODE("生成二维码",4), Date_Query("日历",5), Cron("cron表达式",6), + Mail("发送邮件",7), ; SmallTools(String title, int index) { diff --git a/src/main/resources/config/mail.setting b/src/main/resources/config/mail.setting new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/fxml/home.fxml b/src/main/resources/fxml/home.fxml index 2d33534..930c79e 100644 --- a/src/main/resources/fxml/home.fxml +++ b/src/main/resources/fxml/home.fxml @@ -37,6 +37,7 @@ + diff --git a/src/main/resources/fxml/mail.fxml b/src/main/resources/fxml/mail.fxml new file mode 100644 index 0000000..c4aff0d --- /dev/null +++ b/src/main/resources/fxml/mail.fxml @@ -0,0 +1,40 @@ + + + + + + + + + + + + +