VB.Net调用Microsoft Graph SendMail返回ErrorInternalServerError如何解决?
错误触发核心原因
你在构造邮件收件人列表时错误使用了仅适用于日历事件的Attendee类型填充ToRecipients字段,邮件的ToRecipients集合要求接收的是Recipient类型实例,Attendee额外携带的Type属性在邮件发送接口的校验逻辑中无法被识别,最终触发服务端内部错误。
修复方案
1. 修正收件人构造逻辑
修改DefineImportantEmail方法,将Attendee替换为标准Recipient类型,删除不相关的Type属性赋值:
Function DefineImportantEmail(Locations As String, Recipients As List(Of (String, String))) As Message Dim title = "IMPORTANT OUTAGE NOTIFICATION" Dim message = "Outage at " & Locations Dim RecipientList = New List(Of Recipient) For Each recipient In Recipients RecipientList.Add(New Recipient With { .EmailAddress = New EmailAddress With { .Address = recipient.Item1, .Name = recipient.Item2 } }) Next Return New Message With { .Subject = title, .Body = New ItemBody With { .ContentType = BodyType.Text, .Content = message }, .ToRecipients = RecipientList } End Function
2. 可选优化&兜底排查
如果修改类型后仍存在异常,可按以下顺序排查:
- 确认Azure中配置的
Mail.Send权限已完成管理员同意,权限类型和你的认证逻辑匹配:客户端凭证流使用应用权限,用户登录流使用委托权限 - 确认发件人邮箱账号未被组织限制发送权限,可通过OWA手动登录该账号发送测试邮件验证
- 避免使用
Wait()同步调用异步方法,改为Await调用防止线程死锁引发额外异常,优化后的发送代码参考:
Async Function SendImportantEmail(Locations As String, Recipients As List(Of (String, String))) As Task Dim SaveToSentItems = True Dim graphClient = GetGraphClient() Dim EventObject = DefineImportantEmail(Locations, Recipients) Await graphClient.Users("xxxxx@xxxxxxxxxxxxxxx.com").SendMail(EventObject, SaveToSentItems).Request().PostAsync() End Function
内容的提问来源于stack exchange,提问作者Close to the Wheels
相关产品推荐
相关产品推荐

