<commit_before>/*******************************************************************************
 *  Copyright © 2015 EMBL - European Bioinformatics Institute
 *
 *  Licensed under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License. You may obtain a copy of the License at
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 *  either express or implied. See the License for the specific
 *  language governing permissions and limitations under the
 *  License.
 ******************************************************************************/

package org.mousephenotype.cda.ri.core.utils;

import org.mousephenotype.cda.ri.core.exceptions.InterestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * This class encapsulates the code and data necessary to manage the generation and sending of emails.
 *
 * NOTE: Please do not add any methods here that require being wired in to Spring. Keep this file spring-free, as it
 *       is used in places that are not spring-dependent.
 *
 * Created by mrelac on 02/07/2015.
 */
public class EmailUtils {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * Assembles an e-mail in preparation for sending.
     * @param smtpHost
     * @param smtpPort
     * @param smtpFrom
     * @param smtpReplyto
     * @param subject
     * @param body
     * @param emailAddress
     * @param inHtml
     * @return {@link Message} the assembled email message, ready for sending
     */
    public Message assembleEmail(

            String smtpHost, Integer smtpPort, String smtpFrom, String smtpReplyto,
            String subject, String body, String emailAddress, boolean inHtml) {

        Properties smtpProperties = new Properties();

        smtpProperties.put("mail.smtp.host", smtpHost);
        smtpProperties.put("mail.smtp.port", smtpPort);

        Session session = Session.getInstance(smtpProperties);
        Message message = new MimeMessage(session);

        try {

            message.setFrom(new InternetAddress(smtpFrom));
            InternetAddress[] replyToArray = new InternetAddress[] { new InternetAddress(smtpReplyto) };
            message.setReplyTo(replyToArray);
            message.setRecipients(Message.RecipientType.TO,
                                  InternetAddress.parse(emailAddress));
            message.setSubject(subject);
            if (inHtml) {
                message.setContent(body, "text/html; charset=utf-8");
            } else {
                message.setText(body);
            }

        } catch (MessagingException e) {

            logger.error("InternetAddress parsing error for address '{}'", emailAddress);
            throw new RuntimeException(e);
        }
<commit_msg>InternetAddress parser is rejecting email addresses of the form '<EMAIL>'. Don't know why, as there are no obvious 'illegal' characters. Try setting 'strict' to false.<commit_after>/*******************************************************************************
 *  Copyright © 2015 EMBL - European Bioinformatics Institute
 *
 *  Licensed under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License. You may obtain a copy of the License at
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 *  either express or implied. See the License for the specific
 *  language governing permissions and limitations under the
 *  License.
 ******************************************************************************/

package org.mousephenotype.cda.ri.core.utils;

import org.mousephenotype.cda.ri.core.exceptions.InterestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * This class encapsulates the code and data necessary to manage the generation and sending of emails.
 *
 * NOTE: Please do not add any methods here that require being wired in to Spring. Keep this file spring-free, as it
 *       is used in places that are not spring-dependent.
 *
 * Created by mrelac on 02/07/2015.
 */
public class EmailUtils {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * Assembles an e-mail in preparation for sending.
     * @param smtpHost
     * @param smtpPort
     * @param smtpFrom
     * @param smtpReplyto
     * @param subject
     * @param body
     * @param emailAddress
     * @param inHtml
     * @return {@link Message} the assembled email message, ready for sending
     */
    public Message assembleEmail(

            String smtpHost, Integer smtpPort, String smtpFrom, String smtpReplyto,
            String subject, String body, String emailAddress, boolean inHtml) {

        Properties smtpProperties = new Properties();

        smtpProperties.put("mail.smtp.host", smtpHost);
        smtpProperties.put("mail.smtp.port", smtpPort);

        Session session = Session.getInstance(smtpProperties);
        Message message = new MimeMessage(session);

        try {

            message.setFrom(new InternetAddress(smtpFrom));
            InternetAddress[] replyToArray = new InternetAddress[] { new InternetAddress(smtpReplyto) };
            message.setReplyTo(replyToArray);
            message.setRecipients(Message.RecipientType.TO,
                                  InternetAddress.parse(emailAddress, false));
            message.setSubject(subject);
            if (inHtml) {
                message.setContent(body, "text/html; charset=utf-8");
            } else {
                message.setText(body);
            }

        } catch (MessagingException e) {

            logger.error("InternetAddress parsing error for address '{}'", emailAddress);
            throw new RuntimeException(e);
        }

{'fraction_non_alphanumeric': 0.12822424332786642, 'fraction_numerical': 0.0056657223796034, 'mean_word_length': 3.133086876155268, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1469099', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1669, 'n_words': 559}