Search in sources :

Example 1 with Store

use of jakarta.mail.Store in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testAttachmentsGuts.

private Folder testAttachmentsGuts(final ImapMailReceiver receiver) throws MessagingException, IOException {
    Store store = mock(Store.class);
    Folder folder = mock(Folder.class);
    given(folder.exists()).willReturn(true);
    given(folder.isOpen()).willReturn(true);
    Message message = new MimeMessage(null, new ClassPathResource("test.mail").getInputStream());
    given(folder.search(Mockito.any())).willReturn(new Message[] { message });
    given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    DirectFieldAccessor df = new DirectFieldAccessor(receiver);
    df.setPropertyValue("store", store);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    return folder;
}
Also used : MimeMessage(jakarta.mail.internet.MimeMessage) Message(jakarta.mail.Message) MimeMessage(jakarta.mail.internet.MimeMessage) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) URLName(jakarta.mail.URLName) BeanFactory(org.springframework.beans.factory.BeanFactory) Store(jakarta.mail.Store) Flags(jakarta.mail.Flags) Folder(jakarta.mail.Folder) IMAPFolder(com.sun.mail.imap.IMAPFolder) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 2 with Store

use of jakarta.mail.Store in project spring-integration by spring-projects.

the class MailReceiverTests method testStoreConnect.

@Test
public void testStoreConnect() throws Exception {
    AbstractMailReceiver receiver = new AbstractMailReceiver() {

        @Override
        protected Message[] searchForNewMessages() {
            return null;
        }
    };
    Properties props = new Properties();
    Session session = Session.getInstance(props);
    receiver.setSession(session);
    receiver.setProtocol("imap");
    Store store = session.getStore("imap");
    store = spy(store);
    new DirectFieldAccessor(receiver).setPropertyValue("store", store);
    when(store.isConnected()).thenReturn(false);
    Folder folder = mock(Folder.class);
    when(folder.exists()).thenReturn(true);
    when(folder.isOpen()).thenReturn(false);
    doReturn(folder).when(store).getFolder((URLName) null);
    doNothing().when(store).connect();
    receiver.openFolder();
    receiver.openFolder();
    verify(store, times(2)).connect();
}
Also used : Message(jakarta.mail.Message) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Store(jakarta.mail.Store) Properties(java.util.Properties) Folder(jakarta.mail.Folder) Session(jakarta.mail.Session) Test(org.junit.jupiter.api.Test)

Example 3 with Store

use of jakarta.mail.Store in project exist by eXist-db.

the class MailStoreFunctions method getMailStore.

private Sequence getMailStore(Sequence[] args, Sequence contextSequence) throws XPathException {
    Store store;
    // was a session handle specified?
    if (args[0].isEmpty()) {
        throw (new XPathException(this, "Session handle not specified"));
    }
    // get the Session
    long sessionHandle = ((IntegerValue) args[0].itemAt(0)).getLong();
    Session session = MailModule.retrieveSession(context, sessionHandle);
    if (session == null) {
        throw (new XPathException(this, "Invalid Session handle specified"));
    }
    try {
        String password = session.getProperty("mail." + session.getProperty("mail.store.protocol") + ".password");
        if (password == null) {
            password = session.getProperty("mail.password");
        }
        store = session.getStore();
        store.connect(null, null, password);
    } catch (MessagingException me) {
        throw (new XPathException(this, "Failed to open mail store", me));
    }
    return (new IntegerValue(MailModule.storeStore(context, store)));
}
Also used : XPathException(org.exist.xquery.XPathException) MessagingException(jakarta.mail.MessagingException) IntegerValue(org.exist.xquery.value.IntegerValue) Store(jakarta.mail.Store) Session(jakarta.mail.Session)

Example 4 with Store

use of jakarta.mail.Store in project exist by eXist-db.

the class MailStoreFunctions method closeMailStore.

private Sequence closeMailStore(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was a store handle specified?
    if (args[0].isEmpty()) {
        throw (new XPathException(this, "Store handle not specified"));
    }
    // get the Store
    long storeHandle = ((IntegerValue) args[0].itemAt(0)).getLong();
    Store store = MailModule.retrieveStore(context, storeHandle);
    if (store == null) {
        throw (new XPathException(this, "Invalid Store handle specified"));
    }
    try {
        store.close();
    } catch (MessagingException me) {
        throw (new XPathException(this, "Failed to close mail store", me));
    } finally {
        MailModule.removeStore(context, storeHandle);
    }
    return (Sequence.EMPTY_SEQUENCE);
}
Also used : XPathException(org.exist.xquery.XPathException) MessagingException(jakarta.mail.MessagingException) IntegerValue(org.exist.xquery.value.IntegerValue) Store(jakarta.mail.Store)

Example 5 with Store

use of jakarta.mail.Store in project exist by eXist-db.

the class MailFolderFunctions method getMailFolder.

private Sequence getMailFolder(Sequence[] args, Sequence contextSequence) throws XPathException {
    Folder folder;
    // was a store handle specified?
    if (args[0].isEmpty() || args[1].isEmpty()) {
        throw (new XPathException(this, "Store handle and/or folder name not specified"));
    }
    // get the Store
    long storeHandle = ((IntegerValue) args[0].itemAt(0)).getLong();
    Store store = MailModule.retrieveStore(context, storeHandle);
    if (store == null) {
        throw (new XPathException(this, "Invalid Store handle specified"));
    }
    // get the Folder Name
    String name = args[1].getStringValue();
    try {
        folder = store.getFolder(name);
        folder.open(Folder.READ_WRITE);
    } catch (MessagingException me) {
        throw (new XPathException(this, "Failed to open mail folder", me));
    }
    return (new IntegerValue(MailModule.storeFolder(context, folder)));
}
Also used : XPathException(org.exist.xquery.XPathException) MessagingException(jakarta.mail.MessagingException) IntegerValue(org.exist.xquery.value.IntegerValue) Store(jakarta.mail.Store) Folder(jakarta.mail.Folder)

Aggregations

Store (jakarta.mail.Store)9 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 IMAPFolder (com.sun.mail.imap.IMAPFolder)5 Flags (jakarta.mail.Flags)5 URLName (jakarta.mail.URLName)5 Test (org.junit.jupiter.api.Test)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)4 Folder (jakarta.mail.Folder)4 Message (jakarta.mail.Message)4 MessagingException (jakarta.mail.MessagingException)4 MimeMessage (jakarta.mail.internet.MimeMessage)3 Field (java.lang.reflect.Field)3 XPathException (org.exist.xquery.XPathException)3 IntegerValue (org.exist.xquery.value.IntegerValue)3 FolderClosedException (jakarta.mail.FolderClosedException)2 Session (jakarta.mail.Session)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 AddressException (jakarta.mail.internet.AddressException)1