Mercurial Hosting > nabble
view src/fschmidt/util/mail/javamail/MstorInServer.java @ 69:4bc1fc540265 default tip
update luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 05 Oct 2025 20:45:39 -0600 |
parents | 00520880ad02 |
children |
line wrap: on
line source
/* Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package fschmidt.util.mail.javamail; import java.io.File; import java.io.IOException; import java.util.NoSuchElementException; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.PasswordAuthentication; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Folder; import javax.mail.Store; import javax.mail.URLName; import javax.mail.internet.MimeMessage; import fschmidt.util.mail.Mail; import fschmidt.util.mail.MailException; import fschmidt.util.mail.MailIterator; import fschmidt.util.mail.InServer; public final class MstorInServer implements InServer { private final Properties props = new Properties(); private Session session = null; private final File file; private Store store = null; public MstorInServer(File file) { this.file = file; } public void setMetaEnabled(boolean b) { props.setProperty( "mstor.meta.enabled", Boolean.toString(b) ); } private synchronized Session getSession() { if( session==null ) { session = Session.getInstance(props); } return session; } public synchronized MailIterator getMail() throws MailException { if( store != null ) { store = null; throw new MailException("previous MailIterator not closed"); } try { store = getSession().getStore(new URLName("mstor:"+file.getParentFile().getCanonicalPath())); store.connect(); final Folder folder = store.getFolder(file.getName()); folder.open(Folder.READ_ONLY); final int nMsgs = folder.getMessageCount(); //System.out.println("nMsgs="+nMsgs); return new MailIterator() { int i = 1; public boolean hasNext() { return i <= nMsgs; } public Mail next() throws MailException, NoSuchElementException { if( i > nMsgs ) throw new NoSuchElementException(); //System.out.println("i="+i); try { MimeMessage msg = (MimeMessage)folder.getMessage(i++); return new MailImpl(msg); } catch(MessagingException e) { throw MailImpl.e(toString(),e); } } public void close() throws MailException { try { folder.close(false); store.close(); } catch(MessagingException e) { throw MailImpl.e(toString(),e); } finally { store = null; } } }; } catch(IOException e) { cleanup(); throw new MailException(toString(),e); } catch(MessagingException e) { cleanup(); throw MailImpl.e(toString(),e); } } private void cleanup() { if( store != null ) { try { store.close(); } catch(MessagingException e2) {} store = null; } } public String toString() { return "MstorInServer("+file+")"; } }