Send email with attachment

Iqbal Fathur 1 Reputation point
2021-04-02T07:28:20.903+00:00

so i'm trying to send email with attachment in it, but it always return failed. code sample here:

        LinkedList<Attachment> attachmentsList = new LinkedList<Attachment>();
        FileAttachment attachments = new FileAttachment();
        attachments.name = "attachment.txt";
        attachments.contentType = "text/plain";
        attachments.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");
        attachmentsList.add(attachments);
        AttachmentCollectionResponse attachmentCollectionResponse = new AttachmentCollectionResponse();
        attachmentCollectionResponse.value = attachmentsList;
        AttachmentCollectionPage attachmentCollectionPage = new AttachmentCollectionPage(attachmentCollectionResponse, null);
        message.attachments = attachmentCollectionPage;

error message:

{
  "error": {
    "code": "RequestBodyRead",
    "message": "The annotation \u0027odata.context\u0027 was found. This annotation is either not recognized or not expected at the current position.",
    "innerError": {
      "date": "2021-04-02T07:15:02",
      "request-id": "xxxxxxxxxxxxxxxxxxxxxx",
      "client-request-id": "xxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
  }
}

i've also tried to set the email to draft first, and then add the attachment, but still failed. code sample:

                FileAttachment attachment1 = new FileAttachment();
                attachment1.name = aattta.name;
                attachment1.contentType = aattta.contentType;
                attachment1.isInline = false;
                attachment1.contentBytes = Base64.getDecoder().decode(byteArrray1);

                graphClient
                    .me()
                    .messages(idEmail)
                    .attachments()
                    .buildRequest()
                    .post(attachment1);

error message:

{
  "error": {
    "code": "UnprocessableType",
    "message": "Cannot process input of abstract type \u0027Microsoft.OutlookServices.Attachment\u0027",
    "innerError": {
      "date": "2021-04-02T07:22:52",
      "request-id": "xxxxxxxxxxxxxxxxxxxxx",
      "client-request-id": "xxxxxxxxxxxxxxxxxxxxxx"
    }
  }
}

am i missing something here? even the example code given is returning error. every help is highly appreciated. thanks!
note: i'm using java, ms-graph v2.5.0, and msal4j v1.8.1

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,692 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Danstan Onyango 3,741 Reputation points Microsoft Employee
    2021-04-07T06:57:18.3+00:00

    I think your error is related to the content of the contentBytes. Try adding the raw bytes of the content or file. If that doesn't work,
    I suggest you upgrade your version of SDK to and try the code below.

    byte[] buffer = System.Text.Encoding.Unicode.GetBytes("hello text");
    FileAttachment attachment1 = new FileAttachment();
    attachment1.Name = "attachment.txt";
    attachment1.ContentType = "text/plain";
    attachment1.IsInline = false;
    attachment1.ContentBytes = System.Text.Encoding.Unicode.GetBytes(System.Convert.ToBase64String(buffer));
    
    await graphServiceClient
        .Me
        .Messages["message-id"]
        .Attachments
        .Request()
        .AddAsync(attachment1);