1 /**
2 * Copyright (c) 2014-2017, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package com.jcabi.email.wire;
31
32 import com.icegreen.greenmail.util.GreenMail;
33 import com.icegreen.greenmail.util.ServerSetup;
34 import com.jcabi.email.Envelope;
35 import com.jcabi.email.Postman;
36 import com.jcabi.email.Protocol;
37 import com.jcabi.email.Token;
38 import com.jcabi.email.enclosure.EnHtml;
39 import com.jcabi.email.enclosure.EnPlain;
40 import com.jcabi.email.stamp.StBcc;
41 import com.jcabi.email.stamp.StCc;
42 import com.jcabi.email.stamp.StRecipient;
43 import com.jcabi.email.stamp.StSender;
44 import com.jcabi.email.stamp.StSubject;
45 import java.io.IOException;
46 import java.net.ServerSocket;
47 import javax.mail.Message;
48 import javax.mail.internet.InternetAddress;
49 import javax.mail.internet.MimeMessage;
50 import org.hamcrest.MatcherAssert;
51 import org.hamcrest.Matchers;
52 import org.junit.Test;
53
54 /**
55 * Test case for {@link Smtp}.
56 * @author Yegor Bugayenko (yegor256@gmail.com)
57 * @version $Id: 135110ad2437c9689d661b93cbe8345ac19b2efe $
58 * @since 1.0
59 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
60 */
61 public final class SmtpTest {
62
63 /**
64 * SMTP postman can send email through SMTP wire.
65 * @throws Exception If fails
66 */
67 @Test
68 public void sendsEmailToSmtpServer() throws Exception {
69 final String bind = "localhost";
70 final int received = 3;
71 final int port = SmtpTest.port();
72 final int timeout = 3000;
73 final ServerSetup setup = new ServerSetup(
74 port, bind, ServerSetup.PROTOCOL_SMTP
75 );
76 setup.setServerStartupTimeout(timeout);
77 final GreenMail server = new GreenMail(setup);
78 server.start();
79 try {
80 new Postman.Default(
81 new Smtp(
82 new Token("", "")
83 .access(new Protocol.Smtp(bind, port))
84 )
85 ).send(
86 new Envelope.Safe(
87 new Envelope.Mime()
88 .with(new StSender("from <test-from@jcabi.com>"))
89 .with(new StRecipient("to", "test-to@jcabi.com"))
90 .with(new StCc(new InternetAddress("cc <c@jcabi.com>")))
91 .with(new StBcc("bcc <bcc@jcabi.com>"))
92 .with(new StSubject("test subject: test me"))
93 .with(new EnPlain("hello"))
94 .with(new EnHtml("<p>how are you?</p>"))
95 )
96 );
97 final MimeMessage[] messages = server.getReceivedMessages();
98 MatcherAssert.assertThat(
99 messages.length,
100 Matchers.is(received)
101 );
102 for (final Message msg : messages) {
103 MatcherAssert.assertThat(
104 msg.getFrom()[0].toString(),
105 Matchers.containsString("<test-from@jcabi.com>")
106 );
107 MatcherAssert.assertThat(
108 msg.getSubject(),
109 Matchers.containsString("test me")
110 );
111 }
112 } finally {
113 server.stop();
114 }
115 }
116
117 /**
118 * Allocate free port.
119 * @return Found port.
120 * @throws IOException In case of error.
121 */
122 private static int port() throws IOException {
123 try (ServerSocket socket = new ServerSocket(0)) {
124 return socket.getLocalPort();
125 }
126 }
127
128 }