question

VivekBuddhadev-2979 avatar image
0 Votes"
VivekBuddhadev-2979 asked AkashSharma-5523 commented

Graph API Send Mail with Attachments

I'm using Microsoft-Graph API version 1.4 and trying to send mail with attachment using following code..

 IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
    
 Message message = new Message();
 message.subject = "Meet for lunch?";
 ItemBody body = new ItemBody();
 body.contentType = BodyType.TEXT;
 body.content = "The new cafeteria is open.";
 message.body = body;
 LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
 Recipient toRecipients = new Recipient();
 EmailAddress emailAddress = new EmailAddress();
 emailAddress.address = "meganb@contoso.onmicrosoft.com";
 toRecipients.emailAddress = emailAddress;
 toRecipientsList.add(toRecipients);
 message.toRecipients = toRecipientsList;
 LinkedList<Attachment> attachmentsList = new LinkedList<Attachment>();
 FileAttachment attachments = new FileAttachment();
 attachments.name = "attachment.txt";
 attachments.contentType = "text/plain";
 attachments.contentBytes = "SGVsbG8gV29ybGQh";
 attachmentsList.add(attachments);
 message.attachments = attachmentsList;
    
 graphClient.me()
     .sendMail(message,null)
     .buildRequest()
     .post();

Ref.Link: user-sendmail

But, message.attachments requires AttachmentCollectionPage object not LinkedList<Attachment>();

Can anyone help me to send a mail with multiple attachment.

Thanks


microsoft-graph-mail
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.

1 Answer

mlafleur-msft avatar image
0 Votes"
mlafleur-msft answered AkashSharma-5523 commented

You need to case it to an IMessageAttachmentsCollectionPage (note that you also need to encode ContentBytes):

IMessageAttachmentsCollectionPage Attachments = (IMessageAttachmentsCollectionPage)new List<Attachment>()
{
  new FileAttachment
  {
    Name = "attachment.txt",
    ContentType = "text/plain",
    ContentBytes = Encoding.ASCII.GetBytes("SGVsbG8gV29ybGQh")
  }
}
· 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.

Thanks for the comment for encoding.

But, I didn't find IMessageAttachmentsCollectionPage in Graph1.4.0 version. is there any specific repository to import the same?

0 Votes 0 ·

Version 1.4.0 is exceptionally outdated ( it was released in June 2017). You will need to update your Nuget in order to use IMessageAttachmentsCollectionPage. The latest version is 3.16 and can be found at https://www.nuget.org/packages/Microsoft.Graph.

1 Vote 1 ·

@mlafleur: I'm creating the application using Java. So you can suggest for the same. Now actually I'm using a different approach https://stackoverflow.com/questions/64262929/email-with-multiple-attachments-using-graph-api-4-mb/64263205?noredirect=1#comment113665301_64263205

As I'm using a different approach you can close this ticket for now.

Thank you for your support

0 Votes 0 ·

This is giving an error of not being able to cast an object of type System.Collections.Generic.List to type Microsoft.Graph.IMessageAttachmentsCollectionPage


0 Votes 0 ·