Skip to content

Commit

Permalink
更新 SMTP 消息推送配置文件结构
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Dec 2, 2024
1 parent 7bd5d58 commit bef180e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 41 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,11 @@
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import com.ghostchu.peerbanhelper.push.AbstractPushProvider;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
import com.google.gson.JsonObject;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;

import java.io.UnsupportedEncodingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.util.List;
import java.util.Properties;
import java.util.Locale;
import java.util.Objects;

@Slf4j
public class SmtpPushProvider extends AbstractPushProvider {
Expand All @@ -24,25 +26,26 @@ public class SmtpPushProvider extends AbstractPushProvider {

public SmtpPushProvider(String name, Config config) {
this.name = name;
this.config = config;
this.config = config;
}

public static SmtpPushProvider loadFromJson(String name, JsonObject json) {
return new SmtpPushProvider(name, JsonUtil.getGson().fromJson(json, Config.class));
}

public static SmtpPushProvider loadFromYaml(String name, ConfigurationSection section){
Properties props = new Properties();
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", section.getBoolean("ssl"));
props.put("mail.smtp.host", section.getString("host"));
props.put("mail.smtp.port", section.getString("port"));
public static SmtpPushProvider loadFromYaml(String name, ConfigurationSection section) {
var auth = section.getBoolean("auth");
var username = section.getString("username");
var password = section.getString("password");
var sender = section.getString("sender");
var senderName = section.getString("name");
var receivers = section.getStringList("receiver");
Config config = new Config(props, username, password, sender, senderName, receivers);
var encryption = Encryption.valueOf(section.getString("encryption").toUpperCase(Locale.ROOT));
var sendPartial = section.getBoolean("sendPartial");
Config config = new Config(section.getString("host"),
section.getInt("port"), auth, username, password,
sender, senderName, receivers, encryption, sendPartial
);
return new SmtpPushProvider(name, config);
}

Expand All @@ -55,30 +58,46 @@ public JsonObject saveJson() {
public ConfigurationSection saveYaml() {
YamlConfiguration section = new YamlConfiguration();
section.set("type", "smtp");
section.set("ssl", config.getProps().get("mail.smtp.starttls.enable"));
section.set("host", config.getProps().get("mail.smtp.host"));
section.set("port", config.getProps().get("mail.smtp.port"));
section.set("host", config.getHost());
section.set("port", config.getPort());
section.set("auth", config.isAuth());
section.set("username", config.getUsername());
section.set("password", config.getPassword());
section.set("sender", config.getSender());
section.set("name", config.getSenderName());
section.set("receiver", config.getReceivers());
section.set("encryption", config.getEncryption());
section.set("sendPartial", config.isSendPartial());
return section;
}

public void sendMail(String email, String subject, String text) throws MessagingException, UnsupportedEncodingException {
var session = Session.getDefaultInstance(config.getProps(), new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(config.getUsername(), config.getPassword());
public String sendMail(List<String> email, String subject, String text) throws EmailException {
Email mail = new HtmlEmail();
mail.setAuthentication(config.getUsername(), config.getPassword());
mail.setCharset("UTF-8");
mail.setHostName(config.getHost());
mail.setSmtpPort(config.getPort());
switch (config.getEncryption()) {
case STARTTLS -> mail.setStartTLSEnabled(true);
case ENFORCE_STARTTLS -> {
mail.setStartTLSEnabled(true);
mail.setStartTLSRequired(true);
}
});
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(config.getSender(), config.getSenderName(), "UTF-8"));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));
msg.setSubject(stripMarkdown(subject));
msg.setContent(markdown2Html(text), "text/html;charset=utf-8");
Transport.send(msg);
case SSLTLS -> mail.setSSLOnConnect(true);
}
mail.setSendPartial(config.isSendPartial());
mail.setSubject(subject);
mail.setContent(text, "text/html");
mail.setFrom(config.getSender(), config.getSenderName(), "UTF-8");
mail.setTo(email.stream().map(str -> {
try {
return new InternetAddress(str);
} catch (AddressException exception) {
log.warn("The email address [{}] is invalid", str, exception);
return null;
}
}).filter(Objects::nonNull).toList());
return mail.send();
}

@Override
Expand All @@ -93,27 +112,35 @@ public String getConfigType() {

@Override
public boolean push(String title, String content) {
boolean anySuccess = false;
for (String receiver : config.getReceivers()) {
try {
sendMail(receiver, title, content);
anySuccess = true;
} catch (Exception e) {
log.warn("Failed to send mail to {}", receiver, e);
}
try {
sendMail(config.getReceivers(), title, content);
return true;
} catch (EmailException e) {
log.warn("Unable to push message via SMTP", e);
return false;
}
return anySuccess;
}

public enum Encryption {
NONE,
STARTTLS,
ENFORCE_STARTTLS,
SSLTLS
}

@AllArgsConstructor
@Data
public static class Config {
private Properties props;
// private Properties props;
private String host;
private int port;
private boolean auth;
private String username;
private String password;
private String sender;
private String senderName;
private @NotNull List<String> receivers;
private Encryption encryption;
private boolean sendPartial;
}
}

0 comments on commit bef180e

Please sign in to comment.