Skip to content

Instantly share code, notes, and snippets.

@sinujohn
Created March 27, 2017 10:22
Show Gist options
  • Save sinujohn/f4c6f2fc8d8d2ac05d73cc740943eed2 to your computer and use it in GitHub Desktop.
Save sinujohn/f4c6f2fc8d8d2ac05d73cc740943eed2 to your computer and use it in GitHub Desktop.
Testing email sending via SMTP server using Wiser
<dependency>
<groupId>org.subethamail</groupId>
<artifactId>subethasmtp</artifactId>
<version>3.1.7</version>
<scope>test</scope>
</dependency>
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.subethamail.wiser.Wiser;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class SmtpTest {
private Wiser wiser;
@BeforeClass
public void setup() {
this.wiser = new Wiser();
this.wiser.setPort(2500);
this.wiser.setHostname("localhost");
this.wiser.start();
}
@AfterClass
public void teardown() {
this.wiser.stop();
}
@Before
public void beforeTest() {
this.wiser.getMessages().clear();
}
@After
public void afterTest() {
this.wiser.getMessages().clear();
}
@Test
public void testSendingViaSmtpServer() throws Exception {
final String from = "fromaddress@somewhere.com";
final String to = "someone@aomewhereelse.com";
final String subject = "email subject";
final String body = "email body";
{
// Send EMAIL using above properties
// SMTP should be configured as:
// HOST = localhost
// PORT = 2500
}
assertThat(this.wiser.getMessages().size(), is(1));
assertThat(this.wiser.getMessages().get(0).getEnvelopeSender(), is(from));
assertThat(this.wiser.getMessages().get(0).getEnvelopeReceiver(), is(to));
assertThat(this.wiser.getMessages().get(0).getMimeMessage().getSubject().trim(), is(subject));
assertThat(this.wiser.getMessages().get(0).getMimeMessage().getContent().toString().trim(), is(body));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment