I want to connect to our test email server and want to fetch the mail contents for any specific user.
In the code mentioned below I print the value of 'store' object and found that it printed the machine user name that i have logged in with. However the email for which i want to fetch the mail content is different from machine user.
import javax.mail.*;
import java.util.Properties;
public class VerifyEmails {
public void check(String host, String user, String password) {
try {
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.user", user);
properties.put("mail.pop3.port", "143");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore("pop3s");
System.out.println("test1 " + store);
store.connect(host, user, password);
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
System.out.println("messages.length---" + messages.length);
for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
System.out.println("---------------------------------");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContent().toString());
}
emailFolder.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
VerifyEmails ch = new VerifyEmails();
ch.check("testServer", "test1@anydomain.com", "hello");
}
}
This question has been asked many times and most of the answers shows that we should set, mail.pop3.starttls.enable"= "true" .
I have tried with same but did not succeed.
I am getting below exception when running the code.
javax.mail.MessagingException: Connect failed; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209) at javax.mail.Service.connect(Service.java:345) at javax.mail.Service.connect(Service.java:226)