BrightSide Workbench Full Report + Source Code
MailReader.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.mail.reader;
19 
20 import java.util.Properties;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import javax.mail.Flags;
24 import javax.mail.Folder;
25 import javax.mail.Message;
26 import javax.mail.MessagingException;
27 import javax.mail.Session;
28 import javax.mail.Store;
29 import org.turro.collections.KeyValueMap;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.impl.abstracts.AbstractImplementation;
32 
37 public class MailReader extends AbstractImplementation {
38 
39  private Store store;
40  private Folder emailFolder;
41 
42  public MailReader() {
43  }
44 
45  public boolean isReadOnly() {
46  return getAttributes().get("type").startsWith("pop3");
47  }
48 
49  public boolean isSeen(Message message) {
50  try {
51  return message.isSet(Flags.Flag.SEEN);
52  } catch (MessagingException ex) {
53  Logger.getLogger(MailReader.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
54  }
55  return false;
56  }
57 
58  public void seen(Message message) {
59  try {
60  message.setFlag(Flags.Flag.SEEN, true);
61  } catch (MessagingException ex) {
62  Logger.getLogger(MailReader.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
63  }
64  }
65 
66  public void delete(Message message) {
67  try {
68  message.setFlag(Flags.Flag.DELETED, true);
69  } catch (MessagingException ex) {
70  Logger.getLogger(MailReader.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
71  }
72  }
73 
74  public void close() {
75  try {
76  if (emailFolder != null) {
77  emailFolder.close(false);
78  }
79  if (store != null) {
80  store.close();
81  }
82  } catch (MessagingException ex) {
83  Logger.getLogger(MailReader.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
84  }
85  }
86 
87  public Message[] getMessages() {
88  try {
89  KeyValueMap kvm = getAttributes();
90  Properties properties = new Properties();
91 
92  if(kvm.get("type").startsWith("pop3")) {
93  properties.put("mail.pop3.host", kvm.get("mailhost"));
94  properties.put("mail.pop3.port", kvm.get("port"));
95  properties.put("mail.pop3.starttls.enable", kvm.get("tls"));
96  properties.put("mail.pop3.ssl.enable", kvm.get("ssl"));
97  } else if(kvm.get("type").startsWith("imap")) {
98  properties.put("mail.imap.host", kvm.get("mailhost"));
99  properties.put("mail.imap.port", kvm.get("port"));
100  properties.put("mail.imap.starttls.enable", kvm.get("tls"));
101  properties.put("mail.imap.ssl.enable", kvm.get("ssl"));
102  }
103  Session emailSession = Session.getDefaultInstance(properties);
104 
105  store = emailSession.getStore(kvm.get("type"));
106 
107  String password = kvm.get("password");
108  if (password == null) {
109  try {
110  password = ElephantContext.decrypt(kvm.get("cryptpass"));
111  } catch (Exception ex) {
112  Logger.getLogger(MailReader.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
113  }
114  }
115  store.connect(
116  kvm.get("mailhost"),
117  kvm.get(Integer.class, "port"),
118  kvm.get("user"),
119  password);
120 
121  emailFolder = store.getFolder(kvm.get(String.class, "folder", "INBOX"));
122  emailFolder.open(Folder.READ_WRITE);
123 
124  int count = kvm.get(Integer.class, "count", 50) - 1;
125 
126  int messageCount = emailFolder.getMessageCount();
127 
128  return emailFolder.getMessages(messageCount - count, messageCount);
129  } catch (MessagingException ex) {
130  Logger.getLogger(MailReader.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
131  }
132  return null;
133  }
134 
135 }
boolean isSeen(Message message)
Definition: MailReader.java:49
void seen(Message message)
Definition: MailReader.java:58