I'm fairly new to eclipse and new to working with e-mails in java. My code works perfectly when ran out of eclipse but as an executable JAR it won't work. It all boils down to the Folder.getMessages function. My program logs into the gmail account just fine and even finds the mail folder, but once it tries to get the messages it just crashes. I can't figure out why it would only work in IDE but it's very frustrating.
Here is the function:
public static String[] checkMail(String host, String storeType, String user, String pass, String headerReq) throws IOException
{
try
{
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore("pop3s");
store.connect(host, user, pass);
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
ArrayList<String> messageContent = new ArrayList<String>();
for (int i = 0; i < messages.length; i++)
{
if (messages[i].getSubject() != null)
{
System.out.println(messages[i].getSubject());
if (messages[i].getSubject().toLowerCase().contains(headerReq.toLowerCase()))
{
messageContent.add(getEmailText(messages[i]));
writeToFile(messages[i].getSubject().substring(7) + "\n","senders", true);
}
}
}
String[] contentArray = new String[messageContent.size()];
for (int i = 0; i < contentArray.length; i++)
{
contentArray[i] = messageContent.get(i);
}
emailFolder.close(false);
store.close();
return contentArray;
}
catch (NoSuchProviderException e)
{
writeToFile("No Provider", "MailErrorLog", true);
e.printStackTrace();
}
catch (MessagingException e)
{
writeToFile("MessagingException", "MailErrorLog", true);
e.printStackTrace();
}
catch (Exception e)
{
writeToFile("Exception?", "MailErrorLog", true);
e.printStackTrace();
}
return null;
}
I really appreaciate your time!