org.subethamail.smtp.client.SmartClient Java Examples

The following examples show how to use org.subethamail.smtp.client.SmartClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MessageHandlerTest.java    From subethasmtp with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompletedMailTransaction() throws Exception {

	new Expectations() {
		{
			messageHandlerFactory.create((MessageContext) any);
			result = messageHandler;

			messageHandler.from(anyString);
			messageHandler.recipient(anyString);
			messageHandler.data((InputStream) any);
			messageHandler.done();
		}
	};

	SmartClient client = new SmartClient("localhost", smtpServer.getPort(),
			"localhost");
	client.from("[email protected]");
	client.to("[email protected]");
	client.dataStart();
	client.dataWrite(TextUtils.getAsciiBytes("body"), 4);
	client.dataEnd();
	client.quit();
	smtpServer.stop(); // wait for the server to catch up
}
 
Example #2
Source File: MessageHandlerTest.java    From subethasmtp with Apache License 2.0 6 votes vote down vote up
@Test
public void testAbortedMailTransaction() throws Exception {

	new Expectations() {
		{
			messageHandlerFactory.create((MessageContext) any);
			result = messageHandler;

			messageHandler.from(anyString);
			messageHandler.done();
		}
	};

	SmartClient client = new SmartClient("localhost", smtpServer.getPort(),
			"localhost");
	client.from("[email protected]");
	client.quit();
	smtpServer.stop(); // wait for the server to catch up
}
 
