This commit is contained in:
165
trunk/java/com/l2jserver/loginserver/mail/BaseMail.java
Normal file
165
trunk/java/com/l2jserver/loginserver/mail/BaseMail.java
Normal file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2014 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* L2J Server is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.loginserver.mail;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import com.l2jserver.Config;
|
||||
import com.l2jserver.L2DatabaseFactory;
|
||||
import com.l2jserver.loginserver.mail.MailSystem.MailContent;
|
||||
|
||||
/**
|
||||
* @author mrTJO
|
||||
*/
|
||||
public class BaseMail implements Runnable
|
||||
{
|
||||
private static final Logger _log = Logger.getLogger(BaseMail.class.getName());
|
||||
|
||||
private MimeMessage _messageMime = null;
|
||||
|
||||
private class SmtpAuthenticator extends Authenticator
|
||||
{
|
||||
private final PasswordAuthentication _auth;
|
||||
|
||||
public SmtpAuthenticator()
|
||||
{
|
||||
_auth = new PasswordAuthentication(Config.EMAIL_SYS_USERNAME, Config.EMAIL_SYS_PASSWORD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PasswordAuthentication getPasswordAuthentication()
|
||||
{
|
||||
return _auth;
|
||||
}
|
||||
}
|
||||
|
||||
public BaseMail(String account, String mailId, String... args)
|
||||
{
|
||||
String mailAddr = getUserMail(account);
|
||||
|
||||
if (mailAddr == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MailContent content = MailSystem.getInstance().getMailContent(mailId);
|
||||
if (content == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
String message = compileHtml(account, content.getText(), args);
|
||||
|
||||
final Properties mailProp = new Properties();
|
||||
mailProp.put("mail.smtp.host", Config.EMAIL_SYS_HOST);
|
||||
mailProp.put("mail.smtp.auth", Config.EMAIL_SYS_SMTP_AUTH);
|
||||
mailProp.put("mail.smtp.port", Config.EMAIL_SYS_PORT);
|
||||
mailProp.put("mail.smtp.socketFactory.port", Config.EMAIL_SYS_PORT);
|
||||
mailProp.put("mail.smtp.socketFactory.class", Config.EMAIL_SYS_FACTORY);
|
||||
mailProp.put("mail.smtp.socketFactory.fallback", Config.EMAIL_SYS_FACTORY_CALLBACK);
|
||||
final SmtpAuthenticator authenticator = (Config.EMAIL_SYS_SMTP_AUTH ? new SmtpAuthenticator() : null);
|
||||
|
||||
Session mailSession = Session.getDefaultInstance(mailProp, authenticator);
|
||||
|
||||
try
|
||||
{
|
||||
_messageMime = new MimeMessage(mailSession);
|
||||
_messageMime.setSubject(content.getSubject());
|
||||
try
|
||||
{
|
||||
_messageMime.setFrom(new InternetAddress(Config.EMAIL_SYS_ADDRESS, Config.EMAIL_SERVERINFO_NAME));
|
||||
}
|
||||
catch (UnsupportedEncodingException e)
|
||||
{
|
||||
_log.warning("Sender Address not Valid!");
|
||||
}
|
||||
_messageMime.setContent(message, "text/html");
|
||||
_messageMime.setRecipient(Message.RecipientType.TO, new InternetAddress(mailAddr));
|
||||
}
|
||||
catch (MessagingException e)
|
||||
{
|
||||
_log.warning(getClass().getSimpleName() + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private String compileHtml(String account, String html, String[] args)
|
||||
{
|
||||
if (args != null)
|
||||
{
|
||||
for (int i = 0; i < args.length; i++)
|
||||
{
|
||||
html = html.replace("%var" + i + "%", args[i]);
|
||||
}
|
||||
}
|
||||
html = html.replace("%accountname%", account);
|
||||
return html;
|
||||
}
|
||||
|
||||
private String getUserMail(String username)
|
||||
{
|
||||
try (Connection con = L2DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement statement = con.prepareStatement(Config.EMAIL_SYS_SELECTQUERY))
|
||||
{
|
||||
statement.setString(1, username);
|
||||
try (ResultSet rset = statement.executeQuery())
|
||||
{
|
||||
if (rset.next())
|
||||
{
|
||||
String mail = rset.getString(Config.EMAIL_SYS_DBFIELD);
|
||||
return mail;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.warning("Cannot select user mail: Exception");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_messageMime != null)
|
||||
{
|
||||
Transport.send(_messageMime);
|
||||
}
|
||||
}
|
||||
catch (MessagingException e)
|
||||
{
|
||||
_log.warning("Error encounterd while sending email");
|
||||
}
|
||||
}
|
||||
}
|
155
trunk/java/com/l2jserver/loginserver/mail/MailSystem.java
Normal file
155
trunk/java/com/l2jserver/loginserver/mail/MailSystem.java
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2014 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* L2J Server is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.loginserver.mail;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import javolution.util.FastMap;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import com.l2jserver.Config;
|
||||
|
||||
/**
|
||||
* @author mrTJO
|
||||
*/
|
||||
public class MailSystem
|
||||
{
|
||||
private static final Logger _log = Logger.getLogger(MailSystem.class.getName());
|
||||
private final Map<String, MailContent> _mailData = new FastMap<>();
|
||||
|
||||
public static MailSystem getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
public MailSystem()
|
||||
{
|
||||
loadMails();
|
||||
}
|
||||
|
||||
public void sendMail(String account, String messageId, String... args)
|
||||
{
|
||||
BaseMail mail = new BaseMail(account, messageId, args);
|
||||
mail.run();
|
||||
}
|
||||
|
||||
private void loadMails()
|
||||
{
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setValidating(false);
|
||||
factory.setIgnoringComments(true);
|
||||
File file = new File(Config.DATAPACK_ROOT, "data/mail/MailList.xml");
|
||||
Document doc = null;
|
||||
if (file.exists())
|
||||
{
|
||||
try
|
||||
{
|
||||
doc = factory.newDocumentBuilder().parse(file);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "Could not parse MailList.xml file: " + e.getMessage(), e);
|
||||
return;
|
||||
}
|
||||
|
||||
Node n = doc.getFirstChild();
|
||||
File mailFile;
|
||||
for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
|
||||
{
|
||||
if (d.getNodeName().equals("mail"))
|
||||
{
|
||||
String mailId = d.getAttributes().getNamedItem("id").getNodeValue();
|
||||
String subject = d.getAttributes().getNamedItem("subject").getNodeValue();
|
||||
String maFile = d.getAttributes().getNamedItem("file").getNodeValue();
|
||||
|
||||
mailFile = new File(Config.DATAPACK_ROOT, "data/mail/" + maFile);
|
||||
try (FileInputStream fis = new FileInputStream(mailFile);
|
||||
BufferedInputStream bis = new BufferedInputStream(fis))
|
||||
{
|
||||
int bytes = bis.available();
|
||||
byte[] raw = new byte[bytes];
|
||||
|
||||
bis.read(raw);
|
||||
String html = new String(raw, "UTF-8");
|
||||
html = html.replaceAll(Config.EOL, "\n");
|
||||
html = html.replace("%servermail%", Config.EMAIL_SERVERINFO_ADDRESS);
|
||||
html = html.replace("%servername%", Config.EMAIL_SERVERINFO_NAME);
|
||||
|
||||
_mailData.put(mailId, new MailContent(subject, html));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_log.warning("IOException while reading " + maFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
_log.info("eMail System Loaded");
|
||||
}
|
||||
else
|
||||
{
|
||||
_log.warning("Cannot load eMail System - Missing file MailList.xml");
|
||||
}
|
||||
}
|
||||
|
||||
public class MailContent
|
||||
{
|
||||
private final String _subject;
|
||||
private final String _text;
|
||||
|
||||
/**
|
||||
* @param subject
|
||||
* @param text
|
||||
*/
|
||||
public MailContent(String subject, String text)
|
||||
{
|
||||
_subject = subject;
|
||||
_text = text;
|
||||
}
|
||||
|
||||
public String getSubject()
|
||||
{
|
||||
return _subject;
|
||||
}
|
||||
|
||||
public String getText()
|
||||
{
|
||||
return _text;
|
||||
}
|
||||
}
|
||||
|
||||
public MailContent getMailContent(String mailId)
|
||||
{
|
||||
return _mailData.get(mailId);
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final MailSystem _instance = new MailSystem();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user