comparison src/fschmidt/util/mail/javamail/Pop3ServerImpl.java @ 68:00520880ad02

add fschmidt source
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Oct 2025 17:24:15 -0600
parents
children
comparison
equal deleted inserted replaced
67:9d0fefce6985 68:00520880ad02
1 /*
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 package fschmidt.util.mail.javamail;
24
25 import java.util.NoSuchElementException;
26 import java.util.Properties;
27
28 import javax.mail.Flags;
29 import javax.mail.Folder;
30 import javax.mail.MessageRemovedException;
31 import javax.mail.MessagingException;
32 import javax.mail.Session;
33 import javax.mail.Store;
34 import javax.mail.internet.MimeMessage;
35
36 import fschmidt.util.mail.Mail;
37 import fschmidt.util.mail.MailException;
38 import fschmidt.util.mail.MailIterator;
39 import fschmidt.util.mail.Pop3Server;
40
41
42 final class Pop3ServerImpl implements Pop3Server {
43 private final Properties props = new Properties();
44 private Session session = null;
45 private final String host;
46 private final String username;
47 private final String password;
48 private boolean leaveOnServer = false;
49 private int maxMailCount = Integer.MAX_VALUE;
50 private Store store = null;
51
52 Pop3ServerImpl(String machineName,String username,String password) {
53 this.host = machineName;
54 this.username = username;
55 this.password = password;
56 }
57
58 public void setLeaveOnServer(boolean b) {
59 this.leaveOnServer = b;
60 }
61
62 public void setMailLimit(int maxMailCount) {
63 this.maxMailCount = maxMailCount;
64 }
65
66 public void useSsl() {
67 props.setProperty( "mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
68 props.setProperty( "mail.pop3.port", "995");
69 props.setProperty( "mail.pop3.socketFactory.port", "995");
70 }
71
72 public void setDebug(boolean b) {
73 props.setProperty("mail.debug", Boolean.toString(b));
74 }
75
76 private synchronized Session getSession() {
77 if( session==null ) {
78 session = Session.getInstance(props);
79 }
80 return session;
81 }
82
83 public synchronized MailIterator getMail() throws MailException {
84 if( store != null ) {
85 store = null;
86 throw new MailException("previous MailIterator not closed");
87 }
88 try {
89 store = getSession().getStore("pop3");
90 store.connect(host,username,password);
91 final Folder folder = store.getFolder("INBOX");
92 folder.open(Folder.READ_WRITE);
93 final int nMsgs = Math.min(maxMailCount,folder.getMessageCount());
94 return new MailIterator() {
95 int i = 1;
96 Mail nextMail = null;
97
98 public boolean hasNext() throws MailException {
99 //return i <= nMsgs;
100 while (nextMail==null && i<=nMsgs) {
101 try {
102 MimeMessage msg = (MimeMessage)folder.getMessage(i++);
103 if ( msg.isExpunged() || msg.isSet(Flags.Flag.DELETED) ) {
104 continue;
105 }
106 if ( !leaveOnServer ) {
107 msg.setFlag(Flags.Flag.DELETED,true);
108 }
109 nextMail = new MailImpl(msg);
110 } catch (MessageRemovedException e) {
111 // continue
112 } catch (MessagingException e) {
113 throw MailImpl.e(toString(),e);
114 }
115 }
116 return nextMail!=null;
117 }
118
119 public Mail next() throws MailException, NoSuchElementException {
120 try {
121 if (nextMail==null && !hasNext())
122 throw new NoSuchElementException();
123 return nextMail;
124 } finally {
125 nextMail=null;
126 }
127 }
128
129 public void close() throws MailException {
130 try {
131 if( folder.isOpen() )
132 folder.close(true);
133 store.close();
134 } catch(MessagingException e) {
135 throw MailImpl.e(toString(),e);
136 } finally {
137 store = null;
138 }
139 }
140 };
141 } catch(MessagingException e) {
142 cleanup();
143 throw MailImpl.e(toString(),e);
144 }
145 }
146
147 private void cleanup() {
148 if( store != null ) {
149 try {
150 store.close();
151 } catch(MessagingException e2) {}
152 store = null;
153 }
154 }
155
156 public String getUsername() {
157 return username;
158 }
159
160 public String toString() {
161 return "Pop3ServerImpl("+host+","+username+","+password+")";
162 }
163
164 }