0
 Store store = session.getStore("imaps");
  store.connect(host, "username",
     "password");//change the user and password accordingly
  Folder folder = store.getFolder("inbox");
  if (!folder.exists()) {
     System.out.println("inbox not found");
        System.exit(0);
  }
  folder.open(Folder.READ_ONLY);
  Date today=new Date();
  SearchTerm st = new ReceivedDateTerm(ComparisonTerm.EQ,today);
  Message[] messages = folder.search(st);

  for(int i=0;i<messages.length;i++)
  {

    String s1=messages[i].getSubject();
    if(s1!=null&&s1!="")
    {
    String s2="EXT: FSG daily shipment information";
    if(s1.equalsIgnoreCase(s2))
    {
  String contentType = messages[i].getContentType();
  String messageContent = "";

  // store attachment file name, separated by comma
  String attachFiles = "";

  if (contentType.contains("multipart")) {
      // content may contain attachments
      Multipart multiPart = (Multipart) messages[i].getContent();
      int numberOfParts = multiPart.getCount();
      for (int partCount = 0; partCount < numberOfParts; partCount++) {
          MimeBodyPart part = (MimeBodyPart)   multiPart.getBodyPart(partCount);
          if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
              // this part is attachment
              String fileName = part.getFileName();
              attachFiles += fileName + ", ";
              part.saveFile("C:/Users/rb842469/Documents/MailAttachments" + File.separator + fileName);

              HttpClient client = new HttpClient();
              PostMethod method = new PostMethod("https://dt-microservice-blobstore.run.aws-usw02-pr.ice.predix.io/uploadMultiBlob");
              method.addParameter("file",fileName);
              method.addParameter("directory","C:/Users/rb842469/Documents/MailAttachments");
              method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                                 new DefaultHttpMethodRetryHandler(3, false));
              client.getHostConfiguration().setProxy("ipaddress", port);

              int statusCode = client.executeMethod(method);
              byte[] responseBody = method.getResponseBody();
              //Print the response
             System.out.println(new String(responseBody));

          } 
      }
}

Hi, I am doing work on javax.mail package to access the webmail or outlook mails.

  • Here I'm able to download the attachments present in the mails which are filtered based on search condition(My search condition is having mails with today as received date and subject given in code).

  • But after downloading the attachments i tried to upload files to blobstore using HttpClient and PostMethod. In code i gave PostMethod(url),this url is my microservice url got from predix blobstore serviceinstance.

  • So after running the code i'm getting

    {"timestamp":1476867898345,"status":500,"error":"Internal Server Error","exception":"org.springframework.web.multipart.MultipartException","message":"The current request is not a multipart request","path":"/uploadMultiBlob"}

Can anyone give some suggestions to solve this issue?

1 Answers1

0

One place I might start is separating the problem. You are doing two operations here: 1) download a mail attachment and 2) upload to a blob store. Further each involves a multipart object which could easily lead to confusion. Try each individually while testing to isolate your issue. That will hopefully leave you with a clean and simple example of trying to upload a file to the blob store.

Once you have that, it appears you are trying to do a multipart upload but it doesn't look like you are providing the correct multipart parameters, hence getting the exception. You should review the docs to see if you missed something regarding how to make REST calls to the service: https://www.predix.io/docs#ThVWuJTA

For example, you seem to be sending a file and directory parameter in your POST call but those aren't listed in the docs as parameters for initiating a multipart upload. You may want to simply PUT an object instead. I also don't see an Authorization header which I would expect to be necessary.

Hope that helps get you started. You may also want to take a look at some of the blobstore-samples and just duplicate that to match your use case of email attachment downloads.

j12y
  • 2,112
  • 3
  • 17
  • 22
  • Thank u j12y,I understood how i'm getting the exception.Actually i have to send a multipartfile as a request parameter but in this code i'm sending a string. – Ramakrishna Reddy Oct 20 '16 at 06:38
  • If u know how to send a multipartfile as request parameter to postmethod then can u please answer me...... DiskFileItem fileItem = new DiskFileItem("file", "text/plain", false, file1.getName(), (int) file1.length() , file1.getParentFile()); fileItem.getOutputStream(); MultipartFile multipartFile = new CommonsMultipartFile(fileItem); This is my multipartfile object. – Ramakrishna Reddy Oct 20 '16 at 06:42