title: Download attachment from outlook via javamail not working
body: <p>I am trying to write a program that will download an attachment from outlook automatically through java. </p>
  
  <p>I am using the javamail api. I was able to send the mail using the api but when it comes to downloading the attachment, or even reading mail, I am facing some issue. I have only a basic understanding of the mail protocols that I read up for the purpose of doing this. Other than that, I am not much familiar in that area.</p>
  
  <pre><code>public class MailReceiver {
  
      public static void main(String[] args) {
          getMail();
          sendMail();
      }
  
      private static void sendMail() {
          // TODO Auto-generated method stub
          // Sender's email ID needs to be mentioned
                  String from = "my.email@organization.com";
  
                  String host = "something.organization.com";
  
                  Properties properties = System.getProperties();
                  properties.setProperty("mail.smtp.host", host);
  
                  // Get the default Session object.
                  Session session = Session.getDefaultInstance(properties);
  
                  try {
                      // Create a default MimeMessage object.
                      MimeMessage message = new MimeMessage(session);
  
                      // Set From: header field of the header.
                      message.setFrom(new InternetAddress(from));
  
                      // Set To: header field of the header.
                      message.addRecipients(Message.RecipientType.TO,
                              InternetAddress.parse("some.email@organization.com"));
  
                      // Set Subject: header field
                      message.setSubject("Test Mail");
  
                      // Create the message part
                      BodyPart messageBodyPart = new MimeBodyPart();
  
                      StringBuilder email = new StringBuilder();
                      email.append("Hello World!");
                      // Fill message
                      messageBodyPart.setContent(email.toString(), "text/html");
  
                      // Create a multipar message
                      Multipart multipart = new MimeMultipart();
  
                      // Set text message part
                      multipart.addBodyPart(messageBodyPart);
  
                      // Send the complete message parts
                      message.setContent(multipart);
  
                      // Send message
                      Transport.send(message);
                      System.out.println("Mail sent successfully....");
                  } catch (MessagingException mex) {
                      mex.printStackTrace();
                  }
  
      }
  
      private static void getMail() {
          // Sender's email ID needs to be mentioned
          String user = "my.email@organization.com";
          String password = "my.password";
  
          String host = "something.organization.com";
  
          Properties properties = System.getProperties();  
             properties.setProperty("mail.smtp.host",host );  
             properties.put("mail.smtp.auth", "true");  
  
  
             Session session = Session.getDefaultInstance(properties,  
              new javax.mail.Authenticator() {  
              protected PasswordAuthentication getPasswordAuthentication() {  
               return new PasswordAuthentication(user,password);  
              }  
             });  
  
  
  
          try {
              Store store = session.getStore("pop3");  
               store.connect(host,user,password);  
  
               Folder folder = store.getFolder("inbox");  
               folder.open(Folder.READ_WRITE);  
  
               Message[] message = folder.getMessages(); 
  
              for (int a = 0; a &lt; message.length; a++) {
                  System.out.println("-------------" + (a + 1) + "-----------");
                  System.out.println(message[a].getSentDate());
  
                  Multipart multipart = (Multipart) message[a].getContent();
  
                  for (int i = 0; i &lt; multipart.getCount(); i++) {
                      BodyPart bodyPart = multipart.getBodyPart(i);
                      InputStream stream = bodyPart.getInputStream();
                      BufferedReader br = new BufferedReader(
                              new InputStreamReader(stream));
  
                      while (br.ready()) {
                          System.out.println(br.readLine());
                      }
                      System.out.println();
                  }
                  System.out.println();
              }
  
              folder.close(true);
              store.close();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>Since I wasn't sure what I should set inside <code>session.getStore()</code> (in <code>getMail()</code>), I tried all variations that I could do. </p>
  
  <p>For <code>pop3</code> I got the following error:</p>
  
  <pre><code>DEBUG POP3: server doesn't support TOP, disabling it
  javax.mail.AuthenticationFailedException: Logon failure: unknown user name or bad password.
      at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:174)
      at javax.mail.Service.connect(Service.java:313)
      at javax.mail.Service.connect(Service.java:172)
      at xxx.yyy.zzz.data.MailReceiver.getMail(MailReceiver.java:109)
      at xxx.yyy.zzz.data.MailReceiver.main(MailReceiver.java:28)
  </code></pre>
  
  <p>When I have <code>smtp</code>:</p>
  
  <pre><code>javax.mail.NoSuchProviderException: invalid provider
      at javax.mail.Session.getStore(Session.java:570)
      at javax.mail.Session.getStore(Session.java:536)
      at javax.mail.Session.getStore(Session.java:515)
      at xxx.yyy.zzz.data.MailReceiver.getMail(MailReceiver.java:108)
      at xxx.yyy.zzz.data.MailReceiver.main(MailReceiver.java:28)
  </code></pre>
  
  <p>and for <code>imap</code>, the following:</p>
  
  <pre><code>javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:613)
      at javax.mail.Service.connect(Service.java:313)
      at javax.mail.Service.connect(Service.java:172)
      at xxx.yyy.zzz.data.MailReceiver.getMail(MailReceiver.java:109)
      at xxx.yyy.zzz.data.MailReceiver.main(MailReceiver.java:28)
  </code></pre>
  
  <p>When I give <code>imaps</code> or <code>pop3s</code>, I am getting <code>unable to find valid certification path to requested target</code> error. </p>
  
  <p>I am not able to figure out what is the error since I am able to send a mail without any problem. I am also sure what I should use in <code>session.getStore()</code> either. The tutorial I was following had <code>pop3</code>.</p>