2013-07-23 79 views
0

我正在使用velocity爲要發送的電子郵件生成基於HTML模板的內容,並使用Spring的MimeMessageHelper來發送郵件。我面臨的問題是,在渲染時,HTML模板被拋到catalina.out,這使得文件變大,這是不可取的。java郵件日誌html模板到catalina.out

我有一個單獨的應用程序日誌文件生成日誌。有什麼辦法讓我可以將這個渲染重定向到我的應用程序日誌文件嗎?或者,我可以阻止這個被拋出catalina.out

下面是一個被同時發送電子郵件

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; [email protected]; 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:<[email protected]> 
250 OK 
RCPT TO:<[email protected]> 
250 Accepted 
DEBUG SMTP: Verified Addresses 
DEBUG SMTP: [email protected] 
DATA 
354 Enter message, ending with "." on a line by itself 
Date: Fri, 26 Jul 2013 12:00:48 +0530 (IST) 
From: JSAG Team <[email protected]> 
To: [email protected] 
Message-ID: <[email protected]> 
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 

寫細節,這裏是日誌配置

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 

而且代碼發送電子郵件

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); 
    } 
} 
+0

你是如何配置你的日誌?在我看來,你的日誌級別是DEBUG,這些是調試消息。 – Bart

+0

我已經添加了發送電子郵件的配置和代碼。 –

+0

你可能在你的屬性文件中啓用了* mail.debug *功能嗎? – Bart

回答

3

我想你已經在您的上下文配置中定義了一個類爲org.springframework.mail.javamail.JavaMailSenderImpl的bean。

如果是這樣,您可以嘗試在會話上設置調試屬性。

mailSender.getSession().setDebug(false); 

或者創建屬性文件並在其中設置值。

mail.debug=false 
+0

之上發佈的內容之外,我沒有其他屬性文件是的,我確實爲JavaMailSenderImpl創建了一個bean。讓我看看這是否有效。 –

+0

感謝@Bart,將mail.debug設置爲false會有訣竅。 –

相關問題