feat(email): initial commit

This commit is contained in:
dancingCycle 2022-03-30 10:40:23 +02:00
parent d03f366ac6
commit 0a1acbfee3
8 changed files with 389 additions and 0 deletions

31
email/.gitignore vendored Normal file
View File

@ -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/

7
email/README.md Normal file
View File

@ -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)

103
email/pom.xml Normal file
View File

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>email</name>
<description>description</description>
<url>https://swingbe.de</url>
<groupId>de.swingbe</groupId>
<artifactId>email</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<licenses>
<license>
<name>GNU General Public License</name>
<url>https://www.gnu.org/licenses/gpl-3.0.txt</url>
</license>
</licenses>
<scm>
<url>https://github.com/Software-Ingenieur-Begerad/sandbox-java</url>
</scm>
<properties>
<!-- Other properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies><!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<!--required for Logger class-->
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<!-- exclude signatures from merged JAR to avoid invalid signature messages -->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!-- The shaded JAR will not be the main artifact for the project, it will be attached
for deployment in the way source and docs are. -->
<shadedArtifactAttached>true
</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.swingbe.email.Main
</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}

View File

@ -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();
}
}

View File

@ -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();
}
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
# Rolling appender
<RollingFile name="rollingFile"
fileName="log.txt"
filePattern="log-%d{yyyy-MM-dd}-%i.txt.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>[%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} %m%n
</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="21MB"/>
</Policies>
<DefaultRolloverStrategy max="50"/>
</RollingFile>
# Console appender
<Console name="console" target="SYSTEM_OUT">
# Pattern of log message for console appender
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
# Override log level for specified package
<Logger name="de.swingbe" level="all">
<appender-ref ref="rollingFile" level="all"/>
</Logger>
<Root level="all" additivity="false">
<appender-ref ref="console"/>
</Root>
</Loggers>
</Configuration>