Example #3
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testSiteIdAccept() throws Exception {
    User sender = mock(User.class);
    when(userDirectoryService.findUsersByEmail("[email protected]")).thenReturn(Collections.singleton(sender));

    MailArchiveChannel channel = mock(MailArchiveChannel.class);
    when(channel.getEnabled()).thenReturn(true);
    when(channel.getOpen()).thenReturn(false);
    when(channel.allowAddMessage(sender)).thenReturn(true);
    when(channel.getId()).thenReturn("channelId");
    when(channel.getContext()).thenReturn("siteId");

    when(mailArchiveService.channelReference("siteId", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId")).thenReturn(channel);

    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/simple-email.txt");

    verify(channel, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
}
 
Example #4
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testBadDomainCase() throws Exception {
    // Checks that we ignore the case on the domain
    when(serverConfigurationService.getServerName()).thenReturn("saKai.exAmple.cOm");
    User sender = mock(User.class);
    when(userDirectoryService.findUsersByEmail("[email protected]")).thenReturn(Collections.singleton(sender));

    MailArchiveChannel channel = mock(MailArchiveChannel.class);
    when(channel.getEnabled()).thenReturn(true);
    when(channel.getOpen()).thenReturn(false);
    when(channel.allowAddMessage(sender)).thenReturn(true);
    when(channel.getId()).thenReturn("channelId");
    when(channel.getContext()).thenReturn("siteId");

    when(mailArchiveService.channelReference("siteId", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId")).thenReturn(channel);

    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/simple-email.txt");

    verify(channel, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
}
 
Example #5
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
// Check that the BATV parsing is working correctly
public void testSiteIdAcceptBatv() throws Exception {
    User sender = mock(User.class);
    when(userDirectoryService.findUsersByEmail("[email protected]")).thenReturn(Collections.singleton(sender));

    MailArchiveChannel channel = mock(MailArchiveChannel.class);
    when(channel.getEnabled()).thenReturn(true);
    when(channel.getOpen()).thenReturn(false);
    when(channel.allowAddMessage(sender)).thenReturn(true);
    when(channel.getId()).thenReturn("channelId");
    when(channel.getContext()).thenReturn("siteId");

    when(mailArchiveService.channelReference("siteId", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId")).thenReturn(channel);

    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    // BATV is only in the envelope so the mail message should be the same.
    writeData(client, "/simple-email.txt");

    verify(channel, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
}
 
Example #6
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testSiteIdAccept() throws Exception {
    User sender = mock(User.class);
    when(userDirectoryService.findUsersByEmail("[email protected]")).thenReturn(Collections.singleton(sender));

    MailArchiveChannel channel = mock(MailArchiveChannel.class);
    when(channel.getEnabled()).thenReturn(true);
    when(channel.getOpen()).thenReturn(false);
    when(channel.allowAddMessage(sender)).thenReturn(true);
    when(channel.getId()).thenReturn("channelId");
    when(channel.getContext()).thenReturn("siteId");

    when(mailArchiveService.channelReference("siteId", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId")).thenReturn(channel);

    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/simple-email.txt");

    verify(channel, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
}
 
Example #7
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testBadDomainCase() throws Exception {
    // Checks that we ignore the case on the domain
    when(serverConfigurationService.getServerName()).thenReturn("saKai.exAmple.cOm");
    User sender = mock(User.class);
    when(userDirectoryService.findUsersByEmail("[email protected]")).thenReturn(Collections.singleton(sender));

    MailArchiveChannel channel = mock(MailArchiveChannel.class);
    when(channel.getEnabled()).thenReturn(true);
    when(channel.getOpen()).thenReturn(false);
    when(channel.allowAddMessage(sender)).thenReturn(true);
    when(channel.getId()).thenReturn("channelId");
    when(channel.getContext()).thenReturn("siteId");

    when(mailArchiveService.channelReference("siteId", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId")).thenReturn(channel);

    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/simple-email.txt");

    verify(channel, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
}
 
Example #8
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
// Check that the BATV parsing is working correctly
public void testSiteIdAcceptBatv() throws Exception {
    User sender = mock(User.class);
    when(userDirectoryService.findUsersByEmail("[email protected]")).thenReturn(Collections.singleton(sender));

    MailArchiveChannel channel = mock(MailArchiveChannel.class);
    when(channel.getEnabled()).thenReturn(true);
    when(channel.getOpen()).thenReturn(false);
    when(channel.allowAddMessage(sender)).thenReturn(true);
    when(channel.getId()).thenReturn("channelId");
    when(channel.getContext()).thenReturn("siteId");

    when(mailArchiveService.channelReference("siteId", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId")).thenReturn(channel);

    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    // BATV is only in the envelope so the mail message should be the same.
    writeData(client, "/simple-email.txt");

    verify(channel, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
}
 
Example #9
Source File: ClientServerRelayTest.java    From mireka with Apache License 2.0 5 votes vote down vote up
@Test
public void testReceivingAndSending() throws IOException, SMTPException,
        IOException {
    SmartClient client = new SmartClient("localhost", 8025, "SmartClient");
    client.from("[email protected]");
    client.to("[email protected]");
    client.dataStart();
    byte[] exampleMail = ExampleMailData.simple().bytes;
    client.dataWrite(exampleMail, exampleMail.length);
    client.dataEnd();
    client.quit();

    WiserMessage message = wiser.getMessages().get(0);
    assertThat(message.getData(), new ArrayEndsWith(exampleMail));
}
 
Example #10
Source File: MessageHandlerTest.java    From subethasmtp with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisconnectImmediately() throws Exception {

	new Expectations() {
		{
			messageHandlerFactory.create((MessageContext) any);
			times = 0;
		}
	};

	SmartClient client = new SmartClient("localhost", smtpServer.getPort(),
			"localhost");
	client.quit();
	smtpServer.stop(); // wait for the server to catch up
}
 
Example #11
Source File: RelayFunctionalTest.java    From mireka with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuit() throws Exception {
    SmartClient smartClient = new SmartClient("localhost", 25, "localhost");
    smartClient.from("");
    smartClient.to("[email protected]");
    smartClient.dataStart();
    byte[] bytes = ExampleMailData.simple().bytes;
    smartClient.dataWrite(bytes, bytes.length);
    smartClient.dataEnd();
    smartClient.quit();

    assertTrue(quitReceived);
    assertEquals(1, wiser.getMessages().size());
}
 
Example #12
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test(expected = SMTPException.class)
public void testRejectedAddress() throws Exception {
    when(aliasService.getTarget("user")).thenThrow(IdUnusedException.class);
    when(rb.getString("err_addr_unknown")).thenReturn("err_addr_unknown");
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
}
 
Example #13
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test()
public void testIgnorePostmaster() throws Exception {
    when(aliasService.getTarget("postmaster")).thenThrow(IdUnusedException.class);
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/simple-email.txt");
}
 
Example #14
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testMultipleSiteIdAccept() throws Exception {
    User sender = mock(User.class);
    when(userDirectoryService.findUsersByEmail("[email protected]")).thenReturn(Collections.singleton(sender));

    MailArchiveChannel channel1 = mock(MailArchiveChannel.class);
    when(channel1.getEnabled()).thenReturn(true);
    when(channel1.getOpen()).thenReturn(false);
    when(channel1.allowAddMessage(sender)).thenReturn(true);
    when(channel1.getId()).thenReturn("channelId");
    when(channel1.getContext()).thenReturn("siteId1");

    MailArchiveChannel channel2 = mock(MailArchiveChannel.class);
    when(channel2.getEnabled()).thenReturn(true);
    when(channel2.getOpen()).thenReturn(false);
    when(channel2.allowAddMessage(sender)).thenReturn(true);
    when(channel2.getId()).thenReturn("channelId");
    when(channel2.getContext()).thenReturn("siteId2");

    when(mailArchiveService.channelReference("siteId1", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId1");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId1")).thenReturn(channel1);
    when(mailArchiveService.channelReference("siteId2", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId2");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId2")).thenReturn(channel2);

    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    client.to("[email protected]");
    writeData(client, "/simple-email.txt");

    verify(channel1, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
    verify(channel2, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
}
 
Example #15
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testIgnoreNoReply() throws Exception {
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/no-reply.txt");
}
 
Example #16
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test(expected = SMTPException.class)
public void testNoPermission() throws Exception {
    // Here there is no permission, this should never happen in real life
    String reference = "/mailarchive/no-permission/main";
    when(mailArchiveService.channelReference("no-permission", SiteService.MAIN_CONTAINER)).thenReturn(reference);
    when(mailArchiveService.getMailArchiveChannel(reference)).thenThrow(PermissionException.class);
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/no-permission.txt");
}
 
Example #17
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test(expected = SMTPException.class)
public void testNoReturnPath() throws Exception {
    // Here there isn't a good email in the from address to make a return path
    String reference = "/mailarchive/no-good-mail/main";
    SmartClient client = createClient();
    client.from("");
}
 
Example #18
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test(expected = SMTPException.class)
public void testEmptyReturnPath() throws Exception {
    // Here there isn't a good email in the from address to make a return path
    String reference = "/mailarchive/empty-return-path/main";
    SmartClient client = createClient();
    client.from("");
}
 
Example #19
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testPostmaster() throws Exception {
    // Here there is no postmaster which may happen and should be dealt with
    String reference = "/mailarchive/no-permission/main";
    when(mailArchiveService.channelReference("no-permission", SiteService.MAIN_CONTAINER)).thenReturn(reference);
    when(mailArchiveService.getMailArchiveChannel(reference)).thenThrow(PermissionException.class);
    when(userDirectoryService.getUser("postmaster")).thenThrow(UserNotDefinedException.class);
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/no-permission.txt");
}
 
Example #20
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void writeData(SmartClient client, String resource) throws IOException {
    client.dataStart();
    InputStream is = getClass().getResourceAsStream(resource);
    if(is == null) {
        throw new FileNotFoundException("Couldn't find in classpath: "+ resource);
    }
    byte[] bytes = IOUtils.toByteArray(is);
    client.dataWrite(bytes, bytes.length);
    client.dataEnd();
}
 
Example #21
Source File: MxPopTest.java    From mireka with Apache License 2.0 5 votes vote down vote up
private void sendMail() throws UnknownHostException, IOException,
        SMTPException {
    SmartClient client =
            new SmartClient("localhost", PORT_SMTP, "SmartClient");
    client.from("[email protected]");
    client.to("[email protected]");
    client.dataStart();
    byte[] exampleMail = ExampleMailData.simple().bytes;
    client.dataWrite(exampleMail, exampleMail.length);
    client.dataEnd();
    client.quit();
}
 
Example #22
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test(expected = SMTPException.class)
public void testRejectedAddress() throws Exception {
    when(aliasService.getTarget("user")).thenThrow(IdUnusedException.class);
    when(rb.getString("err_addr_unknown")).thenReturn("err_addr_unknown");
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
}
 
Example #23
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void writeData(SmartClient client, String resource) throws IOException {
    client.dataStart();
    InputStream is = getClass().getResourceAsStream(resource);
    if(is == null) {
        throw new FileNotFoundException("Couldn't find in classpath: "+ resource);
    }
    byte[] bytes = IOUtils.toByteArray(is);
    client.dataWrite(bytes, bytes.length);
    client.dataEnd();
}
 
Example #24
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test()
public void testIgnorePostmaster() throws Exception {
    when(aliasService.getTarget("postmaster")).thenThrow(IdUnusedException.class);
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/simple-email.txt");
}
 
Example #25
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testPostmaster() throws Exception {
    // Here there is no postmaster which may happen and should be dealt with
    String reference = "/mailarchive/no-permission/main";
    when(mailArchiveService.channelReference("no-permission", SiteService.MAIN_CONTAINER)).thenReturn(reference);
    when(mailArchiveService.getMailArchiveChannel(reference)).thenThrow(PermissionException.class);
    when(userDirectoryService.getUser("postmaster")).thenThrow(UserNotDefinedException.class);
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/no-permission.txt");
}
 
Example #26
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test(expected = SMTPException.class)
public void testEmptyReturnPath() throws Exception {
    // Here there isn't a good email in the from address to make a return path
    String reference = "/mailarchive/empty-return-path/main";
    SmartClient client = createClient();
    client.from("");
}
 
Example #27
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test(expected = SMTPException.class)
public void testNoReturnPath() throws Exception {
    // Here there isn't a good email in the from address to make a return path
    String reference = "/mailarchive/no-good-mail/main";
    SmartClient client = createClient();
    client.from("");
}
 
Example #28
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test(expected = SMTPException.class)
public void testNoPermission() throws Exception {
    // Here there is no permission, this should never happen in real life
    String reference = "/mailarchive/no-permission/main";
    when(mailArchiveService.channelReference("no-permission", SiteService.MAIN_CONTAINER)).thenReturn(reference);
    when(mailArchiveService.getMailArchiveChannel(reference)).thenThrow(PermissionException.class);
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/no-permission.txt");
}
 
Example #29
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testIgnoreNoReply() throws Exception {
    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    writeData(client, "/no-reply.txt");
}
 
Example #30
Source File: SakaiMessageHandlerTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testMultipleSiteIdAccept() throws Exception {
    User sender = mock(User.class);
    when(userDirectoryService.findUsersByEmail("[email protected]")).thenReturn(Collections.singleton(sender));

    MailArchiveChannel channel1 = mock(MailArchiveChannel.class);
    when(channel1.getEnabled()).thenReturn(true);
    when(channel1.getOpen()).thenReturn(false);
    when(channel1.allowAddMessage(sender)).thenReturn(true);
    when(channel1.getId()).thenReturn("channelId");
    when(channel1.getContext()).thenReturn("siteId1");

    MailArchiveChannel channel2 = mock(MailArchiveChannel.class);
    when(channel2.getEnabled()).thenReturn(true);
    when(channel2.getOpen()).thenReturn(false);
    when(channel2.allowAddMessage(sender)).thenReturn(true);
    when(channel2.getId()).thenReturn("channelId");
    when(channel2.getContext()).thenReturn("siteId2");

    when(mailArchiveService.channelReference("siteId1", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId1");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId1")).thenReturn(channel1);
    when(mailArchiveService.channelReference("siteId2", SiteService.MAIN_CONTAINER)).thenReturn("/site/siteId2");
    when(mailArchiveService.getMailArchiveChannel("/site/siteId2")).thenReturn(channel2);

    SmartClient client = createClient();
    client.from("[email protected]");
    client.to("[email protected]");
    client.to("[email protected]");
    writeData(client, "/simple-email.txt");

    verify(channel1, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
    verify(channel2, times(1)).addMailArchiveMessage(anyString(), eq("[email protected]"), any(), any(), any(), any());
}