最近Microsoft announced可以发送附件大于4MB的电子邮件。 根据文档,我们必须先创建草稿,然后进行上传会话,上传附件并最终发送邮件。
我可以使用以下代码创建草稿:
var confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithTenantId(tenant)
.Build();
var authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
var graphClient = new GraphServiceClient(authenticationProvider);
var email = new Message
{
Body = new ItemBody
{
Content = i + " Works fine! " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
ContentType = BodyType.Html,
},
Subject = "Test" + (j == 0 ? "" : " " + j),
ToRecipients = recipientList,
Attachments = att
};
Message draft = await graphClient
.Users["test@test.onmicrosoft.com"]
.Messages
.Request()
.AddAsync(mail);
但是当我尝试从文档中摘录时:
var attachmentItem = new AttachmentItem
{
AttachmentType = AttachmentType.File,
Name = "flower",
Size = 3483322
};
await graphClient.Me.Messages["AAMkADI5MAAIT3drCAAA="].Attachments
.CreateUploadSession(attachmentItem)
.Request()
.PostAsync();
我得到那些错误:
我已经添加了对图库的稳定版本和beta版本(Microsoft.Graph,Microsoft.Graph.Beta)的引用(我以前使用过beta端点),但找不到AttachmentItem
。
我已经为AttachmentItem搜索了两个存储库(https://github.com/microsoftgraph/msgraph-sdk-dotnet,https://github.com/microsoftgraph/msgraph-beta-sdk-dotnet),但是没有找到任何内容。
发送带有大附件的邮件是一项新功能(文档发布于2019年10月25日),但根据文档显示,应支持此功能。
文档有误吗?如何创建上传会话和上传附件?我必须手动创建请求吗?还是可以使用Microsoft.Graph库?
我只看到CreateUploadSession
的云端硬盘-https://github.com/microsoftgraph/msgraph-sdk-dotnet/search?q=CreateUploadSession&unscoped_q=CreateUploadSession
答案 0 :(得分:1)
我不得不四处寻找可行的解决方案。请注意,此示例使用 Microsoft.Graph.Auth nuget,在撰写本文时仍处于预览状态,因此可能会进一步更改。
var tenant = "xxxxxxxxxxxxxxxxxx";
var client = "yyyyyyyyyyyyyyyyyy";
var secret = "xxxxxxxxxxxxxxxxxx";
var senderEmail = "user_email@yourdomainato365.com";
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(client)
.WithTenantId(tenant)
.WithClientSecret(secret)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider );
var fileName = "some_huge_file.pdf";
var path = System.IO.Path.Combine(@"D:\Path_To_Your_Files", fileName );
var message = new Message
{
Subject = "Check out this Attachment?",
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = "Its <b>awesome</b>!"
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "john_doe@gmail.com"
}
}
},
};
var attachmentContentSize = new System.IO.FileInfo(path).Length;
//check to make sure content is below 3mb limit
var isOver3mb = attachmentContentSize > (1024*1024*3);
//if below our limit, send directly.
if( isOver3mb == false )
{
message.Attachments = new MessageAttachmentsCollectionPage()
{
new FileAttachment{ Name = fileName, ContentBytes = System.IO.File.ReadAllBytes(path) }
};
await graphClient.Users[senderEmail].SendMail(message, true).Request().PostAsync();
}
else
{
//first create an email draft
var msgResult = await graphClient.Users[senderEmail].Messages
.Request()
.AddAsync(message);
var attachmentItem = new AttachmentItem
{
AttachmentType = AttachmentType.File,
Name = fileName,
Size = attachmentContentSize,
};
//initiate the upload session for large files (note that this example doesn't handle multiple attachment).
var uploadSession = await graphClient.Users[senderEmail].Messages[msgResult.Id].Attachments
.CreateUploadSession(attachmentItem)
.Request()
.PostAsync();
var maxChunkSize = 1024 * 320;
var allBytes = System.IO.File.ReadAllBytes(path);
using( var stream = new MemoryStream ( allBytes ) )
{
stream.Position = 0;
LargeFileUploadTask<FileAttachment> largeFileUploadTask = new LargeFileUploadTask<FileAttachment>(uploadSession, stream, maxChunkSize);
await largeFileUploadTask.UploadAsync();
}
//once uploaded, sends out the email from the Draft folder
await graphClient.Users[senderEmail].Messages[msgResult.Id].Send().Request().PostAsync();
}
答案 1 :(得分:0)
此功能在Preview(aka Beta)中。由于这些API和资源仅存在于Graph Beta中,因此您不能使用AWS Systems Manager库的GA版本。您需要使用该库的Microsoft.Graph版本。