diff --git a/email/.gitignore b/email/.gitignore new file mode 100644 index 0000000..33653da --- /dev/null +++ b/email/.gitignore @@ -0,0 +1,31 @@ + +# Files +.classpath +.externalToolBuilders +.gradle +.project +.pydevproject +.settings +.sonar +*~ +*.ipr +*.iml +*.iws +*.swp +*.DS_Store +*.snap.debug +dependency-reduced-pom.xml + +# Directories +.idea/ +.run/ +.venv/ +_site/ +build/ +dist/ +docs/_build/ +gen-java/ +gen-javabean/ +gen-py/ +node_modules/ +target/ diff --git a/email/README.md b/email/README.md new file mode 100644 index 0000000..e69f60a --- /dev/null +++ b/email/README.md @@ -0,0 +1,7 @@ +# Overview + +This project can serve as starting point for an Email project. + +# Links + +[Sending Email With Java](https://www.baeldung.com/java-email) diff --git a/email/pom.xml b/email/pom.xml new file mode 100644 index 0000000..78c7eef --- /dev/null +++ b/email/pom.xml @@ -0,0 +1,103 @@ + + + 4.0.0 + + email + description + https://swingbe.de + de.swingbe + email + 0.0.1 + jar + + + + GNU General Public License + https://www.gnu.org/licenses/gpl-3.0.txt + + + + + https://github.com/Software-Ingenieur-Begerad/sandbox-java + + + + + UTF-8 + + + + + javax.mail + mail + 1.5.0-b01 + + + + commons-codec + commons-codec + 1.15 + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.17.2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + true + + shaded + + + + + de.swingbe.email.Main + + + + + + + + + + + diff --git a/email/src/main/java/de/swingbe/email/Email.java b/email/src/main/java/de/swingbe/email/Email.java new file mode 100644 index 0000000..411e620 --- /dev/null +++ b/email/src/main/java/de/swingbe/email/Email.java @@ -0,0 +1,19 @@ +package de.swingbe.email; + +/** + * Send an email. + */ +public interface Email { + /** + * Send an email. + * + * @param sender sender of the email + * @param receiver recipient of the email + * @param subject subject of the email + * @param content content of the email + * @param host host used to send email + * @param user user from which the email is sent + * @param key key of the user + */ + void send(String sender, String receiver, String subject, String content, String user, String key, String host, String contentType); +} diff --git a/email/src/main/java/de/swingbe/email/EmailFactory.java b/email/src/main/java/de/swingbe/email/EmailFactory.java new file mode 100644 index 0000000..bdf926b --- /dev/null +++ b/email/src/main/java/de/swingbe/email/EmailFactory.java @@ -0,0 +1,16 @@ +package de.swingbe.email; + +/** + * Factory for creating instance of {@link Email} + */ +public class EmailFactory { + + /** + * Create an instance of {@link Email} + * + * @return the created instance + */ + public static Email createEmail() { + return new EmailImpl(); + } +} diff --git a/email/src/main/java/de/swingbe/email/EmailImpl.java b/email/src/main/java/de/swingbe/email/EmailImpl.java new file mode 100644 index 0000000..0c85bd7 --- /dev/null +++ b/email/src/main/java/de/swingbe/email/EmailImpl.java @@ -0,0 +1,117 @@ +package de.swingbe.email; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.mail.*; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import javax.net.ssl.SSLContext; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.Properties; + +public class EmailImpl implements Email { + private final static Logger LOG = LoggerFactory.getLogger(EmailImpl.class); + + @Override + public void send(String sender, String receiver, String subject, String content, final String user, final String key, String host, String contentType) { + + //TODO Java version 11 and later are working only with TLSv1.2 with this implementation + SSLContext context = null; + try { + context = SSLContext.getInstance("TLS"); + } catch (NoSuchAlgorithmException e) { + LOG.error("ERROR getting Instance TLS"); + e.printStackTrace(); + } + try { + context.init(null, null, null); + } catch (KeyManagementException e) { + LOG.error("ERROR initialising context"); + e.printStackTrace(); + } + String[] supportedProtocols = context.getDefaultSSLParameters().getProtocols(); + LOG.debug("supportedProtocols: " + Arrays.toString(supportedProtocols)); + String sslProtocols = supportedProtocols[supportedProtocols.length - 1]; + LOG.debug("sslProtocols: " + sslProtocols); + + /** + * configure the library with our email service provider's credentials + * then, create a Session that is used in constructing a message for sending + * the configuration is via a Java Properties object + */ + + Properties prop = new Properties(); + prop.put("mail.smtp.starttls.enable", "true"); + prop.put("mail.smtp.host", host); + prop.put("mail.smtp.user", user); + prop.put("mail.smtp.password", key); + prop.put("mail.smtp.auth", "true"); + prop.put("mail.smtp.port", "587"); + prop.put("mail.smtp.ssl.trust", host); + prop.put("mail.smtp.ssl.protocols", sslProtocols); + + //create a session with username and key + Session session = Session.getInstance(prop, new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(user, key); + } + }); + + //create a MimeMessage for sending + Message message = new MimeMessage(session); + try { + message.setFrom(new InternetAddress(sender)); + } catch (MessagingException e) { + LOG.error("ERROR setting From"); + e.printStackTrace(); + } + try { + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiver)); + } catch (MessagingException e) { + LOG.error("ERROR setting Recipient"); + e.printStackTrace(); + } + try { + message.setSubject(subject); + } catch (MessagingException e) { + LOG.error("ERROR setting Subject"); + e.printStackTrace(); + } + + MimeBodyPart mimeBodyPart = new MimeBodyPart(); + try { + mimeBodyPart.setContent(content, contentType); + } catch (MessagingException e) { + LOG.error("ERROR setting Content"); + e.printStackTrace(); + } + + Multipart multipart = new MimeMultipart(); + try { + multipart.addBodyPart(mimeBodyPart); + } catch (MessagingException e) { + LOG.error("ERROR adding Body"); + e.printStackTrace(); + } + + try { + message.setContent(multipart); + } catch (MessagingException e) { + LOG.error("ERROR setting Content"); + e.printStackTrace(); + } + + try { + Transport.send(message); + } catch (MessagingException e) { + LOG.error("ERROR sending message"); + e.printStackTrace(); + } + } +} diff --git a/email/src/main/java/de/swingbe/email/Main.java b/email/src/main/java/de/swingbe/email/Main.java new file mode 100644 index 0000000..2896ba6 --- /dev/null +++ b/email/src/main/java/de/swingbe/email/Main.java @@ -0,0 +1,60 @@ +package de.swingbe.email; + +import org.apache.commons.codec.binary.Base64; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.mail.*; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import javax.net.ssl.SSLContext; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.Properties; + +public class Main { + + private final static Logger LOG = LoggerFactory.getLogger(Main.class); + + /** + * The mail host from which the emails are sent. + */ + private final static String mailHost = "mail.vbn.de"; + + /** + * The mail user from which the emails are sent. + */ + private final static String mailUser = "Tarifmatrix"; + + /** + * The mail address from which the emails are sent. + */ + private final static String mailSender = "no_reply_tm@vbn.de"; + + private final static String mailReceiver = "sbegerad@posteo.de"; + + /** + * The mail user password from which the emails are sent. + */ + private static final String password; + + static { + //TODO make parameter accessible using configuration +// password = new String(Base64.decodeBase64((new BufferedReader(new FileReader("." + File.separator + "credentials" + File.separator + "email.txt"))).readLine().getBytes())); + password = new String(Base64.decodeBase64("cHRlVjlsVTdrekoyZEYwMTlKNDM=")); + } + + public static void main(String[] args) { + LOG.debug("Hello world!"); + EmailFactory.createEmail().send(mailSender, mailReceiver, "Subject", "This is my first email using JavaMailer", mailUser, password, mailHost, "text/html; charset=utf-8"); + LOG.debug("Bye!"); + return; + } +} diff --git a/email/src/main/resources/log4j2.xml b/email/src/main/resources/log4j2.xml new file mode 100644 index 0000000..5240d3b --- /dev/null +++ b/email/src/main/resources/log4j2.xml @@ -0,0 +1,36 @@ + + + + # Rolling appender + + + [%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} %m%n + + + + + + + + + # Console appender + + # Pattern of log message for console appender + + + + + + # Override log level for specified package + + + + + + + + +