diff --git a/network/src/test/java/de/muehlencord/shared/network/mail/TestSendMail.java b/network/src/test/java/de/muehlencord/shared/network/mail/TestSendMail.java new file mode 100644 index 0000000..9183f25 --- /dev/null +++ b/network/src/test/java/de/muehlencord/shared/network/mail/TestSendMail.java @@ -0,0 +1,108 @@ +/* + * 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 de.muehlencord.shared.network.mail; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Properties; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Multipart; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import org.junit.Assume; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Joern Muehlencord + */ +public class TestSendMail { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestSendMail.class); + + private final static String RECEIVER = null; + private final static String PASSWORD = null; + + @Test + public void testSendEmail() throws AddressException, MessagingException, IOException { + + Assume.assumeFalse(RECEIVER == null); + Assume.assumeFalse(PASSWORD == null); + + Properties props = new Properties(); + props.put("mail.smtp.host", "jomu.timelord.de"); + props.put("mail.smtp.auth", true); + props.put("mail.smtp.starttls.enable", true); + props.put("mail.smtp.host", "jomu.timelord.de"); + props.put("mail.smtp.port", "587"); + props.put("mail.debug", "true"); + + Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(RECEIVER, PASSWORD); + } + }); + + MimeMessage message = new MimeMessage(mailSession); + InternetAddress[] address = InternetAddress.parse("joern@muehlencord.de", false); + message.setFrom(address[0]); + message.setRecipients(Message.RecipientType.TO, address); + + message.setSubject("Test HTML, Plan und Attachment", "UTF-8"); + + String htmlBody = "

Das ist ein Test

"; + MimeBodyPart htmlBodyPart = new MimeBodyPart(); + htmlBodyPart.setContent(htmlBody, "text/html; charset=UTF-8"); + + String plainBody = "Das ist ein Test"; + MimeBodyPart plainBodyPart = new MimeBodyPart(); + plainBodyPart.setText(plainBody, "UTF-8"); + + Multipart multipart = new MimeMultipart("alternative"); + multipart.addBodyPart(plainBodyPart); + multipart.addBodyPart(htmlBodyPart); + + MimeBodyPart content = new MimeBodyPart(); + content.setContent(multipart); + + // add all attachments + List attachments = new ArrayList<>(); + attachments.add(new File("/home/jomu/youtube.txt")); + attachments.add(new File("/home/jomu/vvh-contracts.sql")); + MimeMultipart wrapper = new MimeMultipart("related"); + wrapper.addBodyPart(content); + + for (File attachment : attachments) { + MimeBodyPart attachmentBodyPart = new MimeBodyPart(); + attachmentBodyPart.attachFile(attachment); + String contentType = Files.probeContentType(attachment.toPath()); + if (contentType != null) { + attachmentBodyPart.setHeader("Content-Type", contentType); + } + wrapper.addBodyPart(attachmentBodyPart); + } + + message.setContent(wrapper); + message.setSentDate(new Date()); + Transport.send(message); + String messageId = message.getHeader("Message-ID")[0]; + LOGGER.info("Message-Id = {}", messageId); + } +}