1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.email.wire;
31
32 import com.icegreen.greenmail.util.DummySSLSocketFactory;
33 import com.icegreen.greenmail.util.GreenMail;
34 import com.icegreen.greenmail.util.ServerSetup;
35 import com.jcabi.email.Envelope;
36 import com.jcabi.email.Postman;
37 import com.jcabi.email.Protocol;
38 import com.jcabi.email.Token;
39 import com.jcabi.email.enclosure.EnPlain;
40 import com.jcabi.email.stamp.StRecipient;
41 import com.jcabi.email.stamp.StSender;
42 import com.jcabi.email.stamp.StSubject;
43 import java.io.IOException;
44 import java.net.ServerSocket;
45 import java.security.Security;
46 import javax.mail.Message;
47 import javax.mail.internet.MimeMessage;
48 import org.hamcrest.MatcherAssert;
49 import org.hamcrest.Matchers;
50 import org.junit.Test;
51
52
53
54
55
56
57
58
59 public final class SmtpsTest {
60
61
62
63
64
65 @Test
66 public void sendsEmailToTheServerThroughSmtps() throws Exception {
67 final String bind = "localhost";
68 final int received = 1;
69 final int port = SmtpsTest.port();
70 final int timeout = 3000;
71 Security.setProperty(
72 "ssl.SocketFactory.provider",
73 DummySSLSocketFactory.class.getName()
74 );
75 final ServerSetup setup = new ServerSetup(
76 port, bind, ServerSetup.PROTOCOL_SMTPS
77 );
78 setup.setServerStartupTimeout(timeout);
79 final GreenMail server = new GreenMail(setup);
80 server.start();
81 try {
82 new Postman.Default(
83 new Smtps(
84 new Token("", "")
85 .access(new Protocol.Smtps(bind, port))
86 )
87 ).send(
88 new Envelope.Safe(
89 new Envelope.Mime()
90 .with(new StSender("from <test-from@jcabi.com>"))
91 .with(new StRecipient("to", "test-to@jcabi.com"))
92 .with(new StSubject("test subject: test me"))
93 .with(new EnPlain("hello"))
94 )
95 );
96 final MimeMessage[] messages = server.getReceivedMessages();
97 MatcherAssert.assertThat(
98 messages.length, Matchers.is(received)
99 );
100 for (final Message msg : messages) {
101 MatcherAssert.assertThat(
102 msg.getFrom()[0].toString(),
103 Matchers.containsString("<test-from@jcabi.com>")
104 );
105 MatcherAssert.assertThat(
106 msg.getSubject(), Matchers.containsString("test me")
107 );
108 }
109 } finally {
110 server.stop();
111 }
112 }
113
114
115
116
117
118
119 private static int port() throws IOException {
120 try (ServerSocket socket = new ServerSocket(0)) {
121 return socket.getLocalPort();
122 }
123 }
124
125 }