added test case for sending mails

This commit is contained in:
jomu
2017-09-17 14:56:11 +00:00
parent 54598cbce0
commit 1e961727c0

View File

@ -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 <joern at muehlencord.de>
*/
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 = "<h1>Das ist ein Test</h1>";
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<File> 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);
}
}