我已经使用连接池来重用store.connect()。它对单个用户有效,但对多个用户无效。
对于多个用户,它为所有其他用户获取第一个加载的用户数据。 如何解决这个问题。
下面是代码,
public class EmailListingController {
private static EmailListingController instance = null;
private static Store store = null;
public EmailListingController() {
super();
}
private EmailListingController(String host, String mailstoreType, String username, String originalPassword,
int port, boolean auth, boolean security, User user) throws MessagingException {
System.out.println("props + store connect");
Properties properties = new Properties();
properties.put("mail.store.protocol", mailstoreType);
properties.put("mail.imaps.host", host);
properties.put("mail.imaps.port", port);
properties.put("mail.smtp.ssl.enable", security);
Session session = Session.getInstance(properties);
store = session.getStore();
store.connect(username, originalPassword);
}
public static synchronized EmailListingController getInstance(String host, String mailstoreType, String username,
String originalPassword, int port, boolean auth, boolean security, User user) throws MessagingException {
System.out.println("instance");
if (instance == null) {
instance = new EmailListingController(host, mailstoreType, username, originalPassword, port, auth, security,
user);
}
return instance;
}
@GetMapping("/inbox/{userId}")
public ResponseEntity test(@PathVariable("userId") Integer id) {
Optional<User> user = userService.findById(id);
List<Email> emailList = new ArrayList<Email>();
if (user.isPresent()) {
String username = user.get().getUserName();
String pwd = user.get().getPassword();
EmailListingController client = EmailListingController.getInstance(host, mailstoreType, username,
pwd, port, auth, security, user.get());
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
String from;String subject;String message1;String cc;String to;String bcc;
String[] toArray = null;String[] ccArray = null;String[] bccArray = null;Email email2;
FetchProfile fp1 = new FetchProfile();
fp1.add(FetchProfile.Item.ENVELOPE);
fp1.add(IMAPFolder.FetchProfileItem.FLAGS);
fp1.add(IMAPFolder.FetchProfileItem.CONTENT_INFO);
fp1.add(UIDFolder.FetchProfileItem.UID);
folder.fetch(folder.getMessages(), fp1);
for (Message message : folder.getMessages()) {
System.out.println("message" + message.getSubject());
}
return new ResponseEntity(emailList, HttpStatus.OK);
}
}
}
任何帮助将不胜感激!