mirror of
https://github.com/correl/mage.git
synced 2024-11-25 03:00:11 +00:00
Add MailClient that uses Java Mail API to send emails. Use it instead of Mailgun client.
This commit is contained in:
parent
9ebc2d5991
commit
9ca8342e34
6 changed files with 85 additions and 2 deletions
|
@ -41,6 +41,11 @@
|
|||
googleAccount=""
|
||||
mailgunApiKey=""
|
||||
mailgunDomain=""
|
||||
mailSmtpHost=""
|
||||
mailSmtpPort=""
|
||||
mailUser=""
|
||||
mailPassword=""
|
||||
mailFromAddress=""
|
||||
/>
|
||||
<playerTypes>
|
||||
<playerType name="Human" jar="mage-player-human.jar" className="mage.player.human.HumanPlayer"/>
|
||||
|
|
|
@ -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");
|
||||
|
|
53
Mage.Server/src/main/java/mage/server/MailClient.java
Normal file
53
Mage.Server/src/main/java/mage/server/MailClient.java
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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<Plugin> getPlayerTypes() {
|
||||
return config.getPlayerTypes().getPlayerType();
|
||||
}
|
||||
|
|
|
@ -38,6 +38,11 @@
|
|||
<xs:attribute name="googleAccount" 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="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:element>
|
||||
|
||||
|
|
Loading…
Reference in a new issue