Tenho uma Classe para envio de e-mail com JavaMail, funciona tudo corretamente exceto que, preciso desabilitar o antivirus. Existe algo que possa ser feito para contornar isso? Sem ter que desabilitar o antivirus?
public class EmailAttachmentSender {
public static void sendEmailWithAttachments(String host, String port,
final String userName, final String password, String toAddress,
String subject, String message, ArrayList attachFiles)
throws AddressException, MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// properties.put("mail.smtp.ssl.trust", "*");
properties.put("mail.user", userName);
properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = {new InternetAddress(toAddress)};
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachFiles != null && attachFiles.size() > 0) {
for (Object filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile((String) filePath);
} catch (IOException ex) {
ex.printStackTrace();
}
multipart.addBodyPart(attachPart);
}
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
public static ArrayList listFilesFromFolder() {
ArrayList arrayAnexo = new ArrayList();
// File diretorio = new File("D:\\Mail\\01");
// List<String> results = new ArrayList<String>();
File[] files = new File("D:\\Mail\\01").listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
// results.add(files[i].getName());
arrayAnexo.add(files[i].getAbsolutePath());
System.out.println("File:" + arrayAnexo.get(i));
}
}
return arrayAnexo;
}
/**
* Test sending e-mail with attachments
*/
public static void main(String[] args) {
envio(listFilesFromFolder());
}
public static void envio(ArrayList listFiles) {
// SMTP info
String host = "smtp.gmail.com";
String port = "587";
String mailFrom = "rodrigo@gmail.com";
String password = "senha";
String mailTo = "rodrigo@hotmail.com";
String subject = "Testando";
String message = "Teste";
// attachments
ArrayList<String> att = new ArrayList<>();
for (int i = 0; i < listFiles.size(); i++) {
att.add(listFiles.get(i).toString());
}
try {
sendEmailWithAttachments(host, port, mailFrom, password, mailTo,
subject, message, att);
System.out.println("Email sent.");
} catch (Exception ex) {
System.out.println("Could not send email.");
ex.printStackTrace();
}
}
}
Exception:
javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target