Add MailClient that uses Java Mail API to send emails. Use it instead of Mailgun client.

This commit is contained in:
Me Car 2016-01-22 21:40:08 +09:00
parent 9ebc2d5991
commit 9ca8342e34
6 changed files with 85 additions and 2 deletions

View file

@ -41,6 +41,11 @@
googleAccount="" googleAccount=""
mailgunApiKey="" mailgunApiKey=""
mailgunDomain="" mailgunDomain=""
mailSmtpHost=""
mailSmtpPort=""
mailUser=""
mailPassword=""
mailFromAddress=""
/> />
<playerTypes> <playerTypes>
<playerType name="Human" jar="mage-player-human.jar" className="mage.player.human.HumanPlayer"/> <playerType name="Human" jar="mage-player-human.jar" className="mage.player.human.HumanPlayer"/>

View file

@ -140,7 +140,7 @@ public class MageServerImpl implements MageServer {
} }
String authToken = generateAuthToken(); String authToken = generateAuthToken();
activeAuthTokens.put(email, authToken); 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" + "Use this auth token to reset your password: " + authToken + "\n" +
"It's valid until the next server restart.")) { "It's valid until the next server restart.")) {
sendErrorMessageToClient(sessionId, "There was an error inside the server while emailing an auth token"); sendErrorMessageToClient(sessionId, "There was an error inside the server while emailing an auth token");

View file

@ -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;
}
}

View file

@ -99,7 +99,7 @@ public class Session {
return returnMessage; return returnMessage;
} }
AuthorizedUserRepository.instance.add(userName, password, email); 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 + ".")) { "You are successfully registered as " + userName + ".")) {
logger.info("Sent a registration confirmation email to " + email + " for " + userName); logger.info("Sent a registration confirmation email to " + email + " for " + userName);
} else { } else {

View file

@ -147,6 +147,26 @@ public class ConfigSettings {
return config.getServer().getMailgunDomain(); 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<Plugin> getPlayerTypes() { public List<Plugin> getPlayerTypes() {
return config.getPlayerTypes().getPlayerType(); return config.getPlayerTypes().getPlayerType();
} }

View file

@ -38,6 +38,11 @@
<xs:attribute name="googleAccount" type="xs:string" use="optional"/> <xs:attribute name="googleAccount" type="xs:string" use="optional"/>
<xs:attribute name="mailgunApiKey" type="xs:string" use="optional"/> <xs:attribute name="mailgunApiKey" type="xs:string" use="optional"/>
<xs:attribute name="mailgunDomain" type="xs:string" use="optional"/> <xs:attribute name="mailgunDomain" type="xs:string" use="optional"/>
<xs:attribute name="mailSmtpHost" type="xs:string" use="optional"/>
<xs:attribute name="mailSmtpPort" type="xs:string" use="optional"/>
<xs:attribute name="mailUser" type="xs:string" use="optional"/>
<xs:attribute name="mailPassword" type="xs:string" use="optional"/>
<xs:attribute name="mailFromAddress" type="xs:string" use="optional"/>
</xs:complexType> </xs:complexType>
</xs:element> </xs:element>