Micha Kops avatar Micha Kops committed 4d27168

SMTP/SMTPS Examples added.

Comments (0)

Files changed (4)

src/test/java/it/Pop3IT.java

 	@Test
 	public void getMails() throws IOException, MessagingException,
 			UserException, InterruptedException {
+		// create user on mail server
 		GreenMailUser user = mailServer.setUser(EMAIL_USER_ADDRESS, USER_NAME,
 				USER_PASSWORD);
+
+		// create an e-mail message using javax.mail ..
 		MimeMessage message = new MimeMessage((Session) null);
 		message.setFrom(new InternetAddress(EMAIL_TO));
 		message.addRecipient(Message.RecipientType.TO, new InternetAddress(
 				EMAIL_USER_ADDRESS));
 		message.setSubject(EMAIL_SUBJECT);
 		message.setText(EMAIL_TEXT);
+
+		// use greenmail to store the message
 		user.deliver(message);
 
+		// fetch the e-mail from pop3 using javax.mail ..
 		Properties props = new Properties();
 		props.setProperty("mail.pop3.connectiontimeout", "5000");
 		Session session = Session.getInstance(props);

src/test/java/it/Pop3SIT.java

 	@Test
 	public void getMails() throws IOException, MessagingException,
 			UserException, InterruptedException {
+		// create user on mail server
 		GreenMailUser user = mailServer.setUser(EMAIL_USER_ADDRESS, USER_NAME,
 				USER_PASSWORD);
+
+		// create an e-mail message using javax.mail ..
 		MimeMessage message = new MimeMessage((Session) null);
 		message.setFrom(new InternetAddress(EMAIL_TO));
 		message.addRecipient(Message.RecipientType.TO, new InternetAddress(
 				EMAIL_USER_ADDRESS));
 		message.setSubject(EMAIL_SUBJECT);
 		message.setText(EMAIL_TEXT);
+
+		// use greenmail to store the message
 		user.deliver(message);
 
+		// fetch the e-mail from pop3 using javax.mail ..
 		Properties props = new Properties();
 		props.setProperty("mail.pop3.connectiontimeout", "5000");
 		Session session = Session.getInstance(props);

src/test/java/it/SmtpIT.java

+package it;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.Properties;
+
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.icegreen.greenmail.user.UserException;
+import com.icegreen.greenmail.util.GreenMail;
+import com.icegreen.greenmail.util.ServerSetupTest;
+import com.sun.mail.smtp.SMTPTransport;
+
+public class SmtpIT {
+	private static final String USER_PASSWORD = "abcdef123";
+	private static final String USER_NAME = "hascode";
+	private static final String EMAIL_USER_ADDRESS = "hascode@localhost";
+	private static final String EMAIL_TO = "someone@localhost.com";
+	private static final String EMAIL_SUBJECT = "Test E-Mail";
+	private static final String EMAIL_TEXT = "This is a test e-mail.";
+	private static final String LOCALHOST = "127.0.0.1";
+	private GreenMail mailServer;
+
+	@Before
+	public void setUp() {
+		mailServer = new GreenMail(ServerSetupTest.SMTP);
+		mailServer.start();
+	}
+
+	@After
+	public void tearDown() {
+		mailServer.stop();
+	}
+
+	@Test
+	public void getMails() throws IOException, MessagingException,
+			UserException, InterruptedException {
+		// setup user on the mail server
+		mailServer.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD);
+
+		// create the javax.mail stack with session, message and transport ..
+		Properties props = System.getProperties();
+		props.put("mail.smtp.host", LOCALHOST);
+		props.put("mail.smtp.auth", "true");
+		props.put("mail.smtp.port", ServerSetupTest.SMTP.getPort());
+		Session session = Session.getInstance(props, null);
+		Message msg = new MimeMessage(session);
+		msg.setFrom(new InternetAddress(EMAIL_TO));
+		msg.setRecipients(Message.RecipientType.TO,
+				InternetAddress.parse(EMAIL_USER_ADDRESS, false));
+		msg.setSubject(EMAIL_SUBJECT);
+		msg.setText(EMAIL_TEXT);
+		msg.setSentDate(new Date());
+		SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
+		t.connect(LOCALHOST, EMAIL_USER_ADDRESS, USER_PASSWORD);
+		t.sendMessage(msg, msg.getAllRecipients());
+
+		assertEquals("250 OK\n", t.getLastServerResponse());
+		t.close();
+
+		// fetch messages from server
+		MimeMessage[] messages = mailServer.getReceivedMessages();
+		assertNotNull(messages);
+		assertEquals(1, messages.length);
+		MimeMessage m = messages[0];
+		assertEquals(EMAIL_SUBJECT, m.getSubject());
+		assertTrue(String.valueOf(m.getContent()).contains(EMAIL_TEXT));
+		assertEquals(EMAIL_TO, m.getFrom()[0].toString());
+
+	}
+}

src/test/java/it/SmtpsIT.java

+package it;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.security.Security;
+import java.util.Date;
+import java.util.Properties;
+
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.icegreen.greenmail.user.UserException;
+import com.icegreen.greenmail.util.DummySSLSocketFactory;
+import com.icegreen.greenmail.util.GreenMail;
+import com.icegreen.greenmail.util.ServerSetupTest;
+import com.sun.mail.smtp.SMTPTransport;
+
+public class SmtpsIT {
+	private static final String USER_PASSWORD = "abcdef123";
+	private static final String USER_NAME = "hascode";
+	private static final String EMAIL_USER_ADDRESS = "hascode@localhost";
+	private static final String EMAIL_TO = "someone@localhost.com";
+	private static final String EMAIL_SUBJECT = "Test E-Mail";
+	private static final String EMAIL_TEXT = "This is a test e-mail.";
+	private static final String LOCALHOST = "127.0.0.1";
+	private GreenMail mailServer;
+
+	@Before
+	public void setUp() {
+		Security.setProperty("ssl.SocketFactory.provider",
+				DummySSLSocketFactory.class.getName());
+		mailServer = new GreenMail(ServerSetupTest.SMTPS);
+		mailServer.start();
+	}
+
+	@After
+	public void tearDown() {
+		mailServer.stop();
+	}
+
+	@Test
+	public void getMails() throws IOException, MessagingException,
+			UserException, InterruptedException {
+		// setup user on the mail server
+		mailServer.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD);
+
+		// create the javax.mail stack with session, message and transport ..
+		Properties props = System.getProperties();
+		props.put("mail.smtps.host", LOCALHOST);
+		props.put("mail.smtps.auth", "true");
+		props.put("mail.smtps.port", ServerSetupTest.SMTPS.getPort());
+		Session session = Session.getInstance(props, null);
+		Message msg = new MimeMessage(session);
+		msg.setFrom(new InternetAddress(EMAIL_TO));
+		msg.setRecipients(Message.RecipientType.TO,
+				InternetAddress.parse(EMAIL_USER_ADDRESS, false));
+		msg.setSubject(EMAIL_SUBJECT);
+		msg.setText(EMAIL_TEXT);
+		msg.setSentDate(new Date());
+		SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
+		t.connect(LOCALHOST, EMAIL_USER_ADDRESS, USER_PASSWORD);
+		t.sendMessage(msg, msg.getAllRecipients());
+
+		assertEquals("250 OK\n", t.getLastServerResponse());
+		t.close();
+
+		// fetch messages from server
+		MimeMessage[] messages = mailServer.getReceivedMessages();
+		assertNotNull(messages);
+		assertEquals(1, messages.length);
+		MimeMessage m = messages[0];
+		assertEquals(EMAIL_SUBJECT, m.getSubject());
+		assertTrue(String.valueOf(m.getContent()).contains(EMAIL_TEXT));
+		assertEquals(EMAIL_TO, m.getFrom()[0].toString());
+
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.