BaseMail.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.loginserver.mail;
  16. import java.io.UnsupportedEncodingException;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.Properties;
  21. import java.util.logging.Logger;
  22. import javax.mail.Authenticator;
  23. import javax.mail.Message;
  24. import javax.mail.MessagingException;
  25. import javax.mail.PasswordAuthentication;
  26. import javax.mail.Session;
  27. import javax.mail.Transport;
  28. import javax.mail.internet.InternetAddress;
  29. import javax.mail.internet.MimeMessage;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.L2DatabaseFactory;
  32. import com.l2jserver.loginserver.mail.MailSystem.MailContent;
  33. /**
  34. * @author mrTJO
  35. */
  36. public class BaseMail implements Runnable
  37. {
  38. private final static Logger _log = Logger.getLogger(BaseMail.class.getName());
  39. private final Properties _mailProp = new Properties();
  40. private final SmtpAuthenticator _authenticator;
  41. private MimeMessage _messageMime = null;
  42. private class SmtpAuthenticator extends Authenticator
  43. {
  44. private final PasswordAuthentication _auth;
  45. public SmtpAuthenticator()
  46. {
  47. _auth = new PasswordAuthentication(Config.EMAIL_SYS_USERNAME, Config.EMAIL_SYS_PASSWORD);
  48. }
  49. @Override
  50. public PasswordAuthentication getPasswordAuthentication()
  51. {
  52. return _auth;
  53. }
  54. }
  55. public BaseMail(String account, String mailId, String... args)
  56. {
  57. _mailProp.put("mail.smtp.host", Config.EMAIL_SYS_HOST);
  58. _mailProp.put("mail.smtp.auth", Config.EMAIL_SYS_SMTP_AUTH);
  59. _mailProp.put("mail.smtp.port", Config.EMAIL_SYS_PORT);
  60. _mailProp.put("mail.smtp.socketFactory.port", Config.EMAIL_SYS_PORT);
  61. _mailProp.put("mail.smtp.socketFactory.class", Config.EMAIL_SYS_FACTORY);
  62. _mailProp.put("mail.smtp.socketFactory.fallback", Config.EMAIL_SYS_FACTORY_CALLBACK);
  63. _authenticator = Config.EMAIL_SYS_SMTP_AUTH ? new SmtpAuthenticator() : null;
  64. String mailAddr = getUserMail(account);
  65. if (mailAddr == null)
  66. return;
  67. MailContent content = MailSystem.getInstance().getMailContent(mailId);
  68. if (content == null)
  69. return;
  70. String message = compileHtml(account, content.getText(), args);
  71. Session mailSession = Session.getDefaultInstance(_mailProp, _authenticator);
  72. try
  73. {
  74. _messageMime = new MimeMessage(mailSession);
  75. _messageMime.setSubject(content.getSubject());
  76. try
  77. {
  78. _messageMime.setFrom(new InternetAddress(Config.EMAIL_SYS_ADDRESS, Config.EMAIL_SERVERINFO_NAME));
  79. }
  80. catch (UnsupportedEncodingException e)
  81. {
  82. _log.warning("Sender Address not Valid!");
  83. }
  84. _messageMime.setContent(message, "text/html");
  85. _messageMime.setRecipient(Message.RecipientType.TO, new InternetAddress(mailAddr));
  86. }
  87. catch (MessagingException e)
  88. {
  89. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  90. }
  91. }
  92. private String compileHtml(String account, String html, String[] args)
  93. {
  94. if (args != null)
  95. {
  96. for (int i = 0; i < args.length; i++)
  97. {
  98. html = html.replace("%var"+i+"%", args[i]);
  99. }
  100. }
  101. html = html.replace("%accountname%", account);
  102. return html;
  103. }
  104. private String getUserMail(String username)
  105. {
  106. Connection con = null;
  107. try
  108. {
  109. con = L2DatabaseFactory.getInstance().getConnection();
  110. PreparedStatement statement = con.prepareStatement(Config.EMAIL_SYS_SELECTQUERY);
  111. statement.setString(1, username);
  112. ResultSet rset = statement.executeQuery();
  113. if (rset.next())
  114. {
  115. String mail = rset.getString(Config.EMAIL_SYS_DBFIELD);
  116. return mail;
  117. }
  118. rset.close();
  119. statement.close();
  120. }
  121. catch (Exception e)
  122. {
  123. _log.warning("Cannot select user mail: Exception");
  124. }
  125. finally
  126. {
  127. L2DatabaseFactory.close(con);
  128. }
  129. return null;
  130. }
  131. @Override
  132. public void run()
  133. {
  134. try
  135. {
  136. if (_messageMime != null)
  137. Transport.send(_messageMime);
  138. }
  139. catch (MessagingException e)
  140. {
  141. _log.warning("Error encounterd while sending email");
  142. }
  143. }
  144. }