Question: I am trying to attach a png file. Currently when I sent the email, the attachment is 2x bigger than the file should be and an invalid png file. Here is the code I currently have:

```
    import com.sendgrid.*;

    Attachments attachments = new Attachments();
    String filePath = "/Users/david/Desktop/screenshot5.png";
    String data = "";
    try {
        data = new String(Files.readAllBytes(Paths.get(filePath)));
    } catch (IOException e) {
    }

    byte[] encoded = Base64.encodeBase64(data.getBytes());
    String encodedString = new String(encoded);
    attachments.setContent(encodedString);

```

Perhaps I am encoding the data incorrectly? What would be the correct way to 'get' the data to attach it?

Answer: 
With respect, this is why Python presents a problem to modern developers. It abstracts away important concepts that you can't fully understand in interpreted languages.

First, and this is a relatively basic concept, but you [can't convert arbitrary byte sequences to a string and hope it works out](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/). The following line is your first problem:

```
data = new String(Files.readAllBytes(Paths.get(filePath)));

```

EDIT: It looks like the library you are using **expects** the file to be base64 encoded. I have no idea why. Try changing your code to this:

```
Attachments attachments = new Attachments();
String filePath = "/Users/david/Desktop/screenshot5.png";
try {
    byte[] encoded = Base64.encodeBase64(Files.readAllBytes(Paths.get(filePath)));
    String encodedString = new String(encoded);
    attachments.setContent(encodedString);
} catch (IOException e) {
}

```

The only issue you were having is that you were trying to represent arbitrary bytes as a string.
Take a look at the Builder class in the repository [here](https://github.com/sendgrid/sendgrid-java/blob/4d58fcb8b34daa0914ecfcbe23a954277ac85f91/src/main/java/com/sendgrid/helpers/mail/objects/Attachments.java#L139). Example:

```
FileInputStream fileContent = new FileInputStream(filePath);
Attachments.Builder builder = new Attachments.Builder(fileName, fileContent);
mail.addAttachments(builder.build());

```