Please log in via your web browser: https://suppor

2020-03-30 05:01发布

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());
}

标签: java javamail
2条回答
够拽才男人
2楼-- · 2020-03-30 05:42

I would recommend you doing the following:

  1. Just enable 2-step verification on your account.
  2. Then generate one App-Specific-Password and us that to connect instead of your original password.
查看更多
3楼-- · 2020-03-30 05:43

after searching I found that I need to change the host name to imap.googlemail.com

the following code was changed:

mail.store.protocol

changed to:

imap.googlemail.com
查看更多
登录 后发表回答