Skip to content

Commit 8c35674

Browse files
authored
Merge pull request eugenp#11485 from hkhan/JAVA-8592-fix-email-live-test
[JAVA-8592] Fix email service live test
2 parents 4d6671d + 48304db commit 8c35674

3 files changed

Lines changed: 85 additions & 55 deletions

File tree

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
package com.baeldung.mail.mailwithattachment;
22

3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.util.Properties;
63
import javax.mail.BodyPart;
74
import javax.mail.Message;
85
import javax.mail.MessagingException;
96
import javax.mail.Multipart;
107
import javax.mail.PasswordAuthentication;
118
import javax.mail.Session;
129
import javax.mail.Transport;
13-
import javax.mail.internet.AddressException;
1410
import javax.mail.internet.InternetAddress;
1511
import javax.mail.internet.MimeBodyPart;
1612
import javax.mail.internet.MimeMessage;
1713
import javax.mail.internet.MimeMultipart;
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.net.URI;
17+
import java.util.Properties;
1818

1919
public class MailWithAttachmentService {
2020

21-
private String username = "";
22-
private String password = "";
23-
private String host = "";
24-
private String port = "";
25-
26-
MailWithAttachmentService() {
27-
}
21+
private final String username;
22+
private final String password;
23+
private final String host;
24+
private final int port;
2825

29-
MailWithAttachmentService(String username, String password, String host, String port) {
26+
MailWithAttachmentService(String username, String password, String host, int port) {
3027
this.username = username;
3128
this.password = password;
3229
this.host = host;
@@ -40,15 +37,14 @@ public Session getSession() {
4037
props.put("mail.smtp.host", this.host);
4138
props.put("mail.smtp.port", this.port);
4239

43-
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
40+
return Session.getInstance(props, new javax.mail.Authenticator() {
4441
protected PasswordAuthentication getPasswordAuthentication() {
4542
return new PasswordAuthentication(username, password);
4643
}
4744
});
48-
return session;
4945
}
5046

51-
public Message createMail(Session session) throws AddressException, MessagingException, IOException {
47+
public void sendMail(Session session) throws MessagingException, IOException {
5248
Message message = new MimeMessage(session);
5349
message.setFrom(new InternetAddress("[email protected]"));
5450
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
@@ -61,23 +57,27 @@ public Message createMail(Session session) throws AddressException, MessagingExc
6157
multipart.addBodyPart(messageBodyPart);
6258

6359
MimeBodyPart attachmentPart = new MimeBodyPart();
64-
MimeBodyPart attachmentPart2 = new MimeBodyPart();
65-
66-
attachmentPart.attachFile(new File("C:\\Document1.txt"));
67-
attachmentPart2.attachFile(new File("C:\\Document2.txt"));
68-
60+
attachmentPart.attachFile(getFile("attachment.txt"));
6961
multipart.addBodyPart(attachmentPart);
62+
63+
MimeBodyPart attachmentPart2 = new MimeBodyPart();
64+
attachmentPart2.attachFile(getFile("attachment2.txt"));
7065
multipart.addBodyPart(attachmentPart2);
7166

7267
message.setContent(multipart);
73-
74-
return message;
68+
Transport.send(message);
7569
}
7670

77-
public void sendMail(Session session) throws MessagingException, IOException {
78-
79-
Message message = createMail(session);
80-
Transport.send(message);
71+
private File getFile(String filename) {
72+
try {
73+
URI uri = this.getClass()
74+
.getClassLoader()
75+
.getResource(filename)
76+
.toURI();
77+
return new File(uri);
78+
} catch (Exception e) {
79+
throw new IllegalArgumentException("Unable to find file from resources: " + filename);
80+
}
8181
}
8282

83-
}
83+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sample attachment content 2
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,77 @@
11
package com.baeldung.mail.mailwithattachment;
22

3-
import static org.junit.Assert.*;
4-
import javax.annotation.Resource;
5-
import javax.mail.Session;
6-
7-
import org.junit.After;
3+
import com.icegreen.greenmail.configuration.GreenMailConfiguration;
4+
import com.icegreen.greenmail.junit.GreenMailRule;
5+
import com.icegreen.greenmail.util.GreenMailUtil;
6+
import com.icegreen.greenmail.util.ServerSetupTest;
87
import org.junit.Before;
8+
import org.junit.Rule;
99
import org.junit.Test;
1010

11-
import com.baeldung.mail.mailwithattachment.MailWithAttachmentService;
12-
import com.icegreen.greenmail.util.GreenMail;
13-
import com.icegreen.greenmail.util.ServerSetupTest;
11+
import javax.annotation.Resource;
12+
import javax.mail.MessagingException;
13+
import javax.mail.Session;
14+
import javax.mail.internet.MimeMessage;
15+
import javax.mail.internet.MimeMultipart;
16+
17+
import static org.junit.Assert.assertEquals;
1418

1519
public class MailWithAttachmentServiceLiveTest {
1620

21+
private static final String USERNAME = "testUser";
22+
private static final String PASSWORD = "password";
23+
private static final String HOSTNAME = "localhost";
24+
25+
@Rule
26+
public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP)
27+
.withConfiguration(
28+
GreenMailConfiguration.aConfig()
29+
.withUser(USERNAME, PASSWORD)
30+
);
31+
1732
@Resource
1833
private MailWithAttachmentService emailService;
19-
private GreenMail greenMail;
2034

2135
@Before
22-
public void startMailServer() {
23-
emailService = new MailWithAttachmentService();
24-
greenMail = new GreenMail(ServerSetupTest.SMTP);
25-
greenMail.start();
36+
public void setup() {
37+
emailService = new MailWithAttachmentService(
38+
USERNAME, PASSWORD, HOSTNAME, greenMail.getSmtp().getPort()
39+
);
2640
}
2741

28-
@After
29-
public void stopMailServer() {
30-
greenMail.stop();
31-
emailService = null;
42+
@Test
43+
public void givenEmailService_whenMessageSentWithAttachments_thenMessageIsReceived() throws Exception {
44+
45+
Session tlsSession = emailService.getSession();
46+
emailService.sendMail(tlsSession);
47+
48+
MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
49+
assertEquals(1, receivedMessages.length);
50+
51+
MimeMessage receivedMessage = receivedMessages[0];
52+
assertEquals("Testing Subject", subjectFrom(receivedMessage));
53+
assertEquals("This is message body", emailTextFrom(receivedMessage));
54+
assertEquals("sample attachment content", attachment1ContentsFrom(receivedMessage));
55+
assertEquals("sample attachment content 2", attachment2ContentsFrom(receivedMessage));
3256
}
3357

34-
@Test
35-
public void canSendMail() {
36-
try {
37-
Session testSession = greenMail.getSmtp()
38-
.createSession();
39-
emailService.sendMail(testSession);
40-
assertEquals(1, greenMail.getReceivedMessages().length);
41-
42-
} catch (Exception e) {
43-
e.printStackTrace();
44-
}
58+
private static String subjectFrom(MimeMessage receivedMessage) throws MessagingException {
59+
return receivedMessage.getSubject();
60+
}
61+
62+
private static String emailTextFrom(MimeMessage receivedMessage) throws Exception {
63+
return GreenMailUtil.getBody(((MimeMultipart) receivedMessage.getContent())
64+
.getBodyPart(0));
65+
}
66+
67+
private static String attachment1ContentsFrom(MimeMessage receivedMessage) throws Exception {
68+
return GreenMailUtil.getBody(((MimeMultipart) receivedMessage.getContent())
69+
.getBodyPart(1));
70+
}
4571

72+
private static String attachment2ContentsFrom(MimeMessage receivedMessage) throws Exception {
73+
return GreenMailUtil.getBody(((MimeMultipart) receivedMessage.getContent())
74+
.getBodyPart(2));
4675
}
4776

4877
}

0 commit comments

Comments
 (0)