I am trying to connect to my mail box using the java mail api based on imap protocol. I check and for sure I inserting the correct parameters. This is the following exception that I got:
[ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)
I do not know why it is happens in addition I enabled the imap option in my gmail account settings.
Ihis is my code:
Properties protocol = new Properties();
protocol.setProperty("mail.store.protocol", "imaps");
try{
Session session = Session.getInstance(protocol, null);
Store store = session.getStore();
String host = prop.getProperty("host");
String email = prop.getProperty("username");
String password = prop.getProperty("password");
store.connect(host, email, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
model.addAttribute("msg","number of mails"+" "+messageCount);
Message[] messages = inbox.getMessages();
PrintWriter writer = new PrintWriter(username+".txt", "UTF-8");
for(int i=0;i<messageCount || prop.getProperty("status").equals(status.RUNNING.toString()) ;i++ ){
model.addAttribute("msg","Reading Mails");
Multipart mp = (Multipart) messages[i].getContent();
BodyPart bp = mp.getBodyPart(0);
writer.println("From:"+ messages[i].getFrom()+" "+"Subject:"+messages[i].getSubject()+" "+"Message:"+bp.getContent());
prop = loadProperties(username+".properties");
}
writer.close();
inbox.close(true);
store.close();
}catch (Exception e){
model.addAttribute("msg","Exception:"+e.getMessage());
}
I would recommend you doing the following:
after searching I found that I need to change the host name to
imap.googlemail.com
the following code was changed:
changed to: