当前位置: 首页>>代码示例>>Java>>正文


Java IMAPStore.close方法代码示例

本文整理汇总了Java中com.sun.mail.imap.IMAPStore.close方法的典型用法代码示例。如果您正苦于以下问题:Java IMAPStore.close方法的具体用法?Java IMAPStore.close怎么用?Java IMAPStore.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.sun.mail.imap.IMAPStore的用法示例。


在下文中一共展示了IMAPStore.close方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testSendAndReceiveWithQuotedAddress

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
@Test
public void testSendAndReceiveWithQuotedAddress() throws MessagingException, IOException {
    // See https://en.wikipedia.org/wiki/Email_address#Local-part
    String[] toList = {"\"John..Doe\"@localhost",
            "abc.\"defghi\"[email protected]",
            "\"abcdefghixyz\"@localhost",
            "\"Foo Bar\"[email protected]"
    };
    for(String to: toList) {
        greenMail.setUser(to, "pwd");
        InternetAddress[] toAddress = InternetAddress.parse(to);
        String from = to; // Same from and to address for testing correct escaping of both

        final String subject = "testSendAndReceiveWithQuotedAddress";
        final String content = "some body";
        GreenMailUtil.sendTextEmailTest(to, from,
                subject, content);

        assertTrue(greenMail.waitForIncomingEmail(5000, 1));

        final IMAPStore store = greenMail.getImap().createStore();
        store.connect(to, "pwd");
        try {
            IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            Message[] msgs = folder.getMessages();
            assertTrue(null != msgs && msgs.length == 1);
            final Message msg = msgs[0];
            assertEquals(to, ((InternetAddress)msg.getRecipients(Message.RecipientType.TO)[0]).getAddress());
            assertEquals(from, ((InternetAddress)msg.getFrom()[0]).getAddress());
            assertEquals(subject, msg.getSubject());
            assertEquals(content, msg.getContent().toString());
            assertArrayEquals(toAddress, msg.getRecipients(Message.RecipientType.TO));
        } finally {
            store.close();
        }
    }
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:39,代码来源:SenderRecipientTest.java

示例2: testQuotaCapability

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
@Test
public void testQuotaCapability() throws MessagingException {
    greenMail.setUser("[email protected]", "pwd");
    greenMail.setQuotaSupported(false);
    final IMAPStore store = greenMail.getImap().createStore();
    try {
        store.connect("[email protected]", "pwd");

        Quota testQuota = new Quota("INBOX");
        testQuota.setResourceLimit("STORAGE", 1024L * 42L);
        testQuota.setResourceLimit("MESSAGES", 5L);
        store.setQuota(testQuota);
        fail("Excepted MessageException since quota capability is turned off");
    } catch (MessagingException ex) {
        assertEquals(ex.getMessage(), "QUOTA not supported");
    } finally {
        store.close();
    }
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:20,代码来源:ImapServerTest.java

示例3: testSendAndReceive

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
@Test
public void testSendAndReceive() throws UnsupportedEncodingException, MessagingException, UserException {
    Session smtpSession = greenMail.getSmtp().createSession();

    Message msg = new MimeMessage(smtpSession);
    msg.setFrom(new InternetAddress("[email protected]"));
    msg.addRecipient(Message.RecipientType.TO,
            new InternetAddress("[email protected]"));
    msg.setSubject("Email sent to GreenMail via plain JavaMail");
    msg.setText("Fetch me via IMAP");
    Transport.send(msg);

    // Create user, as connect verifies pwd
    greenMail.setUser("[email protected]", "[email protected]", "secret-pwd");

    // Alternative 1: Create session and store or ...
    Session imapSession = greenMail.getImap().createSession();
    Store store = imapSession.getStore("imap");
    store.connect("[email protected]", "secret-pwd");
    Folder inbox = store.getFolder("INBOX");
    inbox.open(Folder.READ_ONLY);
    Message msgReceived = inbox.getMessage(1);
    assertEquals(msg.getSubject(), msgReceived.getSubject());

    // Alternative 2: ... let GreenMail create and configure a store:
    IMAPStore imapStore = greenMail.getImap().createStore();
    imapStore.connect("[email protected]", "secret-pwd");
    inbox = imapStore.getFolder("INBOX");
    inbox.open(Folder.READ_ONLY);
    msgReceived = inbox.getMessage(1);
    assertEquals(msg.getSubject(), msgReceived.getSubject());

    // Alternative 3: ... directly fetch sent message using GreenMail API
    assertEquals(1, greenMail.getReceivedMessagesForDomain("[email protected]").length);
    msgReceived = greenMail.getReceivedMessagesForDomain("[email protected]")[0];
    assertEquals(msg.getSubject(), msgReceived.getSubject());

    store.close();
    imapStore.close();
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:41,代码来源:ExampleJavaMailTest.java

示例4: testRenameINBOXFolder

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
/**
 * 
 * https://tools.ietf.org/html/rfc3501#page-37 :
 * <q>
 *     Renaming INBOX is permitted, and has special behavior.  It moves
 *     all messages in INBOX to a new mailbox with the given name,
 *     leaving INBOX empty.  If the server implementation supports
 *     inferior hierarchical names of INBOX, these are unaffected by a
 *     rename of INBOX.
 *  </q>
 *
 * @throws MessagingException
 * @throws InterruptedException
 */
@Test
public void testRenameINBOXFolder() throws MessagingException, InterruptedException {
    greenMail.setUser("[email protected]", "pwd");
    GreenMailUtil.sendTextEmail("[email protected]", "[email protected]", "Test subject",
            "Test message", greenMail.getSmtp().getServerSetup());

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("[email protected]", "pwd");
    try {

        // Create some folders
        Folder inboxFolder = store.getFolder("INBOX");
        assertTrue(inboxFolder.exists());
        inboxFolder.open(Folder.READ_ONLY);
        assertEquals(1, inboxFolder.getMessages().length);

        Folder inboxRenamedFolder = store.getFolder("INBOX-renamed");
        assertFalse(inboxRenamedFolder.exists());

        inboxFolder.close(true);
        inboxFolder.renameTo(inboxRenamedFolder);
        assertTrue(inboxRenamedFolder.exists());
        inboxRenamedFolder.open(Folder.READ_ONLY);
        assertEquals(1, inboxRenamedFolder.getMessages().length);

        inboxFolder = store.getFolder("INBOX");
        assertTrue(inboxFolder.exists());
        inboxFolder.open(Folder.READ_ONLY);
        assertEquals(0, inboxFolder.getMessages().length);
    } finally {
        store.close();
    }
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:48,代码来源:ImapServerTest.java

示例5: testFolderRequiringEscaping

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
@Test
public void testFolderRequiringEscaping() throws MessagingException {
    greenMail.setUser("[email protected]", "pwd");
    GreenMailUtil.sendTextEmail("[email protected]", "[email protected]", "test subject", "", greenMail.getSmtp().getServerSetup());

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("[email protected]", "pwd");
    try {

        // Create some folders
        Folder inboxFolder = store.getFolder("INBOX");
        inboxFolder.open(Folder.READ_ONLY);

        final Folder folderRequiringEscaping = inboxFolder.getFolder("requires escaping Ä");
        assertTrue(folderRequiringEscaping.create(Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES));
        folderRequiringEscaping.open(Folder.READ_WRITE);

        assertEquals(0, folderRequiringEscaping.getMessageCount());
        assertEquals(1, inboxFolder.getMessageCount());

        inboxFolder.copyMessages(inboxFolder.getMessages(), folderRequiringEscaping);

        folderRequiringEscaping.expunge(); // invalidates folder cache
        assertEquals(1, folderRequiringEscaping.getMessageCount());
    } finally {
        store.close();
    }
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:29,代码来源:ImapServerTest.java

示例6: testUIDExpunge

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
@Test
public void testUIDExpunge() throws MessagingException {
    greenMail.setUser("[email protected]", "pwd");

    // Create some test emails
    int numberOfEmails = 10;
    long[] uids = new long[numberOfEmails];
    for (int i = 0; i < numberOfEmails; i++) {
        GreenMailUtil.sendTextEmail("[email protected]", "[email protected]", "Test UID expunge #" + i,
                "Test message", greenMail.getSmtp().getServerSetup());
    }

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("[email protected]", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);

        Message[] messages = folder.getMessages();
        assertEquals(numberOfEmails, messages.length);

        // Mark even as deleted ...
        Message[] msgsForDeletion = new Message[uids.length / 2];
        for (int i = 0; i < messages.length; i++) {
            assertFalse(messages[i].getFlags().contains(Flags.Flag.DELETED));
            uids[i] = folder.getUID(messages[i]);
            if (i % 2 == 0) { // Deleted
                messages[i].setFlag(Flags.Flag.DELETED, true);
                msgsForDeletion[i / 2] = messages[i];
            }
        }

        // ... and expunge (with UID)
        folder.expunge(msgsForDeletion);

        // Check
        for (int i = 0; i < uids.length; i++) {
            final Message message = folder.getMessageByUID(uids[i]);
            if (i % 2 == 0) { // Deleted
                assertNull(message);
            } else {
                assertTrue("" + i, !message.isExpunged());
                assertTrue("" + i, !message.getFlags().contains(Flags.Flag.DELETED));
            }
        }
    } finally {
        store.close();
    }
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:50,代码来源:ImapServerTest.java

示例7: testAppend

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
@Test
public void testAppend() throws MessagingException {
    greenMail.setUser("[email protected]", "pwd");

    GreenMailUtil.sendTextEmail("[email protected]", "[email protected]", "Test Append",
            "Test message", greenMail.getSmtp().getServerSetup());

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("[email protected]", "pwd");
    try {
        IMAPFolder inboxFolder = (IMAPFolder) store.getFolder("INBOX");
        inboxFolder.open(Folder.READ_WRITE);

        Message[] messages = inboxFolder.getMessages();
        assertEquals(1, messages.length);
        Message message = messages[0];

        Message[] toBeAppended = new Message[]{
                new MimeMessage((MimeMessage) message) // Copy
        };
        toBeAppended[0].setSubject("testAppend#1");

        inboxFolder.appendMessages(toBeAppended);
        messages = inboxFolder.getMessages();
        assertEquals(2, messages.length);

        // UIDPLUS
        toBeAppended[0] = new MimeMessage((MimeMessage) message);
        toBeAppended[0].setSubject("testAppend#2");

        final AppendUID[] appendUIDs = inboxFolder.appendUIDMessages(toBeAppended); // Copy again
        long uid = appendUIDs[0].uid;
        Message newMsg = inboxFolder.getMessageByUID(uid);
        assertEquals(toBeAppended[0].getSubject(), newMsg.getSubject());
        assertTrue(appendUIDs[0].uidvalidity == inboxFolder.getUIDValidity());
        messages = inboxFolder.getMessages();
        assertEquals(3, messages.length);
    } finally {
        store.close();
    }
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:42,代码来源:ImapServerTest.java

示例8: testExpunge

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
@Test
public void testExpunge() throws MessagingException {
    greenMail.setUser("[email protected]", "pwd");

    for (int i = 0; i < 6; i++) {
        GreenMailUtil.sendTextEmail("[email protected]", "[email protected]", "Test subject #" + i,
                "Test message", ServerSetupTest.SMTP);
    }
    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("[email protected]", "pwd");
    try {
        Folder inboxFolder = store.getFolder("INBOX");
        inboxFolder.open(Folder.READ_WRITE);

        Message[] messages = inboxFolder.getMessages();
        assertEquals(6, messages.length);
        inboxFolder.setFlags(new int[]{2, 3}, new Flags(DELETED), true); // 1 and 2, offset is not zero-based

        assertEquals(false, inboxFolder.getMessage(1).isSet(DELETED));
        assertEquals(true, inboxFolder.getMessage(2).isSet(DELETED));
        assertEquals(true, inboxFolder.getMessage(3).isSet(DELETED));
        assertEquals(false, inboxFolder.getMessage(4).isSet(DELETED));
        assertEquals(false, inboxFolder.getMessage(5).isSet(DELETED));
        assertEquals(false, inboxFolder.getMessage(6).isSet(DELETED));
        assertEquals(2, inboxFolder.getDeletedMessageCount());
        Message[] expunged = inboxFolder.expunge();
        assertEquals(2, expunged.length);

        messages = inboxFolder.getMessages();
        assertEquals(4, messages.length);
        assertEquals("Test subject #0", messages[0].getSubject());
        assertEquals("Test subject #3", messages[1].getSubject());
        assertEquals("Test subject #4", messages[2].getSubject());
        assertEquals("Test subject #5", messages[3].getSubject());
    } finally {
        store.close();
    }
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:39,代码来源:ImapServerTest.java

示例9: testForwardWithRfc822

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
/**
 * Structure of test message and content type:
 * <p>
 * Message (multipart/mixed)
 * \--> MultiPart (multipart/mixed)
 * \--> MimeBodyPart (message/rfc822)
 * \--> Message (text/plain)
 */
@Test
public void testForwardWithRfc822() throws MessagingException, IOException {
    greenMail.setUser("[email protected]", "pwd");
    final Session session = greenMail.getSmtp().createSession();

    // Message for forwarding
    Message msgToBeForwarded = GreenMailUtil.createTextEmail(
            "[email protected]", "[email protected]", "test newMessageWithForward", "forwarded mail content",
            greenMail.getSmtp().getServerSetup());


    // Create body part containing forwarded message
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(msgToBeForwarded, "message/rfc822");

    // Add message body part to multi part
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // New main message, containing body part
    MimeMessage newMessageWithForward = new MimeMessage(session);
    newMessageWithForward.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
    newMessageWithForward.setSubject("Fwd: " + "test");
    newMessageWithForward.setFrom(new InternetAddress("[email protected]"));
    newMessageWithForward.setContent(multipart);   //Save changes in newMessageWithForward message
    newMessageWithForward.saveChanges();

    GreenMailUtil.sendMimeMessage(newMessageWithForward);

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("[email protected]", "pwd");
    try {
        Folder inboxFolder = store.getFolder("INBOX");
        inboxFolder.open(Folder.READ_WRITE);
        Message[] messages = inboxFolder.getMessages();
        MimeMessage msg = (MimeMessage) messages[0];
        assertTrue(msg.getContentType().startsWith("multipart/mixed"));
        Multipart multipartReceived = (Multipart) msg.getContent();
        assertTrue(multipartReceived.getContentType().startsWith("multipart/mixed"));
        MimeBodyPart mimeBodyPartReceived = (MimeBodyPart) multipartReceived.getBodyPart(0);
        assertTrue(mimeBodyPartReceived.getContentType().toLowerCase().startsWith("message/rfc822"));

        MimeMessage msgAttached = (MimeMessage) mimeBodyPartReceived.getContent();
        assertThat(msgAttached.getContentType().toLowerCase(), startsWith("text/plain"));
        assertArrayEquals(msgToBeForwarded.getRecipients(Message.RecipientType.TO), msgAttached.getRecipients(Message.RecipientType.TO));
        assertArrayEquals(msgToBeForwarded.getFrom(), msgAttached.getFrom());
        assertEquals(msgToBeForwarded.getSubject(), msgAttached.getSubject());
        assertEquals(msgToBeForwarded.getContent(), msgAttached.getContent());
    } finally {
        store.close();
    }
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:61,代码来源:Rfc822MessageTest.java

示例10: testRenameFolder

import com.sun.mail.imap.IMAPStore; //导入方法依赖的package包/类
@Test
public void testRenameFolder() throws MessagingException, InterruptedException {
    greenMail.setUser("[email protected]", "pwd");

    final IMAPStore store = greenMail.getImap().createStore();
    store.connect("[email protected]", "pwd");
    try {

        // Create some folders
        Folder inboxFolder = store.getFolder("INBOX");
        Folder newFolder = inboxFolder.getFolder("foo-folder");
        assertTrue(newFolder.create(Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES));
        assertTrue(newFolder.exists());

        Folder renamedFolder = inboxFolder.getFolder("foo-folder-renamed");
        assertTrue(!renamedFolder.exists());

        // Rename
        assertTrue(newFolder.renameTo(renamedFolder));
        assertTrue(!newFolder.exists());
        assertTrue(renamedFolder.exists());

        // Rename with sub folder
        Folder subFolder = renamedFolder.getFolder("bar");
        assertTrue(subFolder.create(Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES));
        assertTrue(subFolder.exists());

        Folder renamedFolder2 = inboxFolder.getFolder("foo-folder-renamed-again");
        assertTrue(renamedFolder.renameTo(renamedFolder2));
        assertTrue(!renamedFolder.exists());
        assertTrue(renamedFolder2.exists());
        assertTrue(renamedFolder2.getFolder("bar").exists()); // check that sub folder still exists

        // Rename to a different parent folder
        // INBOX.foo-folder-renamed-again -> INBOX.foo2.foo3
        Folder foo2Folder = inboxFolder.getFolder("foo2");
        assertTrue(foo2Folder.create(Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES));
        assertTrue(foo2Folder.exists());
        Folder foo3Folder = foo2Folder.getFolder("foo3");
        assertTrue(!foo3Folder.exists());

        renamedFolder2.renameTo(foo3Folder);
        assertTrue(inboxFolder.getFolder("foo2.foo3").exists());
        assertTrue(!inboxFolder.getFolder("foo-folder-renamed-again").exists());
    } finally {
        store.close();
    }
}
 
开发者ID:greenmail-mail-test,项目名称:greenmail,代码行数:49,代码来源:ImapServerTest.java


注:本文中的com.sun.mail.imap.IMAPStore.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。