System.clearProperty("trustStore");
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
System.setProperty("javax.net.ssl.keyStore", System.getProperty("user.dir") + "/certificados/" + nomeCertificado);
System.setProperty("javax.net.ssl.keyStorePassword", Criptografia.decrypt(SingleParametros.getInstance().getParSenhaCert()));
package controle;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
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;
public class EnviarEMail {
private String remetente = "remetendo do email";
private String passEMail = "senha";
public EnviarEMail() {
}
public boolean enviarEMail(String nomeArquivo, String textoEmail, List<String> listaEMail, String nomeRemet) throws Exception {
boolean resultado;
String localXML;
String mailBody;
String mailSubject;
boolean isHTML;
String destinatario;
String destinatarioName;
String smtpServer;
MimeBodyPart attachFilePart;
FileDataSource fds;
MimeBodyPart textPart;
Properties mailProps;
Session mailSession;
Message msg;
Multipart mp;
localXML = System.getProperty("user.dir") + "/temp/";
isHTML = true;
mailBody = "Teste 1";
mailSubject = "Teste 123";
destinatarioName = "";
smtpServer = SingleParametros.getInstance().getParSMTP();
mailBody = textoEmail;
attachFilePart = new MimeBodyPart();
fds = new FileDataSource(localXML + nomeArquivo + ".xml");
attachFilePart.setDataHandler(new DataHandler(fds));
attachFilePart.setFileName(fds.getName());
textPart = new MimeBodyPart();
if (isHTML == true) {
textPart.setContent(mailBody, "text/html");
} else {
textPart.setContent(mailBody, "text/plain");
}
try {
SimpleAuth auth = new SimpleAuth(remetente, passEMail);
for (String email : listaEMail) {
destinatario = email;
mailProps = new Properties();
mailProps.setProperty("mail.transport.protocol", "smtp");
mailProps.setProperty("mail.host", smtpServer);
mailProps.setProperty("mail.user", remetente);
mailProps.setProperty("mail.password", passEMail);
mailProps.setProperty("mail.smtp.starttls.enable","true");
mailProps.setProperty("mail.smtp.auth", "true");
mailProps.setProperty("mail.smtp.user", remetente);
mailSession = Session.getDefaultInstance(mailProps, auth);
msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(remetente, nomeRemet));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(destinatario, destinatarioName));
msg.setSubject(mailSubject);
mp = new MimeMultipart();
mp.addBodyPart(textPart);
mp.addBodyPart(attachFilePart);
msg.setContent(mp);
Transport.send(msg);
}
resultado = true;
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
resultado = false;
}
//auth = null;
localXML = null;
mailBody = null;
mailSubject = null;
isHTML = false;
//remetente = null;
destinatario = null;
destinatarioName = null;
smtpServer = null;
//passEMail = null;
attachFilePart = null;
fds = null;
textPart = null;
mailProps = null;
mailSession = null;
msg = null;
mp = null;
return resultado;
}
}
class SimpleAuth extends Authenticator {
public String username = null;
public String password = null;
public SimpleAuth(String user, String pwd) {
username = user;
password = pwd;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
erro que retorna:
javax.mail.MessagingException: Can't send command to SMTP host;
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
javax.mail.MessagingException: Can't send command to SMTP host;
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
Se alguém tiver idéia de como resolver esse problema eu agradeço.