0

I am using velocity to generate HTML template based content for the email which is to be sent, and Java Mail API with Spring's MimeMessageHelper to send emails. The problem i am facing is that while rendering, HTML template is thrown to catalina.out which is making the file grow in size and that is not desirable.

I have a separate application log file where the logs are generated. Is there any way so that I can redirect this rendering to my application log file? Or may be I can stop this to be thrown at catalina.out.

Below is the details which gets written while sending email

Loading javamail.default.providers from jar:file:/D:/workspace/EmailService/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/email-service/WEB-INF/lib/mail-1.4.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@2025b64d; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.nagarro.com", port 587, isSSL false
 220 fuseout2c MailAnyone extSMTP Tue, 23 Jul 2013 02:37:58 -0700
DEBUG SMTP: connected to host "smtp.nagarro.com", port: 587

EHLO Vaibhav202001
 250-fuseout2c Hello Vaibhav202001 [14.141.12.161]
 250-SIZE 52428800
 250-PIPELINING
 250-AUTH LOGIN
 250-STARTTLS
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
STARTTLS
220 TLS go ahead
EHLO Vaibhav202001
250-fuseout2c Hello Vaibhav202001 [14.141.12.161]
250-SIZE 52428800
250-PIPELINING
250-AUTH LOGIN
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN"
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
anNhZy5zdXBwb3J0QG5hZ2Fycm8uY29t
334 UGFzc3dvcmQ6
SnNhZ0AxMjM0
235 Authentication succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<jsagteam@nagarro.com>
250 OK
RCPT TO:<vaibhav.shukla@nagarro.com>
250 Accepted
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   vaibhav.shukla@nagarro.com
DATA
354 Enter message, ending with "." on a line by itself
Date: Fri, 26 Jul 2013 12:00:48 +0530 (IST)
From: JSAG Team <jsagteam@nagarro.com>
To: vaibhav.shukla@nagarro.com
Message-ID: <1817391686.01374820248531.JavaMail.vaibhav2020@Vaibhav202001>
Subject: JSAG Home page - subscribers list
MIME-Version: 1.0
Content-Type: multipart/mixed; 
boundary="----=_Part_0_999692932.1374820244866"

------=_Part_0_999692932.1374820244866
Content-Type: multipart/related; 
boundary="----=_Part_1_1241615899.1374820244889"

------=_Part_1_1241615899.1374820244889
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit


          //My Email Template goes here

------=_Part_1_1241615899.1374820244889--

------=_Part_0_999692932.1374820244866--


250 OK id=1V1Z2g-0002Rh-HO
QUIT
221 fuseout2c closing connection

And here is the logging configuration

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/email-service.log
log4j.appender.file.MaxFileSize=1000KB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4p %m%n
log4j.appender.DatePattern='.'yyyy-MM-dd

# Root logger option
log4j.rootLogger=WARN, file
log4j.logger.com.nagarro=WARN

Also the code to send email

JavaMailSender mailSender; // injected through Spring DI

/**
 * The mail message.
 */
SimpleMailMessage mailMessage; // Spring DI

@Override
public void sendEMail(final EmailServiceRequest request, byte[] data) {
    MimeMessage message = mailSender.createMimeMessage();
    OutputStream outStream = null;
    try {

        MimeMessageHelper helper = new MimeMessageHelper(message, true);


        // set attributes
        if (null != request.getRecipientEmailId()) {
            helper.setTo(request.getRecipientEmailId());
        }
        helper.setFrom(request.getSenderEmailId());
        helper.setSubject(request.getSubject());
        helper.setText(request.getContent(), true);

        // check for recipient list
        if (null != request.getRecipients() && !request.getRecipients().isEmpty()) {
            InternetAddress[] recipients = new InternetAddress[request.getRecipients().size()];

            for (int index = 0; index < request.getRecipients().size(); index++) {
                recipients[index] = new InternetAddress(request.getRecipients().get(index));
            }
            helper.setTo(recipients);
        }

        // data handler
        if (null != data) {
            try {

                File file = File.createTempFile(request.getAttachmentName(), "");
                outStream = new FileOutputStream(file);
                outStream.write(data);

                helper.addAttachment(request.getAttachmentName(), file);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            } finally {
                try {
                    outStream.flush();
                    outStream.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }

        if (!StringUtil.isNullOrEmpty(request.getEmailIdCC())) {
            helper.addCc(request.getEmailIdCC());
        }
        if (!StringUtil.isNullOrEmpty(request.getEmailIdBCC())) {
            helper.addBcc(request.getEmailIdBCC());
        }

        mailSender.send(message);
    } catch (MessagingException e) {
        logger.error(e.getLocalizedMessage(), e);
    }
}
4

1 回答 1

3

org.springframework.mail.javamail.JavaMailSenderImpl我假设您已经在上下文配置中定义了一个带有类的 bean 。

如果是这样,您可以尝试在会话上设置调试属性。

mailSender.getSession().setDebug(false);

或者创建一个属性文件并在那里设置值。

mail.debug=false
于 2013-07-23T17:29:20.800 回答