<commit_before>/**
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library 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
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.jtalks.jcommune.service.nontransactional;

import com.sun.mail.smtp.SMTPMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * Object sends email in a separate thread.
 * Amount of thread depends on parameter "pool-size" in spring task.
 * See http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html
 *
 * @author <NAME>
 */
public class MailSender extends JavaMailSenderImpl {
    private static final Logger LOGGER = LoggerFactory.getLogger(MailSender.class);

    @Async
    @Override
    public void send(MimeMessage mimeMessage) throws MailException {
        try {
            String subject = mimeMessage.getSubject();
            StringBuilder to = new StringBuilder();
            for (Address address : mimeMessage.getRecipients(MimeMessage.RecipientType.TO)) {
                to.append(address.toString());
            }
            SMTPMessage message = new SMTPMessage(mimeMessage);
            message.setEnvelopeFrom(getUsername());
            long started = System.currentTimeMillis();
            super.send(message);
            long secsTook = (System.currentTimeMillis() - started) / 1000;
            if (secsTook > 30) {
                LOGGER.warn("Sending email took long time [{}] for receiver: [{}]. Subject: [{}]",
                        new Object[]{secsTook, to, subject});
            } else if (secsTook > 5) {
                LOGGER.info("Sending email took long time [{}] for receiver: [{}]. Subject: [{}]",
                        new Object[]{secsTook, to, subject});
            }
            LOGGER.debug("Email was sent to [{}] with subject [{}]. Note that this doesn't mean the mail" +
                    " is delivered to the end user, this only means that mail server accepted the email and will" +
                    " try to send it further.", to, subject);
        } catch (MessagingException e) {
            LOGGER.error("Mail sending failed", e);
        }
    }
}<commit_msg>#JC-1995:Now we log all kind of exceptions when mail send fails<commit_after>/**
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library 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
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.jtalks.jcommune.service.nontransactional;

import com.sun.mail.smtp.SMTPMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;

import javax.mail.Address;
import javax.mail.internet.MimeMessage;

/**
 * Object sends email in a separate thread.
 * Amount of thread depends on parameter "pool-size" in spring task.
 * See http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html
 *
 * @author <NAME>
 */
public class MailSender extends JavaMailSenderImpl {
    private static final Logger LOGGER = LoggerFactory.getLogger(MailSender.class);

    @Async
    @Override
    public void send(MimeMessage mimeMessage) throws MailException {
        try {
            String subject = mimeMessage.getSubject();
            StringBuilder to = new StringBuilder();
            for (Address address : mimeMessage.getRecipients(MimeMessage.RecipientType.TO)) {
                to.append(address.toString());
            }
            SMTPMessage message = new SMTPMessage(mimeMessage);
            message.setEnvelopeFrom(getUsername());
            long started = System.currentTimeMillis();
            super.send(message);
            long secsTook = (System.currentTimeMillis() - started) / 1000;
            if (secsTook > 30) {
                LOGGER.warn("Sending email took long time [{}] for receiver: [{}]. Subject: [{}]",
                        new Object[]{secsTook, to, subject});
            } else if (secsTook > 5) {
                LOGGER.info("Sending email took long time [{}] for receiver: [{}]. Subject: [{}]",
                        new Object[]{secsTook, to, subject});
            }
            LOGGER.debug("Email was sent to [{}] with subject [{}]. Note that this doesn't mean the mail" +
                    " is delivered to the end user, this only means that mail server accepted the email and will" +
                    " try to send it further.", to, subject);
        } catch (Exception e) {
            LOGGER.error("Mail sending failed", e);
        }
    }
}
{'fraction_non_alphanumeric': 0.09511358065043307, 'fraction_numerical': 0.009805523778395163, 'mean_word_length': 2.8202247191011236, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4247999', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1594, 'n_words': 576}