使用图形 API 发送带有多个附件的邮件

huangapple go评论69阅读模式
英文:

Send mail with multiple attachment using Graph API

问题

我正在使用 Microsoft Graph API 版本 1.4,并尝试使用以下代码发送带有附件的邮件:

IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

Message message = new Message();
message.subject = "午餐见面?";
ItemBody body = new ItemBody();
body.contentType = BodyType.TEXT;
body.content = "新的自助餐厅已经开放。";
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();

但是,message.attachments 需要一个 AttachmentCollectionPage 对象,而不是 LinkedList<Attachment>

有谁可以帮助我发送带有多个附件的邮件。

谢谢。

英文:

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 = &quot;Meet for lunch?&quot;;
ItemBody body = new ItemBody();
body.contentType = BodyType.TEXT;
body.content = &quot;The new cafeteria is open.&quot;;
message.body = body;
LinkedList&lt;Recipient&gt; toRecipientsList = new LinkedList&lt;Recipient&gt;();
Recipient toRecipients = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = &quot;meganb@contoso.onmicrosoft.com&quot;;
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;
LinkedList&lt;Attachment&gt; attachmentsList = new LinkedList&lt;Attachment&gt;();
FileAttachment attachments = new FileAttachment();
attachments.name = &quot;attachment.txt&quot;;
attachments.contentType = &quot;text/plain&quot;;
attachments.contentBytes = &quot;SGVsbG8gV29ybGQh&quot;;
attachmentsList.add(attachments);
message.attachments = attachmentsList;

graphClient.me()
	.sendMail(message,null)
	.buildRequest()
	.post();

Ref.Link: Graph-Send-Mail

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

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

Thanks

答案1

得分: 1

我找到了使用1.4.0版本发送单封邮件中的多个附件的解决方案。请查看以下代码...

val address = EmailAddress()
address.address = "vivek@domain.com"
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"
att.name = "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"
att2.name = "hi2.txt"
att2.oDataType = "#microsoft.graph.fileAttachment"

message.addAttachment(att)
message.addAttachment(att2)

graphClient.me()
.sendMail(message,false)
.buildRequest()
.post();

上述代码可用于发送大小小于4 MB 的多个附件。要发送超过此限制的附件,请参阅 此链接

英文:

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 = &quot;vivek@domain.com&quot;
val recipient = Recipient()
recipient.emailAddress = address

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

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

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

message.addAttachment(att)
message.addAttachment(att2)

graphClient.me()
.sendMail(message,false)
.buildRequest()
.post();

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

huangapple
  • 本文由 发表于 2020年10月6日 23:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64228880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定