Enviar Correo con JAVA


pom.xml

<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>

Clase java

private static void enviarConOutlook(Correo correo) throws IOException {

try {

Properties props = new Properties();
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(correo.getCorreoRemitente(), correo.getClaveRemitente());
}
});

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(correo.getCorreoRemitente(), "NoReply"));

if(correo.getDestinatario() != null && !correo.getDestinatario().equals("")) {
String[] arrayTo = correo.getDestinatario().split(";", -1);
InternetAddress[] mailAddress_TO = new InternetAddress[arrayTo.length];

for(int x = 0; x < arrayTo.length; x++){
mailAddress_TO[x] = new InternetAddress(arrayTo[x]);
}

msg.addRecipients(Message.RecipientType.TO, mailAddress_TO);
}

if(correo.getDestinatarioCC() != null && !correo.getDestinatarioCC().equals("")) {
String[] arrayCc = correo.getDestinatarioCC().split(";", -1);
InternetAddress[] mailAddress_CC = new InternetAddress[arrayCc.length];

for(int x = 0; x < arrayCc.length; x++){
mailAddress_CC[x] = new InternetAddress(arrayCc[x]);
}

msg.addRecipients(Message.RecipientType.CC, mailAddress_CC);
}

if(correo.getDestinatarioCO() != null && !correo.getDestinatarioCO().equals("")) {
String[] arrayCo = correo.getDestinatarioCO().split(";", -1);
InternetAddress[] mailAddress_CO = new InternetAddress[arrayCo.length];

for(int x = 0; x < arrayCo.length; x++){
mailAddress_CO[x] = new InternetAddress(arrayCo[x]);
}

msg.addRecipients(Message.RecipientType.BCC, mailAddress_CO);
}

msg.setSubject(correo.getAsunto());

MimeMultipart multipart = new MimeMultipart();
BodyPart html = new MimeBodyPart();
html.setContent(correo.getCuerpo(), "text/html");
//htmlPart.setDisposition(BodyPart.INLINE);
multipart.addBodyPart(html);

if(correo.getArchivoUno() != null) {
if(!correo.getArchivoUno().isEmpty() && correo.getArchivoUno() != null) {

BodyPart attach = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(correo.getArchivoUno().getBytes(), correo.getArchivoUno().getContentType());

attach.setDataHandler(new DataHandler(fds));
attach.setFileName(correo.getArchivoUno().getOriginalFilename());
attach.setDisposition(Part.ATTACHMENT);

multipart.addBodyPart(attach);

}
}

if(correo.getArchivoDos() != null) {
if(!correo.getArchivoDos().isEmpty()) {

BodyPart attach = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(correo.getArchivoDos().getBytes(), correo.getArchivoDos().getContentType());

attach.setDataHandler(new DataHandler(fds));
attach.setFileName(correo.getArchivoDos().getOriginalFilename());
attach.setDisposition(Part.ATTACHMENT);

multipart.addBodyPart(attach);

}
}

if(correo.getArchivoTres() != null) {
if(!correo.getArchivoTres().isEmpty() && correo.getArchivoTres() != null) {

BodyPart attach = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(correo.getArchivoTres().getBytes(), correo.getArchivoTres().getContentType());

attach.setDataHandler(new DataHandler(fds));
attach.setFileName(correo.getArchivoTres().getOriginalFilename());
attach.setDisposition(Part.ATTACHMENT);

multipart.addBodyPart(attach);

}
}

msg.setContent(multipart);

Transport.send(msg);
System.out.println("Email sent successfully...");

} catch (AddressException e) {
LoggerCustom.errorApp("", "", e);
} catch (MessagingException e) {
LoggerCustom.errorApp("", "", e);
} catch (UnsupportedEncodingException e) {
LoggerCustom.errorApp("", "", e);
}
}

Prueba:


Comentarios

Entradas populares de este blog

Subir proyecto al repositorio existente en Bitbucket

Sonarqube : The component parameter is missing