title: Can't send file via HttpPost multipart to sendgrid API
body: <p>I've been stuck on this issue for a while. So I use SendGrid for send email and everything is ok except attach image file, So I follow the documentation from <a href="https://sendgrid.com/docs/API_Reference/Web_API/mail.html" rel="nofollow">this</a></p>
  
  <p>and this is my code</p>
  
  <pre><code>    private class SendDataInBackGround extends AsyncTask&lt;Void, Void, String&gt;{
  
      @Override
      protected String doInBackground(Void... arg0) {
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(Constants.MAIL_URL);
  
          try {
              MultipartEntity multipartEntity = new MultipartEntity();
              multipartEntity.addPart("api_user", new StringBody(Constants.MAIL_USERNAME));
              multipartEntity.addPart("api_key", new StringBody(Constants.MAIL_PASSWORD));
              multipartEntity.addPart("to", new StringBody("me@mycompany.com"));
              multipartEntity.addPart("subject", new StringBody("subject"));
              multipartEntity.addPart("text", new StringBody("message"));
              multipartEntity.addPart("from", new StringBody("someone@somewhere.com"));
  
              File file = new File(Environment.getExternalStorageDirectory()
                      + "/safe.png");
              try {
                  file.createNewFile();
  
              } catch (IOException e1) {
                  e1.printStackTrace();
              }
              multipartEntity.addPart("files", new FileBody(file));
  
              httppost.setEntity(multipartEntity);
              String res = httpclient.execute(httppost, new UploadResponseHandler());
              return res;
          } catch (ClientProtocolException e) {
              return null;
          } catch (IOException e) {
              return null;
          }
      }
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
           Helper.createToast(getSherlockActivity(), result).show();
      }
  
      private class UploadResponseHandler implements ResponseHandler&lt;String&gt; {
  
          @Override
          public String handleResponse(HttpResponse response)
                  throws ClientProtocolException, IOException {
  
              HttpEntity r_entity = response.getEntity();
              String responseString = EntityUtils.toString(r_entity);
              return responseString;
          }
  
      }
  }
  </code></pre>
  
  <p>I'm not sure there's something I missed or have better solution for handle this.</p>
  
  <p>Thanks.</p>