添加邮件工具 2023年3月10日09:49:11

master
zhangmeng 2023-03-10 09:49:19 +08:00
parent 708c7c4c82
commit ca725a96e7
3 changed files with 57 additions and 53 deletions

View File

@ -270,6 +270,12 @@
<artifactId>jaxb-api</artifactId> <artifactId>jaxb-api</artifactId>
<version>2.3.0</version> <version>2.3.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -3,6 +3,7 @@ package com.zhangmeng.tools.controller;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.Resource; import cn.hutool.core.io.resource.Resource;
import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil; import cn.hutool.extra.mail.MailUtil;
import cn.hutool.setting.Setting; import cn.hutool.setting.Setting;
import com.zhangmeng.tools.utils.AlertUtils; import com.zhangmeng.tools.utils.AlertUtils;
@ -21,6 +22,8 @@ import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List;
/** /**
@ -64,6 +67,9 @@ public class MailController {
@FXML @FXML
public TextField file_path; public TextField file_path;
@FXML
public TextField subject;
@FXML @FXML
public Button choose_file; public Button choose_file;
@ -73,10 +79,10 @@ public class MailController {
public static final String p = "smtp"; public static final String p = "smtp";
private final Setting setting = new Setting("config/mail.setting");
private SimpleObjectProperty<File> file_info = new SimpleObjectProperty<>(); private SimpleObjectProperty<File> file_info = new SimpleObjectProperty<>();
//pa = ufinynhrjynkjfec
@FXML @FXML
public void initialize() { public void initialize() {
@ -102,33 +108,6 @@ public class MailController {
file_info.setValue(file); 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)); port.setText(String.valueOf(MailController.mail_port));
@ -157,6 +136,11 @@ public class MailController {
return; return;
} }
if (user_list.getItems().size() == 0) {
AlertUtils.alert_warning("请添加收件人再试!");
return;
}
if (host.getText().length() == 0) { if (host.getText().length() == 0) {
host.setText(p + "." + from.getText().split("@")[1]); host.setText(p + "." + from.getText().split("@")[1]);
} }
@ -164,26 +148,38 @@ public class MailController {
if (port.getText().length() == 0) { if (port.getText().length() == 0) {
user.setText(from.getText().split("@")[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<String> items = user_list.getItems();
List<String> 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);
}
} }

View File

@ -8,7 +8,7 @@
<?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?> <?import javafx.scene.text.Text?>
<AnchorPane fx:controller="com.zhangmeng.tools.controller.MailController" prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1"> <AnchorPane prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.MailController">
<children> <children>
<Label layoutX="33.0" layoutY="46.0" text="邮件服务器的SMTP地址:" /> <Label layoutX="33.0" layoutY="46.0" text="邮件服务器的SMTP地址:" />
<TextField fx:id="host" layoutX="171.0" layoutY="42.0" prefHeight="25.0" prefWidth="285.0" /> <TextField fx:id="host" layoutX="171.0" layoutY="42.0" prefHeight="25.0" prefWidth="285.0" />
@ -25,8 +25,8 @@
<Text layoutX="471.0" layoutY="176.0" strokeType="OUTSIDE" strokeWidth="0.0" text="(必须正确,否则发送失败)" /> <Text layoutX="471.0" layoutY="176.0" strokeType="OUTSIDE" strokeWidth="0.0" text="(必须正确,否则发送失败)" />
<Text layoutX="471.0" layoutY="277.0" strokeType="OUTSIDE" strokeWidth="0.0" text="(注意某些邮箱需要为SMTP服务单独设置授权码)" /> <Text layoutX="471.0" layoutY="277.0" strokeType="OUTSIDE" strokeWidth="0.0" text="(注意某些邮箱需要为SMTP服务单独设置授权码)" />
<Text layoutX="471.0" layoutY="228.0" strokeType="OUTSIDE" strokeWidth="0.0" text="(默认为发件人邮箱前缀)" /> <Text layoutX="471.0" layoutY="228.0" strokeType="OUTSIDE" strokeWidth="0.0" text="(默认为发件人邮箱前缀)" />
<Label layoutX="138.0" layoutY="319.0" text="内容:" /> <Label layoutX="136.0" layoutY="349.0" text="内容:" />
<TextArea fx:id="content" layoutX="171.0" layoutY="328.0" prefHeight="178.0" prefWidth="940.0" AnchorPane.bottomAnchor="151.0" AnchorPane.leftAnchor="171.0" AnchorPane.rightAnchor="89.0" AnchorPane.topAnchor="320.0" /> <TextArea fx:id="content" layoutX="171.0" layoutY="349.0" prefHeight="149.0" prefWidth="940.0" AnchorPane.bottomAnchor="151.0" AnchorPane.leftAnchor="171.0" AnchorPane.rightAnchor="89.0" AnchorPane.topAnchor="349.0" />
<Button fx:id="send" layoutX="276.0" layoutY="565.0" mnemonicParsing="false" text="发送" AnchorPane.bottomAnchor="59.0" /> <Button fx:id="send" layoutX="276.0" layoutY="565.0" mnemonicParsing="false" text="发送" AnchorPane.bottomAnchor="59.0" />
<Label layoutX="752.0" layoutY="46.0" text="发送人列表:" /> <Label layoutX="752.0" layoutY="46.0" text="发送人列表:" />
<ListView fx:id="user_list" layoutX="826.0" layoutY="40.0" prefHeight="149.0" prefWidth="285.0" AnchorPane.leftAnchor="826.0" AnchorPane.rightAnchor="89.0" AnchorPane.topAnchor="40.0" /> <ListView fx:id="user_list" layoutX="826.0" layoutY="40.0" prefHeight="149.0" prefWidth="285.0" AnchorPane.leftAnchor="826.0" AnchorPane.rightAnchor="89.0" AnchorPane.topAnchor="40.0" />
@ -36,5 +36,7 @@
<Label layoutX="130.0" layoutY="513.0" text="附件:" AnchorPane.bottomAnchor="119.0" /> <Label layoutX="130.0" layoutY="513.0" text="附件:" AnchorPane.bottomAnchor="119.0" />
<TextField fx:id="file_path" layoutX="233.0" layoutY="509.0" prefHeight="25.0" prefWidth="330.0" AnchorPane.bottomAnchor="115.0" AnchorPane.leftAnchor="233.0" AnchorPane.rightAnchor="637.0" /> <TextField fx:id="file_path" layoutX="233.0" layoutY="509.0" prefHeight="25.0" prefWidth="330.0" AnchorPane.bottomAnchor="115.0" AnchorPane.leftAnchor="233.0" AnchorPane.rightAnchor="637.0" />
<Button fx:id="choose_file" layoutX="171.0" layoutY="509.0" mnemonicParsing="false" text="Button" AnchorPane.bottomAnchor="115.0" /> <Button fx:id="choose_file" layoutX="171.0" layoutY="509.0" mnemonicParsing="false" text="Button" AnchorPane.bottomAnchor="115.0" />
<Label layoutX="138.0" layoutY="308.0" text="标题:" />
<TextField fx:id="subject" layoutX="171.0" layoutY="304.0" prefHeight="25.0" prefWidth="285.0" />
</children> </children>
</AnchorPane> </AnchorPane>