2
0

BaseMail.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. *
  35. * @author mrTJO
  36. */
  37. public class BaseMail implements Runnable
  38. {
  39. private final static Logger _log = Logger.getLogger(BaseMail.class.getName());
  40. private final Properties _mailProp = new Properties();
  41. private final SmtpAuthenticator _authenticator;
  42. private MimeMessage _messageMime = null;
  43. private class SmtpAuthenticator extends Authenticator
  44. {
  45. private final PasswordAuthentication _auth;
  46. public SmtpAuthenticator()
  47. {
  48. _auth = new PasswordAuthentication(Config.EMAIL_SYS_USERNAME, Config.EMAIL_SYS_PASSWORD);
  49. }
  50. @Override
  51. public PasswordAuthentication getPasswordAuthentication()
  52. {
  53. return _auth;
  54. }
  55. }
  56. public BaseMail(String account, String mailId, String... args)
  57. {
  58. _mailProp.put("mail.smtp.host", Config.EMAIL_SYS_HOST);
  59. _mailProp.put("mail.smtp.auth", Config.EMAIL_SYS_SMTP_AUTH);
  60. _mailProp.put("mail.smtp.port", Config.EMAIL_SYS_PORT);
  61. _mailProp.put("mail.smtp.socketFactory.port", Config.EMAIL_SYS_PORT);
  62. _mailProp.put("mail.smtp.socketFactory.class", Config.EMAIL_SYS_FACTORY);
  63. _mailProp.put("mail.smtp.socketFactory.fallback", Config.EMAIL_SYS_FACTORY_CALLBACK);
  64. _authenticator = Config.EMAIL_SYS_SMTP_AUTH ? new SmtpAuthenticator() : null;
  65. String mailAddr = getUserMail(account);
  66. if (mailAddr == null)
  67. return;
  68. MailContent content = MailSystem.getInstance().getMailContent(mailId);
  69. if (content == null)
  70. return;
  71. String message = compileHtml(account, content.getText(), args);
  72. Session mailSession = Session.getDefaultInstance(_mailProp, _authenticator);
  73. try
  74. {
  75. _messageMime = new MimeMessage(mailSession);
  76. _messageMime.setSubject(content.getSubject());
  77. try
  78. {
  79. _messageMime.setFrom(new InternetAddress(Config.EMAIL_SYS_ADDRESS, Config.EMAIL_SERVERINFO_NAME));
  80. }
  81. catch (UnsupportedEncodingException e)
  82. {
  83. _log.warning("Sender Address not Valid!");
  84. }
  85. _messageMime.setContent(message, "text/html");
  86. _messageMime.setRecipient(Message.RecipientType.TO, new InternetAddress(mailAddr));
  87. }
  88. catch (MessagingException e)
  89. {
  90. e.printStackTrace();
  91. }
  92. }
  93. private String compileHtml(String account, String html, String[] args)
  94. {
  95. if (args != null)
  96. {
  97. for (int i = 0; i < args.length; i++)
  98. {
  99. html = html.replace("%var"+i+"%", args[i]);
  100. }
  101. }
  102. html = html.replace("%accountname%", account);
  103. return html;
  104. }
  105. private String getUserMail(String username)
  106. {
  107. Connection con = null;
  108. try
  109. {
  110. con = L2DatabaseFactory.getInstance().getConnection();
  111. PreparedStatement statement = con.prepareStatement(Config.EMAIL_SYS_SELECTQUERY);
  112. statement.setString(1, username);
  113. ResultSet rset = statement.executeQuery();
  114. if (rset.next())
  115. {
  116. String mail = rset.getString(Config.EMAIL_SYS_DBFIELD);
  117. return mail;
  118. }
  119. rset.close();
  120. statement.close();
  121. }
  122. catch (Exception e)
  123. {
  124. _log.warning("Cannot select user mail: Exception");
  125. }
  126. finally
  127. {
  128. try
  129. {
  130. L2DatabaseFactory.close(con);
  131. }
  132. catch (Exception e)
  133. {
  134. }
  135. }
  136. return null;
  137. }
  138. /* (non-Javadoc)
  139. * @see java.lang.Runnable#run()
  140. */
  141. @Override
  142. public void run()
  143. {
  144. try
  145. {
  146. if (_messageMime != null)
  147. Transport.send(_messageMime);
  148. }
  149. catch (MessagingException e)
  150. {
  151. _log.warning("Error encounterd while sending email");
  152. }
  153. }
  154. }