Pessoal,
Estou usando essa API abaixo para enviar e-mail por SMTP e está funcionando normalmente.
Entretanto, não estou conseguindo capturar as mensagens de DEBUG que são enviadas naturalmente da API para o Console.
Como posso fazer isso ?
+++
package eMail ;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import eventLog.* ;
public class MailwithAttach
{
public MailwithAttach() throws IOException
{
final String ClasseAtual = this.getClass().getSimpleName() ;
Logging EvLog = new Logging() ;
try
{
String from = "xxxxx@gmail.com";
String to = "xxxxx@gmail.com";
String subject = "Html";
String body = "<IMG > " +
"<font color=blue>" +
"message3:" +
"<A href='http://www.google.com'>google</A>";
MimeMessage message = new MimeMessage(getGmailSession());
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(addHtmlPart(body));
try {
multipart.addBodyPart(addAttach("image.jpg", true));
multipart.addBodyPart(addAttach("LOGS.txt" , false));
} catch ( Exception e){
EvLog.Log ( ClasseAtual , Logging.indexERROR() , "Não foi localizado arquivo de LOG na pasta" ) ;
}
message.setContent(multipart);
Transport.send(message);
}
catch (Exception e)
{
String ErroMsg = e.getMessage() ;
EvLog.Log ( ClasseAtual , Logging.indexDEBUG() , "Mensagem de Erro (E-mail) : " + ErroMsg ) ;
if ( ErroMsg.lastIndexOf( "Could not connect to SMTP host:" ) >= 0 ) {
EvLog.Log ( ClasseAtual , Logging.indexWARNING() , "Não foi possível enviar mensagem. Verifique o Firewall" ) ;
}
}
}
public BodyPart addHtmlPart(String html) throws MessagingException
{
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html,"text/html");
htmlPart.setDisposition(BodyPart.INLINE);
return htmlPart;
}
public BodyPart addAttach(String fileName, boolean isInlineImage) throws MessagingException
{
BodyPart attach = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
attach.setDataHandler(new DataHandler(source));
attach.setFileName(fileName);
if(isInlineImage)
attach.setHeader("Content-ID","<image_" + fileName + ">");
return attach;
}
public Session getLocalSession()
{
Properties props = System.getProperties();
props.put("mail.debug", "false");
props.put("mail.smtp.host", "172.27.19.112");
return Session.getDefaultInstance(props, null);
}
public static Session getGmailSession()
{
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port ", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("xxxxx@gmail.com", "PASSWORD");
}
}
);
return session;
}
public static void main(String[] args) throws IOException
{
new MailwithAttach();
}
}
Segue um trecho da Saída no console :
( repare que a opção debug está ativada na configuração acima )
[quote]DEBUG: JavaMail version 1.4.1
DEBUG: successfully loaded file: C:\Arquivos de programas\Java\jre6\lib\javamail.providers
DEBUG: URL jar:file:/C:/Arquivos%20de%20programas/Java/jre6/lib/mail.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/Arquivos%20de%20programas/Java/jre6/lib/mail.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: URL jar:file:/C:/Arquivos%20de%20programas/Java/jre6/lib/mail.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded resource: jar:file:/C:/Arquivos%20de%20programas/Java/jre6/lib/mail.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded file: C:\Arquivos de programas\Java\jre6\lib\javamail.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host “smtp.gmail.com”, port 25, isSSL false
220 mx.google.com ESMTP w26sm2724176vcf.21
DEBUG SMTP: connected to host “smtp.gmail.com”, port: 465
EHLO BR3L0059
250-mx.google.com at your service, [187.118.175.241]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension “SIZE”, arg “35651584”
DEBUG SMTP: Found extension “8BITMIME”, arg “”
DEBUG SMTP: Found extension “AUTH”, arg “LOGIN PLAIN XOAUTH”
DEBUG SMTP: Found extension “ENHANCEDSTATUSCODES”, arg “”
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
YW5kcmUudGVwcm9tQGdtYWlsLmNvbQ==
334 UGFzc3dvcmQ6
cGVkcm85
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
[/quote]