Example usage for org.apache.commons.net.smtp SMTPClient setSender

List of usage examples for org.apache.commons.net.smtp SMTPClient setSender

Introduction

In this page you can find the example usage for org.apache.commons.net.smtp SMTPClient setSender.

Prototype

public boolean setSender(String address) throws IOException 

Source Link

Document

Set the sender of a message using the SMTP MAIL command, specifying the sender's email address.

Usage

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testReceivedHeader() throws Exception {
    init(smtpConfiguration);//ww w  .  j  a  v  a 2  s . c  o  m

    SMTPClient smtp = newSMTPClient();

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    smtp.helo(InetAddress.getLocalHost().toString());
    smtp.setSender("mail@localhost");
    smtp.addRecipient("mail@localhost");
    smtp.sendShortMessageData("Subject: test\r\n\r\n");

    smtp.quit();
    smtp.disconnect();

    assertNotNull("spooled mail has Received header", queue.getLastMail().getMessage().getHeader("Received"));
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Ignore
@Test// w  w w . j av a 2s .  com
public void testEmptyMessageReceivedHeader() throws Exception {
    init(smtpConfiguration);

    SMTPClient smtp = newSMTPClient();

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    smtp.helo(InetAddress.getLocalHost().toString());
    smtp.setSender("mail@localhost");
    smtp.addRecipient("mail@localhost");
    smtp.sendShortMessageData("");

    smtp.quit();
    smtp.disconnect();

    assertNotNull("spooled mail has Received header", queue.getLastMail().getMessage().getHeader("Received"));
    // TODO: test body size
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testSimpleMailSendWithHELO() throws Exception {
    init(smtpConfiguration);/* w  w  w  . ja  v a 2  s . c  o  m*/

    SMTPClient smtpProtocol = new SMTPClient();
    smtpProtocol.connect("127.0.0.1", smtpListenerPort);

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    smtpProtocol.helo(InetAddress.getLocalHost().toString());

    smtpProtocol.setSender("mail@localhost");

    smtpProtocol.addRecipient("mail@localhost");

    smtpProtocol
            .sendShortMessageData("Subject: test mail\r\n\r\nTest body testSimpleMailSendWithHELO\r\n.\r\n");

    smtpProtocol.quit();
    smtpProtocol.disconnect();

    // mail was propagated by SMTPServer
    assertNotNull("mail received by mail server", queue.getLastMail());
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testTwoSimultaneousMails() throws Exception {
    init(smtpConfiguration);//from   w w w .  ja  v a2 s .  c om

    SMTPClient smtpProtocol1 = new SMTPClient();
    smtpProtocol1.connect("127.0.0.1", smtpListenerPort);
    SMTPClient smtpProtocol2 = new SMTPClient();
    smtpProtocol2.connect("127.0.0.1", smtpListenerPort);

    assertTrue("first connection taken", smtpProtocol1.isConnected());
    assertTrue("second connection taken", smtpProtocol2.isConnected());

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());
    smtpProtocol2.helo(InetAddress.getLocalHost().toString());

    String sender1 = "mail_sender1@localhost";
    String recipient1 = "mail_recipient1@localhost";
    smtpProtocol1.setSender(sender1);
    smtpProtocol1.addRecipient(recipient1);

    String sender2 = "mail_sender2@localhost";
    String recipient2 = "mail_recipient2@localhost";
    smtpProtocol2.setSender(sender2);
    smtpProtocol2.addRecipient(recipient2);

    smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoSimultaneousMails1\r\n.\r\n");
    verifyLastMail(sender1, recipient1, null);

    smtpProtocol2.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoSimultaneousMails2\r\n.\r\n");
    verifyLastMail(sender2, recipient2, null);

    smtpProtocol1.quit();
    smtpProtocol2.quit();

    smtpProtocol1.disconnect();
    smtpProtocol2.disconnect();
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testTwoMailsInSequence() throws Exception {
    init(smtpConfiguration);// w  ww.j  ava  2s.c  o m

    SMTPClient smtpProtocol1 = new SMTPClient();
    smtpProtocol1.connect("127.0.0.1", smtpListenerPort);

    assertTrue("first connection taken", smtpProtocol1.isConnected());

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());

    String sender1 = "mail_sender1@localhost";
    String recipient1 = "mail_recipient1@localhost";
    smtpProtocol1.setSender(sender1);
    smtpProtocol1.addRecipient(recipient1);

    smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoMailsInSequence1\r\n");
    verifyLastMail(sender1, recipient1, null);

    String sender2 = "mail_sender2@localhost";
    String recipient2 = "mail_recipient2@localhost";
    smtpProtocol1.setSender(sender2);
    smtpProtocol1.addRecipient(recipient2);

    smtpProtocol1.sendShortMessageData("Subject: test2\r\n\r\nTest body2 testTwoMailsInSequence2\r\n");
    verifyLastMail(sender2, recipient2, null);

    smtpProtocol1.quit();
    smtpProtocol1.disconnect();
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

private void doTestHeloEhloResolv(String heloCommand) throws IOException {
    SMTPClient smtpProtocol = new SMTPClient();
    smtpProtocol.connect("127.0.0.1", smtpListenerPort);

    assertTrue("first connection taken", smtpProtocol.isConnected());

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    String fictionalDomain = "abgsfe3rsf.de";
    String existingDomain = "james.apache.org";
    String mail = "sender@james.apache.org";
    String rcpt = "rcpt@localhost";

    smtpProtocol.sendCommand(heloCommand, fictionalDomain);
    smtpProtocol.setSender(mail);
    smtpProtocol.addRecipient(rcpt);/*from  w w w. j  a va 2s .c o  m*/

    // this should give a 501 code cause the helo/ehlo could not resolved
    assertEquals("expected error: " + heloCommand + " could not resolved", 501, smtpProtocol.getReplyCode());

    smtpProtocol.sendCommand(heloCommand, existingDomain);
    smtpProtocol.setSender(mail);
    smtpProtocol.addRecipient(rcpt);

    if (smtpProtocol.getReplyCode() == 501) {
        fail(existingDomain
                + " domain currently cannot be resolved (check your DNS/internet connection/proxy settings to make test pass)");
    }
    // helo/ehlo is resolvable. so this should give a 250 code
    assertEquals(heloCommand + " accepted", 250, smtpProtocol.getReplyCode());

    smtpProtocol.quit();
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testReverseEqualsHelo() throws Exception {
    smtpConfiguration.setReverseEqualsHelo();
    smtpConfiguration.setAuthorizedAddresses("192.168.0.1");
    // temporary alter the loopback resolution
    try {//from   www . j  a v  a  2  s.c om
        dnsServer.setLocalhostByName(InetAddress.getByName("james.apache.org"));
    } catch (UnknownHostException e) {
        fail("james.apache.org currently cannot be resolved (check your DNS/internet connection/proxy settings to make test pass)");
    }
    try {
        init(smtpConfiguration);

        SMTPClient smtpProtocol1 = new SMTPClient();
        smtpProtocol1.connect("127.0.0.1", smtpListenerPort);

        assertTrue("first connection taken", smtpProtocol1.isConnected());

        // no message there, yet
        assertNull("no mail received by mail server", queue.getLastMail());

        String helo1 = "abgsfe3rsf.de";
        String helo2 = "james.apache.org";
        String mail = "sender";
        String rcpt = "recipient";

        smtpProtocol1.sendCommand("helo", helo1);
        smtpProtocol1.setSender(mail);
        smtpProtocol1.addRecipient(rcpt);

        // this should give a 501 code cause the helo not equal reverse of
        // ip
        assertEquals("expected error: helo not equals reverse of ip", 501, smtpProtocol1.getReplyCode());

        smtpProtocol1.sendCommand("helo", helo2);
        smtpProtocol1.setSender(mail);
        smtpProtocol1.addRecipient(rcpt);

        // helo is resolvable. so this should give a 250 code
        assertEquals("Helo accepted", 250, smtpProtocol1.getReplyCode());

        smtpProtocol1.quit();
    } finally {
        dnsServer.setLocalhostByName(null);
    }
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testSenderDomainResolv() throws Exception {
    smtpConfiguration.setSenderDomainResolv();
    smtpConfiguration.setAuthorizedAddresses("192.168.0.1/32");
    init(smtpConfiguration);/*from  w ww  . j a v a2 s.  c o m*/

    SMTPClient smtpProtocol1 = new SMTPClient();
    smtpProtocol1.connect("127.0.0.1", smtpListenerPort);

    assertTrue("first connection taken", smtpProtocol1.isConnected());

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());

    String sender1 = "mail_sender1@xfwrqqfgfe.de";

    smtpProtocol1.setSender(sender1);
    assertEquals("expected 501 error", 501, smtpProtocol1.getReplyCode());

    smtpProtocol1.addRecipient("test@localhost");
    assertEquals("Recipient not accepted cause no valid sender", 503, smtpProtocol1.getReplyCode());
    smtpProtocol1.quit();

}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testSenderDomainResolvDefault() throws Exception {
    init(smtpConfiguration);//from  w w  w.  j  ava2s  .  com

    SMTPClient smtpProtocol1 = new SMTPClient();
    smtpProtocol1.connect("127.0.0.1", smtpListenerPort);

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());

    String sender1 = "mail_sender1@xfwrqqfgfe.de";

    smtpProtocol1.setSender(sender1);

    smtpProtocol1.quit();
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testSenderDomainResolvRelayClientDefault() throws Exception {
    smtpConfiguration.setSenderDomainResolv();
    init(smtpConfiguration);//from   w w w . j  a va  2 s. com

    SMTPClient smtpProtocol1 = new SMTPClient();
    smtpProtocol1.connect("127.0.0.1", smtpListenerPort);

    assertTrue("first connection taken", smtpProtocol1.isConnected());

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());

    String sender1 = "mail_sender1@xfwrqqfgfe.de";

    // Both mail shold
    smtpProtocol1.setSender(sender1);

    smtpProtocol1.quit();

}