forked from External/mage
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
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue