|
@@ -0,0 +1,127 @@
|
|
|
+package com.ademicon.service;
|
|
|
+
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+import javax.mail.Message;
|
|
|
+import javax.mail.MessagingException;
|
|
|
+import javax.mail.PasswordAuthentication;
|
|
|
+import javax.mail.Session;
|
|
|
+import javax.mail.Transport;
|
|
|
+import javax.mail.internet.MimeBodyPart;
|
|
|
+import javax.mail.internet.MimeMessage;
|
|
|
+import javax.mail.internet.MimeMultipart;
|
|
|
+
|
|
|
+import com.ademicon.model.Email;
|
|
|
+
|
|
|
+public class EmailService {
|
|
|
+
|
|
|
+ final String username = "prosegnews@conseg.com.br";
|
|
|
+ final String password = "Ademicon#2024";
|
|
|
+
|
|
|
+ public void sendmail(Email email) throws Exception {
|
|
|
+ Properties props = new Properties();
|
|
|
+ props.setProperty("mail.smtp.starttls.enable", "true");
|
|
|
+ props.setProperty("mail.smtp.auth", "true");
|
|
|
+ props.setProperty("mail.smtp.starttls.enable", "true");
|
|
|
+ props.setProperty("mail.smtp.host", "smtp.office365.com");
|
|
|
+ props.setProperty("mail.smtp.port", "587");
|
|
|
+ props.setProperty("mail.smtp.ssl.trust", "smtp.office365.com");
|
|
|
+
|
|
|
+ Session session = Session.getInstance(props,
|
|
|
+ new javax.mail.Authenticator() {
|
|
|
+ protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
+ return new PasswordAuthentication(username, password);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ Transport transport = session.getTransport();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ Message message = new MimeMessage(session);
|
|
|
+ MimeBodyPart messageBodyPart = new MimeBodyPart();
|
|
|
+ MimeMultipart multipart = new MimeMultipart("mixed");
|
|
|
+
|
|
|
+ // Define o e-mail e nome do remetente
|
|
|
+ message.setFrom(new javax.mail.internet.InternetAddress("prosegnews@conseg.com.br", "Ademicon"));
|
|
|
+
|
|
|
+ // Seta o assunto do e-mail
|
|
|
+ message.setSubject(email.getSubject());
|
|
|
+
|
|
|
+ // Adiciona o destinatário principal
|
|
|
+ message.addRecipient(
|
|
|
+ javax.mail.Message.RecipientType.TO,
|
|
|
+ new javax.mail.internet.InternetAddress(email.getTo()));
|
|
|
+
|
|
|
+ // Seta os destinatários que receberão uma cópia do e-mail
|
|
|
+ if (!email.getCc().isEmpty()) {
|
|
|
+ List<String> c = email.getCc();
|
|
|
+ for (String cString : c) {
|
|
|
+ message.addRecipient(
|
|
|
+ javax.mail.Message.RecipientType.CC,
|
|
|
+ new javax.mail.internet.InternetAddress(cString));
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Seta os destinatários que receberão uma cópia oculta
|
|
|
+ if (!email.getCco().isEmpty()) {
|
|
|
+ List<String> cco = email.getCco();
|
|
|
+ for (String c : cco) {
|
|
|
+ message.addRecipient(
|
|
|
+ javax.mail.Message.RecipientType.BCC,
|
|
|
+ new javax.mail.internet.InternetAddress(c));
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Seta o conteúdo do corpo do e-mail e sua codificação
|
|
|
+ if (!email.getTemplate().isEmpty()) {
|
|
|
+ byte[] decodedBytes = Base64.getDecoder().decode(email.getTemplate());
|
|
|
+ String template = new String(decodedBytes);
|
|
|
+ messageBodyPart.setContent(template, "text/html; charset=utf-8");
|
|
|
+ } else {
|
|
|
+ messageBodyPart.setContent(email.getMessage(), "text/plain; charset=utf-8");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Adiciona os anexos que serão enviados no e-mail
|
|
|
+ if (!email.getAttachments().isEmpty()) {
|
|
|
+ Map<String, String> attachments = email.getAttachments();
|
|
|
+ for (Map.Entry<String, String> set : attachments.entrySet()) {
|
|
|
+ var byteArray = java.util.Base64.getDecoder().decode(new String(set.getValue()));
|
|
|
+ var attachment = new javax.mail.internet.MimeBodyPart();
|
|
|
+ attachment.setDataHandler(new javax.activation.DataHandler(
|
|
|
+ new javax.mail.util.ByteArrayDataSource(byteArray, "application/octet-stream")));
|
|
|
+ attachment.setDisposition(javax.mail.internet.MimeBodyPart.ATTACHMENT);
|
|
|
+ attachment.setFileName(set.getKey());
|
|
|
+ multipart.addBodyPart(attachment);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ multipart.addBodyPart(messageBodyPart);
|
|
|
+ message.setContent(multipart);
|
|
|
+
|
|
|
+ // Método para se autenticar ao servidor
|
|
|
+ transport.connect();
|
|
|
+
|
|
|
+ // Método para enviar a mensagem criada
|
|
|
+ System.out.println("Sending");
|
|
|
+ transport.sendMessage(message, message.getAllRecipients());
|
|
|
+ System.out.println("Done");
|
|
|
+
|
|
|
+ } catch (MessagingException e) {
|
|
|
+ System.out.println("Fail");
|
|
|
+ System.out.println("ERRO EMAIL ENVIO");
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new Exception("Falha ao enviar e-mail de faturamento para " + email.getTo());
|
|
|
+ } finally {
|
|
|
+ transport.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|