Power Automate:如何从请求传递附件并发送带附件邮件
为Power Automate邮件流添加附件:数据类型选择与C#调用方法
一、Power Automate端的附件数据类型配置
触发动作的请求体Schema定义
若你使用「当收到HTTP请求时」作为流触发,需更新请求体的JSON Schema,新增attachments字段,类型为数组,数组内每个对象需包含:name:字符串类型,存储带后缀的文件名(如report.pdf)contentBytes:字符串类型,存储文件的Base64编码内容
示例Schema片段:
{ "type": "object", "properties": { "firstName": { "type": "string" }, "lastName": { "type": "string" }, "subject": { "type": "string" }, "email": { "type": "string" }, "attachments": { "type": "array", "items": { "type": "object", "properties": { "name": { "type": "string" }, "contentBytes": { "type": "string" } }, "required": ["name", "contentBytes"] } } }, "required": ["firstName", "lastName", "subject", "email"] }发送邮件动作的附件绑定
在「发送电子邮件(V2)」动作的「附件」区域,直接绑定触发动作返回的attachments数组即可。Power Automate会自动解析数组中的name和contentBytes字段,还原为邮件附件。
二、C#端调用流的文件传递方法
无需直接传递byte数组,需将文件转换为Base64编码字符串后嵌入请求JSON,具体步骤如下:
读取文件并转换为Base64字符串
// 读取本地文件到byte数组 byte[] fileBytes = File.ReadAllBytes(@"C:\path\to\your\file.pdf"); // 转换为Base64字符串 string base64Content = Convert.ToBase64String(fileBytes);构造包含附件的请求JSON
可通过实体类或匿名对象构造请求体:public class EmailAttachment { public string Name { get; set; } public string ContentBytes { get; set; } } public class EmailRequest { public string FirstName { get; set; } public string LastName { get; set; } public string Subject { get; set; } public string Email { get; set; } public List<EmailAttachment> Attachments { get; set; } } // 构造请求对象 var request = new EmailRequest { FirstName = "John", LastName = "Doe", Subject = "测试邮件", Email = "recipient@example.com", Attachments = new List<EmailAttachment> { new EmailAttachment { Name = "report.pdf", ContentBytes = base64Content } } };发送HTTP请求到Power Automate流
使用HttpClient发送POST请求:using (var httpClient = new HttpClient()) { string jsonContent = JsonSerializer.Serialize(request); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); // 替换为你的Power Automate流触发URL var response = await httpClient.PostAsync("https://prod-xx.westus.logic.azure.com:443/workflows/xxx/triggers/manual/paths/invoke", content); response.EnsureSuccessStatusCode(); }
关键注意事项
- Base64编码会使文件体积增加约30%,大文件建议先上传至存储服务再传递文件URL;常规大小附件用Base64是最直接的方案。
- 确保Power Automate流的触发URL权限配置正确,避免调用失败。
内容的提问来源于stack exchange,提问作者samantha07
相关产品推荐
相关产品推荐

