[英]mail sessions - too many active sessions
我正在嘗試開發一個郵件程序來從 Internet 服務器下載/讀取電子郵件(例如 gmail,gmx,...)。 因此我需要:
Message[]
-Array 用於從服務器存儲/復制收件箱文件夾Store
-object 連接到 pop3-Server。Map<String,List<?>>
來跟蹤三個不同的列表,分別存儲發件人地址、發送日期和郵件主題。 由於這些方法中的每一個都需要訪問 pop3-server,因此會創建一個store
對象並將其連接到 pop-server。 在每種方法中分別創建store
對象時,我得到一個“javax.mail.AuthenticationFailedException”(太多的 axctive 會話)。 我猜,對於Message[]
-Array 中的每封郵件,都會創建一個會話對象,從而獲得大約 1.800 個會話。 有沒有辦法在全局范圍內創建store
對象或 session 類,並將相同的實例傳遞給每個方法? 並且,特別是為數組中的每條消息使用一個store
對象?
感謝您的任何提示。
PS.:如果有必要提及,不知道,但我在 eclipse-IDE 上使用 javax.mail
哇,感謝您的即時回答:所以這就是我現在正在做的事情:
設置服務器屬性:
protected Properties getServerProperties(String protocol, String host, String port){
Properties properties = new Properties();
properties.put(String.format("mail.%s.host", protocol), host);
properties.put(String.format("mail.%s.port", protocol), port);
properties.setProperty(String.format("mail.%s.socketFactory.class", protocol), "javax.net.ssl.SSLSocketFactory");
properties.setProperty(String.format("mail.%s.socketFactory.fallback", protocol), "false");
properties.setProperty(String.format("mail.%s.socketFactory.port", protocol), String.valueOf(port));
properties.setProperty("mail.pop3.port", "995");
properties.setProperty("mail.pop3.host", "pop.gmx.net");
properties.setProperty("mail.pop3.user", "****");
properties.setProperty("mail.pop3.ssl.protocols", "TLSv1.2");
return properties;
}
創建 Message[] 數組:
public Message[] return_messages(String protocol, String host, String port, String userName, String password) throws MessagingException {
Properties properties = getServerProperties(protocol, host, port);
Session session = Session.getInstance(properties, auth);
Store store = session.getStore(protocol);
store.connect("pop.gmx.net", userName, password);
folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);
Message[] messages = folderInbox.getMessages();
return messages;
}
打開郵件文件夾
public Store open_mail_folder(String protocol, String host, String port, String userName, String password) throws MessagingException {
Properties properties = getServerProperties(protocol, host, port);
Session session = Session.getInstance(properties, auth);
Store store = session.getStore(protocol);
store.connect("pop.gmx.net", userName, password);
return store;
}
將郵件存儲在列表中
Map<String,List<?>> map = new HashMap<String, List<?>>();
//@SuppressWarnings("static-access")
public Map<String, List<?>> downloadEmails(String protocol, String host, String port, String userName, String password) throws IOException {
Properties properties = getServerProperties(protocol, host, port);
Session session = Session.getInstance(properties, auth);
try {
Store store = null;
if(store == null) {
store = session.getStore(protocol);
store.connect("pop.gmx.net", userName, password);
}
folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);
Message[] messages = folderInbox.getMessages();
List<String> fromList = new ArrayList<String>();
List<Date> dateList = new ArrayList<Date>();
List<String> subjectList = new ArrayList<String>();
for(int i=0;i<messages.length;i++) {
Address[] from = messages[i].getFrom();
String from_temp = (String)(((InternetAddress)from[0]).getAddress());
fromList.add(from_temp);
Date date = messages[i].getSentDate();
dateList.add(date);
String subject = messages[i].getSubject();
subjectList.add(subject);
}
map.put("fromList", fromList);
map.put("dateList", dateList);
map.put("subjectList", subjectList);
String result = "";
StringBuilder textBuilder = new StringBuilder();
}catch (NoSuchProviderException ex) {
//for debugging only
System.out.println("No provider for protocol " + protocol);
ex.printStackTrace();
}catch (MessagingException ex) {
System.out.println("Could not connect to the message store");
ex.printStackTrace();
}
return map;
}
此外,由於與 pop-server 的連接似乎在一段時間后自動關閉,因此我定期運行一個線程來檢查連接是否仍然打開,或者如果沒有,則重新連接:
public class Session_relog extends Mail_Reader implements Runnable {
static int session_count;
public Session_relog() {
// TODO Auto-generated constructor stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
try {
Thread.sleep(30,000);
if(super.isopen_mail_folder("pop3", "pop.gmx.net", "955", "****", "****")==false) {
try {
super.open_mail_folder("pop3", "pop.gmx.net", "995", "****", "****");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
Thread.sleep(30,000);
continue;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.