Debug email sending - java-mail-1.4.4

1

I have following code:

private static boolean envioTest(final String descricao, final String msg, final String to)throws MessagingException{
    final Properties props = new Properties();
    props.put("mail.smtp.host", "64.233.186.108");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    final Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user,ps);
        }
    });

    try {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        final PrintStream ps = new PrintStream(os);

        session.setDebug(true);
        session.setDebugOut(ps);
        final Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
        message.setSubject(descricao);
        message.setText(msg);
        System.out.println("Enviando....");
        Transport.send(message);

        ps.println(); // Deveria imprimir?
        return true;
    } catch (MessagingException e) {
        throw e;
    }finally {
        System.out.println("\n");
    }
}

I would like to know how do I print the shipping information?

I do not really know what to do with PrintStream .

I've tried ps.println(); but it does not display anything.

Thank you!

    
asked by anonymous 29.12.2015 / 18:35

1 answer

1

Reflecting on the comments, I came to the following conclusion:

It needs a PrintStream right?

And why create one? If I already have System.out ?

So instead of creating a new one, I use what I already have:

try {
        session.setDebug(true);
        session.setDebugOut(System.out);

      final Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
        message.setSubject(descricao);
        message.setText(msg);
        System.out.println("Enviando....");
        Transport.send(message);

        return true;
    } catch (MessagingException e) {
        throw e;
    }finally {
        System.out.println("\n");
    }

And so it worked perfectly!

    
30.12.2015 / 13:06