<commit_before>package org.inheritsource.service.delegates;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.regex.Pattern;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

	public static final Logger log = LoggerFactory.getLogger(SendMail.class);

	private static final Pattern rfc2822 = Pattern
			.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");

	protected static void send(String to, String from, String messageSubject,
			String messageText, String SMTPSERVER, int smtpPort,
			String authMecanisms, final String username, final String password) {
		log.info("to: " + to);
		// check email address
		// might like to replace this with EmailValidator from apache.commons
		if (!rfc2822.matcher(to).matches()) {
			log.error("Invalid address");
			return;
		}

		log.info("SMTPSERVER:" + SMTPSERVER);

		log.info("Email subject: " + messageSubject);
		log.info("Email text: " + messageText);

		// Setup mail server
		Properties mailprops = new Properties();
		mailprops.setProperty("mail.smtp.host", SMTPSERVER);
		mailprops.setProperty("mail.smtp.port", Integer.toString(smtpPort));

		try {
			Session session = null;
			if (smtpPort == 465) {
				// SSL
				mailprops.setProperty("mail.smtp.socketFactory.port", "465");
				mailprops.setProperty("mail.smtp.socketFactory.class",
						"javax.net.ssl.SSLSocketFactory");
				mailprops.setProperty("mail.smtp.auth", "true");

				session = Session.getInstance(mailprops,
						new javax.mail.Authenticator() {
							protected PasswordAuthentication getPasswordAuthentication() {
								return new PasswordAuthentication(username,
										password);
							}
						});
			} else {
				if (smtpPort == 587) { // TLS
					mailprops.setProperty("mail.smtp.auth", "true");
					mailprops.setProperty("mail.smtp.starttls.enable", "true");
					// mailprops.setProperty("mail.smtp.starttls.enable",
					// "false");
					mailprops.setProperty("mail.smtp.port", "587");
					if (authMecanisms != null) {
						mailprops.setProperty("mail.smtp.auth.mechanisms",
								authMecanisms);
						// mailprops.setProperty("mail.smtp.auth.mechanisms",
						// "DIGEST-MD5") ;
					}
					session = Session.getInstance(mailprops,
							new javax.mail.Authenticator() {<commit_msg>Added copyright.<commit_after>/* == Motrice Copyright Notice == 
 * 
 * Motrice Service Platform 
 * 
 * Copyright (C) 2011-2014 Motrice AB 
 * 
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Affero General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * This program 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 Affero General Public License for more details. 
 * 
 * You should have received a copy of the GNU Affero General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>. 
 * 
 * e-mail: info _at_ motrice.se 
 * mail: Motrice AB, Långsjövägen 8, SE-131 33 NACKA, SWEDEN 
 * phone: +46 8 641 64 14 
 
 */

package org.inheritsource.service.delegates;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.regex.Pattern;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Authenticator ; 

public class SendMail {

	public static final Logger log = LoggerFactory.getLogger(SendMail.class);

	private static final Pattern rfc2822 = Pattern
			.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");

	protected static void send(String to, String from, String messageSubject,
			String messageText, String SMTPSERVER, int smtpPort,
			String authMecanisms, final String username, final String password) {
		log.info("to: " + to);
		// check email address
		// might like to replace this with EmailValidator from apache.commons
		if (!rfc2822.matcher(to).matches()) {
			log.error("Invalid address");
			return;
		}

		log.info("SMTPSERVER:" + SMTPSERVER);

		log.info("Email subject: " + messageSubject);
		log.info("Email text: " + messageText);

		// Setup mail server
		Properties mailprops = new Properties();
		mailprops.setProperty("mail.smtp.host", SMTPSERVER);
		mailprops.setProperty("mail.smtp.port", Integer.toString(smtpPort));

		try {
			Session session = null;
			if (smtpPort == 465) {
				// SSL
				mailprops.setProperty("mail.smtp.socketFactory.port", "465");
				mailprops.setProperty("mail.smtp.socketFactory.class",
						"javax.net.ssl.SSLSocketFactory");
				mailprops.setProperty("mail.smtp.auth", "true");

				session = Session.getInstance(mailprops,
						new Authenticator() {
							protected PasswordAuthentication getPasswordAuthentication() {
								return new PasswordAuthentication(username,
										password);
							}
						});
			} else {
				if (smtpPort == 587) { // TLS
					mailprops.setProperty("mail.smtp.auth", "true");
					mailprops.setProperty("mail.smtp.starttls.enable", "true");
					// mailprops.setProperty("mail.smtp.starttls.enable",
					// "false");
					mailprops.setProperty("mail.smtp.port", "587");
					if (authMecanisms != null) {
						mailprops.setProperty("mail.smtp.auth.mechanisms",
								authMecanisms);
						// mailprops.setProperty("mail.smtp.auth.mechanisms",
						// "DIGEST-MD5") ;
					}
					session = Session.getInstance(mailprops,
							new Authenticator() {
{'fraction_non_alphanumeric': 0.14849531327084362, 'fraction_numerical': 0.016938003617826016, 'mean_word_length': 3.7110766847405112, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5223075', 'n_tokens_mistral': 2364, 'n_tokens_neox': 1945, 'n_words': 432}