Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package dhbw.clippinggorilla.external.mailserver; import dhbw.clippinggorilla.objects.article.Article; import dhbw.clippinggorilla.objects.clipping.Clipping; import dhbw.clippinggorilla.objects.group.GroupInterestProfile; import dhbw.clippinggorilla.objects.interestprofile.InterestProfile; import dhbw.clippinggorilla.objects.user.User; import dhbw.clippinggorilla.objects.user.UserUtils; import dhbw.clippinggorilla.utilities.language.Language; import dhbw.clippinggorilla.utilities.language.Word; import dhbw.clippinggorilla.utilities.log.Log; import dhbw.clippinggorilla.utilities.ressources.FileUtils; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.LinkedHashSet; import org.apache.commons.mail.EmailException; /** * * @author josua.frank */ public class MailUtils { private static final String CLIPPING_ROOT_HTML; private static final String CLIPPING_GROUP_HTML; private static final String CLIPPING_ARTICLE_HTML; static { String root = "<html><head></head><body></body></html>"; String group = ""; String article = ""; try { Path path = FileUtils.createAndGetFile("clippingmail/CLIPPING_ROOT_HTML.html"); root = new String(Files.readAllBytes(path)); path = FileUtils.createAndGetFile("clippingmail/CLIPPING_GROUP_HTML.html"); group = new String(Files.readAllBytes(path)); path = FileUtils.createAndGetFile("clippingmail/CLIPPING_ARTICLE_HTML.html"); article = new String(Files.readAllBytes(path)); } catch (IOException ex) { Log.error("Could not read Clipping-HTML", ex); } CLIPPING_ROOT_HTML = root; CLIPPING_GROUP_HTML = group; CLIPPING_ARTICLE_HTML = article; } public static void sendClipping(Clipping clipping) { User user = UserUtils.getUser(clipping.getUserid()); String email = user.getEmail(); String html = CLIPPING_ROOT_HTML; String rows = ""; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd. LLLL. yyyy"); formatter.withZone(ZoneId.of("Europe/Berlin")); rows = clipping.getArticles().entrySet().stream().map((entry) -> { InterestProfile userprofile = entry.getKey(); LinkedHashSet<Article> articleSet = entry.getValue(); String profileRow = CLIPPING_GROUP_HTML.replace("[GROUPNAME]", userprofile.getName()); String articleRows = articleSet.stream() .map((article) -> CLIPPING_ARTICLE_HTML .replace("[ARTICLE_IMAGE_URL]", article.getUrlToImage().isEmpty() ? article.getSourceAsSource().getLogo().toExternalForm() : article.getUrlToImage()) .replace("[ARTICLE_TITLE]", article.getTitle()) .replace("[ARTICLE_DESCRIPTION]", article.getDescription().length() > 400 ? article.getDescription().substring(0, 400) + "..." : article.getDescription()) .replace("[SOURCE_NAME]", article.getSourceAsSource().getName()) .replace("[ARTICLE_DATE]", article.getPublishedAtAsLocalDateTime().toLocalDate().format(formatter)) .replace("[AUTHOR]", article.getAuthor())) .reduce("", String::concat); return profileRow + "\n" + articleRows; }).reduce(rows, String::concat); rows = clipping.getArticlesFromGroup().entrySet().stream().map((entry) -> { GroupInterestProfile groupprofile = entry.getKey(); LinkedHashSet<Article> articleSet = entry.getValue(); String profileRow = CLIPPING_GROUP_HTML.replace("[GROUPNAME]", groupprofile.getName()); String articleRows = articleSet.stream() .map((article) -> CLIPPING_ARTICLE_HTML .replace("[ARTICLE_IMAGE_URL]", article.getUrlToImage().isEmpty() ? article.getSourceAsSource().getLogo().toExternalForm() : article.getUrlToImage()) .replace("[ARTICLE_TITLE]", article.getTitle()) .replace("[ARTICLE_DESCRIPTION]", article.getDescription().length() > 400 ? article.getDescription().substring(0, 400) + "..." : article.getDescription()) .replace("[SOURCE_NAME]", article.getSourceAsSource().getName()) .replace("[ARTICLE_DATE]", article.getPublishedAtAsLocalDateTime().toLocalDate().format(formatter)) .replace("[AUTHOR]", article.getAuthor())) .reduce("", String::concat); return profileRow + "\n" + articleRows; }).reduce(rows, String::concat); String clippingText = Language.get(Word.CLIPPING_TEXT).replace("[FIRSTNAME]", user.getFirstName()) .replace("[LASTNAME]", user.getLastName()).replace("[DATE]", clipping.getDate().format(formatter)); html = html.replace("[CLIPPING_TEXT]", clippingText).replace("[GROUPS]", rows); // try { // Files.write(Paths.get(System.getProperty("user.home"), "Desktop", "email.html"), html.getBytes("UTF-8")); // } catch (IOException ex) { // Logger.getLogger(MailUtils.class.getName()).log(Level.SEVERE, null, ex); // } try { Mail.send(email, "ClippingGorilla Clippings", "Your email client does not support HTML messages", html); } catch (EmailException ex) { Log.error("Could not send Clipping to user " + user.getFirstName() + " " + user.getLastName() + " (" + user.getUsername() + ")", ex); } } }