question

IqbalFathur-1568 avatar image
0 Votes"
IqbalFathur-1568 asked saldana-msft edited

Send email with attachment

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-sdkmicrosoft-graph-mail
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In your code, what is supposed to be the content of the file attachment.txt on Graph side?

0 Votes 0 ·

i suppose just a simple text, because that part of code is given by MS Graph documentation itself

0 Votes 0 ·

I get that. your code sample is to create a draft with a file called attachment.txt attached it it. If so, what is your expected contents of the file?

0 Votes 0 ·
Show more comments

1 Answer

Danstan-MSFT avatar image
0 Votes"
Danstan-MSFT answered BenWillson-3442 commented

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);

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks for the response, and sorry for my late response had some other things to do
1. adding the raw bytes of a file, same code as above but i change

 attachments.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");

to this

         String filePath = "C:\\such folder\\such file.txt";
         byte[] bytes = null;
         bytes = Files.readAllBytes(Paths.get(filePath));
    
             ......
         attachments.contentBytes = bytes;

which return error with this message

 "message": "The property \u0027contentBytes\u0027 does not exist on type \u0027Microsoft.OutlookServices.Attachment\u0027. Make sure to only use property names that are defined by the type or mark the type as open type."

  1. update SDK version. tried to upgrade to MS Graph 3.1, but all the syntax is very different and the documentation is not as complete as what the version i'm using.














0 Votes 0 ·

Make sure you notice he is using the class FileAttachment and not Attachment.

0 Votes 0 ·