2013-02-19 79 views
6

這裏的時候,在控制檯上禁用Java郵件跟蹤是我用來發送電子郵件的代碼:如何發送電子郵件

public void sendMail() 
{ 
    try 
    { 
     // Propiedades de la conexión 
     Properties props = new Properties(); 
     props.put("mail.transport.protocol", "smtp"); 
     props.put("mail.smtp.host", SMTP_HOST_NAME); 
     props.put("mail.smtp.port", SMTP_PORT); 
     props.put("mail.smtp.auth", "true"); 

     Authenticator auth = new SMTPAuthenticator(); 
     Session mailSession = Session.getDefaultInstance(props, auth); 

     Transport transport = mailSession.getTransport(); 

     MimeMessage message = new MimeMessage(mailSession); 
     Multipart multipart = new MimeMultipart("alternative"); 

     BodyPart text = new MimeBodyPart(); 
     text.setContent(mailMessage,"text/html; charset=UTF-8"); 

     multipart.addBodyPart(text); 

     message.setContent(multipart); 

     if(friendlyName != null){ 
      //String send = friendlyName + " <" + sender + ">"; 
      message.setFrom(new InternetAddress(sender, friendlyName)); 
     }else{ 
      message.setFrom(new InternetAddress(sender)); 
     } 

     message.setSubject(subject,"UTF-8"); 
     message.addRecipient(Message.RecipientType.TO, 
       new InternetAddress(receiver)); 

     transport.connect(); 
     transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); 
     transport.close(); 


    }catch (Exception e) { 
     // TODO: handle exception 
    } 

} 

當我發送一封電子郵件,我得到的控制檯(或catalina.out的)這樣的輸出:

>>>>>發送數據EHLO XXXXXX < < < < < <

>>>>>發送數據AUTH LOGIN < < < < < <

>>>>>發送數據XXXXXXXXXXXXXXXXXXXX < < < < < <

>>>>>發送數據XXXXXXXXXXXXXX < < < < < <

>>>>>發送數據來自:< < < < < <

>>>>>發送數據RCPT TO:< < < < < <

>>>>>發送數據DATA < < < < < <

>>>>>發送數據< < < < < <

>>>>>發送數據。 < < < < < <

>>>>>發送數據QUIT < < < < < <

如何禁用此輸出?我的catalina.out增長太快了。 Setdebug(false)方法不能解決我的問題。

+3

你已經嘗試過'session.setDebug(false)'? – 2013-02-19 11:21:21

+0

或者你是否使用屬性'-Dmail.debug = true'開始你的應用程序? - http://www.oracle.com/technetwork/java/faq-135477.html#debug – Augusto 2013-02-19 11:25:34

+0

session.setDebug(false)不能解決我的問題。我不在我的tomcat應用中使用-Dmail.debug = true。 – user2086740 2013-02-19 11:36:05

回答

2

它與setDebug方法無關!
我使用的是javax.mail庫的1.40版本,並且有完全相同的問題。 SMTPTransport類中有System.out.println(">>>>>Sending data " + data + "<<<<<<");這一行代碼,它與調試日誌無關,並始終將其數據寫入控制檯!

只要去圖書館1.5版本,問題就解決了。
你可以從Maven repository得到它。

5

在春天這個工作對我來說:

<property name="javaMailProperties"> 
     <props> 
      <prop key="mail.transport.protocol">smtp</prop> 
      <prop key="mail.smtp.auth">false</prop> 
      <prop key="mail.smtp.starttls.enable">true</prop> 
      <prop key="mail.debug">false</prop> 
     </props> 
    </property> 

與1.4版本的測試。7+