Merge pull request #11485 from hkhan/JAVA-8592-fix-email-live-test
[JAVA-8592] Fix email service live test
This commit is contained in:
commit
feefcf22cf
|
@ -1,8 +1,5 @@
|
||||||
package com.baeldung.mail.mailwithattachment;
|
package com.baeldung.mail.mailwithattachment;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Properties;
|
|
||||||
import javax.mail.BodyPart;
|
import javax.mail.BodyPart;
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
|
@ -10,23 +7,23 @@ import javax.mail.Multipart;
|
||||||
import javax.mail.PasswordAuthentication;
|
import javax.mail.PasswordAuthentication;
|
||||||
import javax.mail.Session;
|
import javax.mail.Session;
|
||||||
import javax.mail.Transport;
|
import javax.mail.Transport;
|
||||||
import javax.mail.internet.AddressException;
|
|
||||||
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.InternetAddress;
|
||||||
import javax.mail.internet.MimeBodyPart;
|
import javax.mail.internet.MimeBodyPart;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
import javax.mail.internet.MimeMultipart;
|
import javax.mail.internet.MimeMultipart;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
public class MailWithAttachmentService {
|
public class MailWithAttachmentService {
|
||||||
|
|
||||||
private String username = "";
|
private final String username;
|
||||||
private String password = "";
|
private final String password;
|
||||||
private String host = "";
|
private final String host;
|
||||||
private String port = "";
|
private final int port;
|
||||||
|
|
||||||
MailWithAttachmentService() {
|
MailWithAttachmentService(String username, String password, String host, int port) {
|
||||||
}
|
|
||||||
|
|
||||||
MailWithAttachmentService(String username, String password, String host, String port) {
|
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.host = host;
|
this.host = host;
|
||||||
|
@ -40,15 +37,14 @@ public class MailWithAttachmentService {
|
||||||
props.put("mail.smtp.host", this.host);
|
props.put("mail.smtp.host", this.host);
|
||||||
props.put("mail.smtp.port", this.port);
|
props.put("mail.smtp.port", this.port);
|
||||||
|
|
||||||
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
|
return Session.getInstance(props, new javax.mail.Authenticator() {
|
||||||
protected PasswordAuthentication getPasswordAuthentication() {
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
return new PasswordAuthentication(username, password);
|
return new PasswordAuthentication(username, password);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return session;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Message createMail(Session session) throws AddressException, MessagingException, IOException {
|
public void sendMail(Session session) throws MessagingException, IOException {
|
||||||
Message message = new MimeMessage(session);
|
Message message = new MimeMessage(session);
|
||||||
message.setFrom(new InternetAddress("mail@gmail.com"));
|
message.setFrom(new InternetAddress("mail@gmail.com"));
|
||||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("mail@gmail.com"));
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("mail@gmail.com"));
|
||||||
|
@ -61,23 +57,27 @@ public class MailWithAttachmentService {
|
||||||
multipart.addBodyPart(messageBodyPart);
|
multipart.addBodyPart(messageBodyPart);
|
||||||
|
|
||||||
MimeBodyPart attachmentPart = new MimeBodyPart();
|
MimeBodyPart attachmentPart = new MimeBodyPart();
|
||||||
MimeBodyPart attachmentPart2 = new MimeBodyPart();
|
attachmentPart.attachFile(getFile("attachment.txt"));
|
||||||
|
|
||||||
attachmentPart.attachFile(new File("C:\\Document1.txt"));
|
|
||||||
attachmentPart2.attachFile(new File("C:\\Document2.txt"));
|
|
||||||
|
|
||||||
multipart.addBodyPart(attachmentPart);
|
multipart.addBodyPart(attachmentPart);
|
||||||
|
|
||||||
|
MimeBodyPart attachmentPart2 = new MimeBodyPart();
|
||||||
|
attachmentPart2.attachFile(getFile("attachment2.txt"));
|
||||||
multipart.addBodyPart(attachmentPart2);
|
multipart.addBodyPart(attachmentPart2);
|
||||||
|
|
||||||
message.setContent(multipart);
|
message.setContent(multipart);
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendMail(Session session) throws MessagingException, IOException {
|
|
||||||
|
|
||||||
Message message = createMail(session);
|
|
||||||
Transport.send(message);
|
Transport.send(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private File getFile(String filename) {
|
||||||
|
try {
|
||||||
|
URI uri = this.getClass()
|
||||||
|
.getClassLoader()
|
||||||
|
.getResource(filename)
|
||||||
|
.toURI();
|
||||||
|
return new File(uri);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalArgumentException("Unable to find file from resources: " + filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
sample attachment content 2
|
|
@ -1,48 +1,77 @@
|
||||||
package com.baeldung.mail.mailwithattachment;
|
package com.baeldung.mail.mailwithattachment;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import com.icegreen.greenmail.configuration.GreenMailConfiguration;
|
||||||
import javax.annotation.Resource;
|
import com.icegreen.greenmail.junit.GreenMailRule;
|
||||||
import javax.mail.Session;
|
import com.icegreen.greenmail.util.GreenMailUtil;
|
||||||
|
import com.icegreen.greenmail.util.ServerSetupTest;
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.mail.mailwithattachment.MailWithAttachmentService;
|
import javax.annotation.Resource;
|
||||||
import com.icegreen.greenmail.util.GreenMail;
|
import javax.mail.MessagingException;
|
||||||
import com.icegreen.greenmail.util.ServerSetupTest;
|
import javax.mail.Session;
|
||||||
|
import javax.mail.internet.MimeMessage;
|
||||||
|
import javax.mail.internet.MimeMultipart;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class MailWithAttachmentServiceLiveTest {
|
public class MailWithAttachmentServiceLiveTest {
|
||||||
|
|
||||||
|
private static final String USERNAME = "testUser";
|
||||||
|
private static final String PASSWORD = "password";
|
||||||
|
private static final String HOSTNAME = "localhost";
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP)
|
||||||
|
.withConfiguration(
|
||||||
|
GreenMailConfiguration.aConfig()
|
||||||
|
.withUser(USERNAME, PASSWORD)
|
||||||
|
);
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private MailWithAttachmentService emailService;
|
private MailWithAttachmentService emailService;
|
||||||
private GreenMail greenMail;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void startMailServer() {
|
public void setup() {
|
||||||
emailService = new MailWithAttachmentService();
|
emailService = new MailWithAttachmentService(
|
||||||
greenMail = new GreenMail(ServerSetupTest.SMTP);
|
USERNAME, PASSWORD, HOSTNAME, greenMail.getSmtp().getPort()
|
||||||
greenMail.start();
|
);
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void stopMailServer() {
|
|
||||||
greenMail.stop();
|
|
||||||
emailService = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canSendMail() {
|
public void givenEmailService_whenMessageSentWithAttachments_thenMessageIsReceived() throws Exception {
|
||||||
try {
|
|
||||||
Session testSession = greenMail.getSmtp()
|
|
||||||
.createSession();
|
|
||||||
emailService.sendMail(testSession);
|
|
||||||
assertEquals(1, greenMail.getReceivedMessages().length);
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
Session tlsSession = emailService.getSession();
|
||||||
e.printStackTrace();
|
emailService.sendMail(tlsSession);
|
||||||
}
|
|
||||||
|
|
||||||
|
MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
|
||||||
|
assertEquals(1, receivedMessages.length);
|
||||||
|
|
||||||
|
MimeMessage receivedMessage = receivedMessages[0];
|
||||||
|
assertEquals("Testing Subject", subjectFrom(receivedMessage));
|
||||||
|
assertEquals("This is message body", emailTextFrom(receivedMessage));
|
||||||
|
assertEquals("sample attachment content", attachment1ContentsFrom(receivedMessage));
|
||||||
|
assertEquals("sample attachment content 2", attachment2ContentsFrom(receivedMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String subjectFrom(MimeMessage receivedMessage) throws MessagingException {
|
||||||
|
return receivedMessage.getSubject();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String emailTextFrom(MimeMessage receivedMessage) throws Exception {
|
||||||
|
return GreenMailUtil.getBody(((MimeMultipart) receivedMessage.getContent())
|
||||||
|
.getBodyPart(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String attachment1ContentsFrom(MimeMessage receivedMessage) throws Exception {
|
||||||
|
return GreenMailUtil.getBody(((MimeMultipart) receivedMessage.getContent())
|
||||||
|
.getBodyPart(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String attachment2ContentsFrom(MimeMessage receivedMessage) throws Exception {
|
||||||
|
return GreenMailUtil.getBody(((MimeMultipart) receivedMessage.getContent())
|
||||||
|
.getBodyPart(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue