Skip to content
Snippets Groups Projects
BaseMail.java 4.38 KiB
Newer Older
Zoey76's avatar
Zoey76 committed
/*
Zoey76's avatar
Zoey76 committed
 * Copyright © 2004-2024 L2J Server
Zoey76's avatar
Zoey76 committed
 * 
 * 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;

Zoey76's avatar
Zoey76 committed
import static com.l2jserver.loginserver.config.Configuration.email;

Zoey76's avatar
Zoey76 committed
import java.io.UnsupportedEncodingException;
import java.util.Properties;

Zoey76's avatar
Zoey76 committed
import jakarta.mail.Authenticator;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
Zoey76's avatar
Zoey76 committed

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.l2jserver.commons.database.ConnectionFactory;

/**
 * Base Mail.
 * @author mrTJO
 * @version 2.6.1.0
 */
public class BaseMail implements Runnable {
	
	private static final Logger LOG = LoggerFactory.getLogger(BaseMail.class);
	
	private MimeMessage _messageMime = null;
	
Zoey76's avatar
Zoey76 committed
	private static class SmtpAuthenticator extends Authenticator {
Zoey76's avatar
Zoey76 committed
		private final PasswordAuthentication _auth;
		
		public SmtpAuthenticator() {
Zoey76's avatar
Zoey76 committed
			_auth = new PasswordAuthentication(email().getSmtpUsername(), email().getSmtpPassword());
Zoey76's avatar
Zoey76 committed
		}
		
		@Override
		public PasswordAuthentication getPasswordAuthentication() {
			return _auth;
		}
	}
	
	public BaseMail(String account, String mailId, String... args) {
		final var mailAddr = getUserMail(account);
		if (mailAddr == null) {
			return;
		}
		
		final var content = MailSystem.getInstance().getMailContent(mailId);
		if (content == null) {
			return;
		}
		
		final var message = compileHtml(account, content.getText(), args);
		final var mailProp = new Properties();
Zoey76's avatar
Zoey76 committed
		mailProp.put("mail.smtp.host", email().getHost());
		mailProp.put("mail.smtp.auth", email().isSmtpAuthRequired());
		mailProp.put("mail.smtp.port", email().getPort());
		mailProp.put("mail.smtp.socketFactory.port", email().getPort());
		mailProp.put("mail.smtp.socketFactory.class", email().getSmtpFactory());
		mailProp.put("mail.smtp.socketFactory.fallback", email().smtpFactoryCallback());
		final var authenticator = (email().isSmtpAuthRequired() ? new SmtpAuthenticator() : null);
Zoey76's avatar
Zoey76 committed
		final var mailSession = Session.getDefaultInstance(mailProp, authenticator);
		
		try {
			_messageMime = new MimeMessage(mailSession);
			_messageMime.setSubject(content.getSubject());
			try {
Zoey76's avatar
Zoey76 committed
				_messageMime.setFrom(new InternetAddress(email().getServerEmail(), email().getServerName()));
Zoey76's avatar
Zoey76 committed
			} catch (UnsupportedEncodingException ex) {
Zoey76's avatar
Zoey76 committed
				LOG.warn("Sender address {} is not Valid!", email().getServerEmail());
Zoey76's avatar
Zoey76 committed
			}
			_messageMime.setContent(message, "text/html");
			_messageMime.setRecipient(Message.RecipientType.TO, new InternetAddress(mailAddr));
		} catch (MessagingException ex) {
			LOG.warn("There has been an error sending the email!", ex);
		}
	}
	
	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]);
			}
		}
		return html.replace("%accountname%", account);
	}
	
	private String getUserMail(String username) {
		try (var con = ConnectionFactory.getInstance().getConnection();
Zoey76's avatar
Zoey76 committed
			var statement = con.prepareStatement(email().getSelectQuery())) {
Zoey76's avatar
Zoey76 committed
			statement.setString(1, username);
Zoey76's avatar
Zoey76 committed
			try (var rs = statement.executeQuery()) {
				if (rs.next()) {
					return rs.getString(email().getDatabaseField());
Zoey76's avatar
Zoey76 committed
				}
			}
		} catch (Exception ex) {
			LOG.warn("Cannot select user mail!", ex);
		}
		return null;
	}
	
	@Override
	public void run() {
		try {
			if (_messageMime != null) {
				Transport.send(_messageMime);
			}
		} catch (MessagingException ex) {
			LOG.warn("There has been an error while sending email!", ex);
		}
	}
}