From ca725a96e75eb7a71a437134aee3aa7cafce958c Mon Sep 17 00:00:00 2001
From: zhangmeng <1334717033@qq.com>
Date: Fri, 10 Mar 2023 09:49:19 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=82=AE=E4=BB=B6=E5=B7=A5?=
=?UTF-8?q?=E5=85=B7=202023=E5=B9=B43=E6=9C=8810=E6=97=A509:49:11?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 6 ++
.../tools/controller/MailController.java | 96 +++++++++----------
src/main/resources/fxml/mail.fxml | 8 +-
3 files changed, 57 insertions(+), 53 deletions(-)
diff --git a/pom.xml b/pom.xml
index e3686c6..9c32679 100644
--- a/pom.xml
+++ b/pom.xml
@@ -270,6 +270,12 @@
jaxb-api
2.3.0
+
+
+ com.sun.mail
+ javax.mail
+ 1.6.2
+
diff --git a/src/main/java/com/zhangmeng/tools/controller/MailController.java b/src/main/java/com/zhangmeng/tools/controller/MailController.java
index d14919a..395e119 100644
--- a/src/main/java/com/zhangmeng/tools/controller/MailController.java
+++ b/src/main/java/com/zhangmeng/tools/controller/MailController.java
@@ -3,6 +3,7 @@ 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.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.setting.Setting;
import com.zhangmeng.tools.utils.AlertUtils;
@@ -21,6 +22,8 @@ import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
/**
@@ -64,6 +67,9 @@ public class MailController {
@FXML
public TextField file_path;
+ @FXML
+ public TextField subject;
+
@FXML
public Button choose_file;
@@ -73,10 +79,10 @@ public class MailController {
public static final String p = "smtp";
- private final Setting setting = new Setting("config/mail.setting");
-
private SimpleObjectProperty file_info = new SimpleObjectProperty<>();
+ //pa = ufinynhrjynkjfec
+
@FXML
public void initialize() {
@@ -102,35 +108,8 @@ public class MailController {
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){
+ if (port.getText().length() == 0) {
port.setText(String.valueOf(MailController.mail_port));
}
@@ -139,7 +118,7 @@ public class MailController {
user_list.setPlaceholder(new Label("暂无发送人!"));
from.textProperty().addListener((observable, oldValue, newValue) -> {
- if (newValue != null){
+ if (newValue != null) {
host.setText(p + "." + newValue.split("@")[1]);
user.setText(newValue.split("@")[0]);
}
@@ -147,43 +126,60 @@ public class MailController {
send.setOnAction(event -> {
- if (from.getText().length() == 0 ){
+ if (from.getText().length() == 0) {
AlertUtils.alert_warning("发件人不能为空!");
return;
}
- if (pass.getText().length() == 0 ){
+ if (pass.getText().length() == 0) {
AlertUtils.alert_warning("发件人密码不能为空!");
return;
}
- if (host.getText().length() == 0 ){
+ if (user_list.getItems().size() == 0) {
+ AlertUtils.alert_warning("请添加收件人再试!");
+ return;
+ }
+
+ if (host.getText().length() == 0) {
host.setText(p + "." + from.getText().split("@")[1]);
}
- if (port.getText().length() == 0){
+ if (port.getText().length() == 0) {
user.setText(from.getText().split("@")[0]);
}
- save_config();
+ MailAccount account = new MailAccount();
+ account.setHost(host.getText());
+ account.setPort(Integer.parseInt(port.getText()));
+ account.setAuth(true);
+ account.setFrom(from.getText());
+ account.setUser(user.getText());
+ account.setPass(pass.getText());
+ account.setSslEnable(true);
+
+ log.info("account:{}",account);
+
+ //发送人列表
+ ObservableList items = user_list.getItems();
+ List to_list = new ArrayList<>(items);
//判断附件是否为空
+ if (file_info.getValue() != null) {
+ MailUtil.send(account,to_list, subject.getText(), content.getText(), false, file_info.getValue());
+ } else {
+ MailUtil.send(account,to_list, subject.getText(), content.getText(), false);
+ }
+ AlertUtils.alert_msg("发送成功!");
});
- choose_file.setOnAction(event -> {
-
-
+ add_user_to.setOnAction(event -> {
+ if (to.getText().length() == 0) {
+ AlertUtils.alert_warning("请输入收件人!");
+ return;
+ }
+ list.add(to.getText());
+ to.setText(null);
});
}
-
- 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/resources/fxml/mail.fxml b/src/main/resources/fxml/mail.fxml
index c4aff0d..6c4b533 100644
--- a/src/main/resources/fxml/mail.fxml
+++ b/src/main/resources/fxml/mail.fxml
@@ -8,7 +8,7 @@
-
+
@@ -25,8 +25,8 @@
-
-
+
+
@@ -36,5 +36,7 @@
+
+