-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetState.java
159 lines (136 loc) · 3.89 KB
/
NetState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cs241;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.protocol.IMAPProtocol;
import java.util.ArrayList;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.MimeMessage;
/**
*
* @author Austin Lund
*/
public class NetState {
static String protocol;
static String host;
static int port;
static int sendPort;
String username;
static int keepAlive;
static boolean imaptls;
static boolean smtptls;
static boolean poptls;
ArrayList<Message> messages;
Session session;
Store store;
Folder inbox;
static{
protocol="imap";
host="mymail.clarkson.edu";
port=143;
sendPort=587;
keepAlive = 300000;
imaptls=true;
poptls=true;
smtptls=true;
}
/**
* Create a new Netstate with a given username.
* @param uname username to use
*/
public NetState(String uname){
session=null;
store=null;
inbox=null;
username=uname;
messages=new ArrayList();
}
/**
* Get a new blank message to be filled and sent
* @return an empty message
*/
public MimeMessage getBlank(){
return new MimeMessage(session);
}
/**
* Sends an EMail.
* @param m the Message to be sent
* @param pass password for reauthentication
* @return always returns false
* @throws Exception handle possible network exceptions
*/
public boolean sendMail(Message m, String pass) throws Exception{
Transport trans = session.getTransport();
trans.connect(host,sendPort, username, pass);
//m.setFrom(new InternetAddress(username));
trans.sendMessage(m, m.getAllRecipients());
return false;
}
/**
* Reloads the inbox.
*/
public void reload(){
messages.clear();
try{
Message[] mm=inbox.getMessages();
for(Message n: mm)
messages.add(n);
}catch(Exception e){}
}
/**
* Starts a connection and populates the inbox.
* This should be called almost imeadiatly after creation.
* @param password password for authentication
* @return true if connection was successful
*/
public boolean startSession(String password){
Properties props = new Properties();
props.setProperty("mail.store.protocol", protocol);
//props.put("mail.debug", "true");
if(imaptls)
props.put("mail.imap.starttls.enable","true");
if(smtptls)
props.put("mail.smtp.starttls.enable","true");
if(poptls)
props.put("mail.pop3.starttls.enable","true");
try {
session = Session.getInstance(props, null);
store = session.getStore();
store.connect(host,port, username, password);
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] mm=inbox.getMessages();
for(Message n: mm)
messages.add(n);
if(inbox instanceof IMAPFolder)
new HeartBeet();
return true;
} catch (Exception mex) {
mex.printStackTrace();
}
return false;
}
/**
* Thread to maintain connection
*/
private class HeartBeet extends Thread{
public HeartBeet(){
start();
}
@Override
public void run(){
IMAPFolder fold;
fold=(IMAPFolder)inbox;
while(true){
try{
Thread.sleep(keepAlive);
// Perform a NOOP just to keep alive the connection
fold.doCommand((IMAPProtocol p) -> {
p.simpleCommand("NOOP", null);
return null;
});
}catch (InterruptedException e){}
catch (MessagingException e){}
}
}
}
}