Search in sources :

Example 1 with SmtpConnection

use of com.zimbra.cs.mailclient.smtp.SmtpConnection in project zm-mailbox by Zimbra.

the class TestSmtpClient method testMimeMessage.

@Test
public void testMimeMessage() throws Exception {
    TestUtil.createAccount(USER_NAME);
    TestUtil.createAccount(USER2_NAME);
    // Assemble the message.
    MimeMessage mm = new ZMimeMessage(JMSession.getSession());
    InternetAddress addr = new JavaMailInternetAddress(TestUtil.getAddress(USER_NAME));
    mm.setFrom(addr);
    mm.setRecipient(RecipientType.TO, addr);
    String subject = NAME_PREFIX + " testMimeMessage";
    mm.setSubject(subject);
    mm.setText("testMimeMessage");
    mm.saveChanges();
    // Initialize SMTP client.
    SmtpConfig config = new SmtpConfig(mHost, mPort, "localhost");
    SmtpConnection conn = new SmtpConnection(config);
    conn.sendMessage(mm);
    // Make sure it arrived.
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    // Send the same message to a different envelope recipient.
    conn.sendMessage(addr.getAddress(), new String[] { TestUtil.getAddress(USER2_NAME) }, mm);
    mbox = TestUtil.getZMailbox(USER2_NAME);
    TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
}
Also used : SmtpConfig(com.zimbra.cs.mailclient.smtp.SmtpConfig) SmtpConnection(com.zimbra.cs.mailclient.smtp.SmtpConnection) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMailbox(com.zimbra.client.ZMailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Test(org.junit.Test)

Example 2 with SmtpConnection

use of com.zimbra.cs.mailclient.smtp.SmtpConnection in project zm-mailbox by Zimbra.

the class TestSmtpClient method sendAndVerify.

private void sendAndVerify(String sender, String[] recipients, String subject, String body, String expectedBody) throws Exception {
    // Fix up user names and subject.
    if (sender.indexOf("@") < 0) {
        sender = TestUtil.getAddress(sender);
    }
    for (int i = 0; i < recipients.length; i++) {
        if (recipients[i].indexOf("@") < 0) {
            recipients[i] = TestUtil.getAddress(recipients[i]);
        }
    }
    if (subject.indexOf(NAME_PREFIX) < 0) {
        subject = NAME_PREFIX + " " + subject;
    }
    // Send.
    String content = new MessageBuilder().withFrom(sender).withToRecipient(recipients[0]).withSubject(subject).withBody(body).create();
    SmtpConfig config = new SmtpConfig(mHost, mPort, "localhost");
    SmtpConnection smtp = new SmtpConnection(config);
    smtp.sendMessage(sender, recipients, content);
    // Verify.
    for (String recipient : recipients) {
        ZMailbox mbox = TestUtil.getZMailbox(recipient);
        ZMessage msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
        String currentBody = getBodyContent(msg.getMimeStructure());
        TestUtil.assertMessageContains(currentBody, expectedBody);
    }
}
Also used : ZMessage(com.zimbra.client.ZMessage) SmtpConfig(com.zimbra.cs.mailclient.smtp.SmtpConfig) SmtpConnection(com.zimbra.cs.mailclient.smtp.SmtpConnection) ZMailbox(com.zimbra.client.ZMailbox)

Aggregations

ZMailbox (com.zimbra.client.ZMailbox)2 SmtpConfig (com.zimbra.cs.mailclient.smtp.SmtpConfig)2 SmtpConnection (com.zimbra.cs.mailclient.smtp.SmtpConnection)2 ZMessage (com.zimbra.client.ZMessage)1 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)1 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 Test (org.junit.Test)1