From 9ca8342e3441585eff979c974400ea0160a559a2 Mon Sep 17 00:00:00 2001 From: Me Car Date: Fri, 22 Jan 2016 21:40:08 +0900 Subject: [PATCH] Add MailClient that uses Java Mail API to send emails. Use it instead of Mailgun client. --- Mage.Server/config/config.xml | 5 ++ .../main/java/mage/server/MageServerImpl.java | 2 +- .../src/main/java/mage/server/MailClient.java | 53 +++++++++++++++++++ .../src/main/java/mage/server/Session.java | 2 +- .../java/mage/server/util/ConfigSettings.java | 20 +++++++ .../main/xml-resources/jaxb/Config/Config.xsd | 5 ++ 6 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 Mage.Server/src/main/java/mage/server/MailClient.java diff --git a/Mage.Server/config/config.xml b/Mage.Server/config/config.xml index 4d22d71917..db1925307a 100644 --- a/Mage.Server/config/config.xml +++ b/Mage.Server/config/config.xml @@ -41,6 +41,11 @@ googleAccount="" mailgunApiKey="" mailgunDomain="" + mailSmtpHost="" + mailSmtpPort="" + mailUser="" + mailPassword="" + mailFromAddress="" /> diff --git a/Mage.Server/src/main/java/mage/server/MageServerImpl.java b/Mage.Server/src/main/java/mage/server/MageServerImpl.java index b2a7553eb9..bbd30b05e3 100644 --- a/Mage.Server/src/main/java/mage/server/MageServerImpl.java +++ b/Mage.Server/src/main/java/mage/server/MageServerImpl.java @@ -140,7 +140,7 @@ public class MageServerImpl implements MageServer { } String authToken = generateAuthToken(); activeAuthTokens.put(email, authToken); - if (!MailgunClient.sendMessage(email, "XMage Password Reset Auth Token", + if (!MailClient.sendMessage(email, "XMage Password Reset Auth Token", "Use this auth token to reset your password: " + authToken + "\n" + "It's valid until the next server restart.")) { sendErrorMessageToClient(sessionId, "There was an error inside the server while emailing an auth token"); diff --git a/Mage.Server/src/main/java/mage/server/MailClient.java b/Mage.Server/src/main/java/mage/server/MailClient.java new file mode 100644 index 0000000000..9c7ef61458 --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/MailClient.java @@ -0,0 +1,53 @@ +package mage.server; + +import java.util.Properties; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import mage.server.util.ConfigSettings; +import org.apache.log4j.Logger; + +public class MailClient { + + private static final Logger logger = Logger.getLogger(Main.class); + + public static boolean sendMessage(String email, String subject, String text) { + if (email.length() == 0) { + logger.info("Email is not sent because the address is empty"); + return false; + } + ConfigSettings config = ConfigSettings.getInstance(); + + Properties properties = System.getProperties(); + properties.setProperty("mail.smtps.host", config.getMailSmtpHost()); + properties.setProperty("mail.smtps.port", config.getMailSmtpPort()); + properties.setProperty("mail.smtps.auth", "true"); + properties.setProperty("mail.user", config.getMailUser()); + properties.setProperty("mail.password", config.getMailPassword()); + + Session session = Session.getDefaultInstance(properties); + + try{ + MimeMessage message = new MimeMessage(session); + message.setFrom(new InternetAddress(config.getMailFromAddress())); + message.addRecipient(Message.RecipientType.TO, new InternetAddress(email)); + message.setSubject(subject); + message.setText(text); + + Transport trnsport; + trnsport = session.getTransport("smtps"); + trnsport.connect(null, properties.getProperty("mail.password")); + message.saveChanges(); + trnsport.sendMessage(message, message.getAllRecipients()); + trnsport.close(); + + return true; + }catch (MessagingException ex) { + logger.error("Error sending message to " + email, ex); + } + return false; + } +} diff --git a/Mage.Server/src/main/java/mage/server/Session.java b/Mage.Server/src/main/java/mage/server/Session.java index 69c8ad42fa..da36d1d892 100644 --- a/Mage.Server/src/main/java/mage/server/Session.java +++ b/Mage.Server/src/main/java/mage/server/Session.java @@ -99,7 +99,7 @@ public class Session { return returnMessage; } AuthorizedUserRepository.instance.add(userName, password, email); - if (MailgunClient.sendMessage(email, "XMage Registration Completed", + if (MailClient.sendMessage(email, "XMage Registration Completed", "You are successfully registered as " + userName + ".")) { logger.info("Sent a registration confirmation email to " + email + " for " + userName); } else { diff --git a/Mage.Server/src/main/java/mage/server/util/ConfigSettings.java b/Mage.Server/src/main/java/mage/server/util/ConfigSettings.java index 4cda0d9195..d472b0a8ce 100644 --- a/Mage.Server/src/main/java/mage/server/util/ConfigSettings.java +++ b/Mage.Server/src/main/java/mage/server/util/ConfigSettings.java @@ -147,6 +147,26 @@ public class ConfigSettings { return config.getServer().getMailgunDomain(); } + public String getMailSmtpHost() { + return config.getServer().getMailSmtpHost(); + } + + public String getMailSmtpPort() { + return config.getServer().getMailSmtpPort(); + } + + public String getMailUser() { + return config.getServer().getMailUser(); + } + + public String getMailPassword() { + return config.getServer().getMailPassword(); + } + + public String getMailFromAddress() { + return config.getServer().getMailFromAddress(); + } + public List getPlayerTypes() { return config.getPlayerTypes().getPlayerType(); } diff --git a/Mage.Server/src/main/xml-resources/jaxb/Config/Config.xsd b/Mage.Server/src/main/xml-resources/jaxb/Config/Config.xsd index 1c25007dd2..0a82ec9bae 100644 --- a/Mage.Server/src/main/xml-resources/jaxb/Config/Config.xsd +++ b/Mage.Server/src/main/xml-resources/jaxb/Config/Config.xsd @@ -38,6 +38,11 @@ + + + + +