Skip to content

Commit

Permalink
重构邮箱发送 (#19)
Browse files Browse the repository at this point in the history
邮箱发送重构
  • Loading branch information
shulng committed Aug 23, 2024
1 parent f5a691f commit aeeb2f1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 57 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cc.baka9</groupId>
<artifactId>CatSeedLogin</artifactId>
<version>1.4.4-SNAPSHOT</version>
<version>1.4.5-SNAPSHOT</version>

<name>CatSeedLogin</name>
<url>https://www.mcbbs.net/thread-847859-1-1.html</url>
Expand Down Expand Up @@ -92,6 +92,11 @@
<artifactId>FoliaLib</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version> <!-- 请使用最新的版本号 -->
</dependency>
</dependencies>
<repositories>
<!-- Spigot -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private static Method initTeleportAsync() {
try {
return Player.class.getMethod("teleportAsync", Location.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ public void onEnable(){

@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
// 当玩家退出时调用
timeoutManager.onPlayerQuit(event.getPlayer().getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
public class LoginPlayerHelper {
private static final Set<LoginPlayer> set = new HashSet<>();
private static Map<String, Long> playerExitTimes = new ConcurrentHashMap<>();
private long timeoutDuration;

public static List<LoginPlayer> getList(){
return new ArrayList<>(set);
Expand Down Expand Up @@ -98,19 +97,16 @@ public static boolean recordCurrentIP(Player player) {
}

LoginPlayer storedPlayer = Cache.getIgnoreCase(playerName);
LoginPlayerHelper helper = new LoginPlayerHelper();

if (storedPlayer != null) {
List<String> storedIPs = getStoredIPs(storedPlayer);
if (storedIPs != null) {
long lastLoginTime = storedPlayer.getLastAction();
Long exitTime = playerExitTimes.get(playerName);
long timeoutInMilliseconds = Config.Settings.IPTimeout * 60 * 1000;

if (Config.Settings.IPTimeout == 0) {
return storedIPs.contains(currentIP);
} else {
if (exitTime != null && storedIPs.contains(currentIP) && (System.currentTimeMillis() - exitTime) <= timeoutInMilliseconds) {
if (exitTime != null && storedIPs.contains(currentIP) && (System.currentTimeMillis() - exitTime) <= Config.Settings.IPTimeout * 60 * 1000) {
return true;
}
}
Expand All @@ -133,9 +129,6 @@ public void recordPlayerExitTime(String playerName) {



public void PlayerTimeoutManager() {
timeoutDuration = Config.Settings.IPTimeout * 60 * 1000;
}

public void onPlayerQuit(String playerName) {
if (Config.Settings.IPTimeout != 0 && isLogin(playerName)) {
Expand Down
61 changes: 16 additions & 45 deletions src/main/java/cc/baka9/catseedlogin/util/Mail.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package cc.baka9.catseedlogin.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.HtmlEmail;

import cc.baka9.catseedlogin.bukkit.Config;

Expand All @@ -17,47 +13,22 @@ private Mail() {

public static void sendMail(String receiveMailAccount, String subject, String content) throws Exception {

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", Config.EmailVerify.EmailSmtpHost);
props.setProperty("mail.smtp.auth", "true");

final String smtpPort = Config.EmailVerify.EmailSmtpPort;
props.setProperty("mail.smtp.port", smtpPort);

Email email = new HtmlEmail();
email.setHostName(Config.EmailVerify.EmailSmtpHost);
email.setSmtpPort(Integer.parseInt(Config.EmailVerify.EmailSmtpPort));
email.setAuthenticator(new DefaultAuthenticator(Config.EmailVerify.EmailAccount, Config.EmailVerify.EmailPassword));
if (Config.EmailVerify.SSLAuthVerify) {
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
email.setSSLOnConnect(true);
} else {
// 如果 SSLAuthVerify 为 false,移除 SSL 相关的配置
props.remove("mail.smtp.socketFactory.class");
props.remove("mail.smtp.socketFactory.fallback");
props.remove("mail.smtp.socketFactory.port");
email.setStartTLSEnabled(true);
}

String emailAccount = Config.EmailVerify.EmailAccount;
String emailPassword = Config.EmailVerify.EmailPassword;

Session session = Session.getInstance(props);

// 设置为debug模式, 查看详细的发送 log
session.setDebug(true);

// 创建邮件
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailAccount, Config.EmailVerify.FromPersonal, "UTF-8"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMailAccount, "", "UTF-8"));
message.setSubject(subject, "UTF-8");
message.setContent(content, Util.isOSLinux() ? "text/html; charset=UTF-8" : "text/html; charset=GBK");
message.setSentDate(new Date());
message.saveChanges();

// 发送
Transport transport = session.getTransport();
transport.connect(emailAccount, emailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
email.setFrom(Config.EmailVerify.EmailAccount, Config.EmailVerify.FromPersonal);
email.setSubject(subject);
email.setMsg(content);
email.addTo(receiveMailAccount);
((HtmlEmail) email).setCharset("UTF-8");
((HtmlEmail) email).setCharset("GBK");
email.send();

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/emailVerify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ EmailAccount: "[email protected]"
EmailPassword: "123456"
EmailSmtpHost: "smtp.qq.com"
EmailSmtpPort: "465"
SSLAuthVerify: false
SSLAuthVerify: true
FromPersonal: "xxx服务器"

0 comments on commit aeeb2f1

Please sign in to comment.