我正在开发一个从因特网服务器下载/读取电子邮件的邮件程序(例如gmail,gmx,.)。因此,我需要:
respectively.
Message[]-Array存储/复制服务器Store-object中的收件箱文件夹,以连接到pop3 3-Server.Map<String,List<?>>以跟踪三个不同的列表,存储发件人-入口、发送日期和邮件主题由于每个方法都需要访问pop 3-服务器,因此创建了一个store-object并将其连接到pop-服务器。当在每个方法中分别创建store-object时,我会得到一个"javax.mail.AuthenticationFailedException“(太多的轴心会话)。我想,对于Message[]-Array中的每个邮件,都会创建一个会话对象,从而获得大约1.800次会话。是否有一种方法可以创建store-object或全局会话类--将相同的实例传递给每个方法?尤其是,对数组中的每个消息使用一个store-Object?
谢谢你的建议。
如果有必要的话,我不知道,但是我在eclipse上使用了javax.mail。
发布于 2022-09-06 05:43:07
哇,谢谢你的即时答复!所以我现在要做的是:
设置服务器-属性:
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[]-array:
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服务器的连接似乎在一段时间后自动关闭,所以我定期运行一个线程来检查连接是否仍然打开,如果没有,则重新连接:
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();
}
}
}}
https://stackoverflow.com/questions/73616905
复制相似问题