Skip to content

Send mail with multiple attachment using Graph API

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 = "";
toRecipients.emailAddress = emailAddress;
message.toRecipients = toRecipientsList;
LinkedList<Attachment> attachmentsList = new LinkedList<Attachment>();
FileAttachment attachments = new FileAttachment(); = "attachment.txt";
attachments.contentType = "text/plain";
attachments.contentBytes = "SGVsbG8gV29ybGQh";
message.attachments = attachmentsList;

Ref.Link: Graph-Send-Mail

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

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




I found the solution to send multiple attachment in a single mail using 1.4.0 version. Checkout the following code…

val address = EmailAddress()
address.address = ""
val recipient = Recipient()
recipient.emailAddress = address

val message = MyMessage()
message.subject = "Test E-Mail"
message.body = getItemBody()
message.toRecipients = Collections.singletonList(recipient)

val att = FileAttachment()
att.contentBytes = File("/home/user/file.pdf").readBytes()
att.contentType = "text/pdf" = "file.pdf"
att.oDataType = "#microsoft.graph.fileAttachment"

val att2 = FileAttachment()
att2.contentBytes = "hello there! from second file".toByteArray(StandardCharsets.UTF_8)
att2.contentType = "text/plain" = "hi2.txt"
att2.oDataType = "#microsoft.graph.fileAttachment"


The above code can be used to send multiple attachments with size less than 4 MB. To send above the limit please refer this.

9 People found this is helpful