发送多线程SendEmail请求返回一般Mapi失败
我正在使用 Dave Brooks 的 MAPI 代码。
我正在尝试以编程方式发送生成的水晶报告。
当我在没有线程的情况下运行代码时,一切都运行良好。 问题是当我使用线程时,我收到返回错误“常规 MAPI 失败 [2]”。
我以前从未使用过线程,并且知道其中存在危险。 任何人都可以提供对这个问题的任何见解吗? 注意:我删除了异常处理以使代码更清晰。
Private Sub RunReport()
SetParameters()
SaveReportFile()
Dim operation As New ThreadStart(AddressOf SendEmail)
Dim theThread As New Thread(operation)
theThread.Start()
End Sub
Public Sub SendEmail()
Dim m As MAPI
m = New MAPI()
Dim email As String
For Each email In emailAddress
m.AddRecipientBCC(email)
Next email
m.AddAttachment(@"c:\temp\report.pdf")
m.SendMailPopup("Requested Report", "")
End Sub
I'm using the MAPI code by Dave Brooks.
I am attempting to programatically send out a Crystal Report that is generated.
When I run through the code without threading everything runs fine. The problem is when I use threading I get the return error "General MAPI failure [2]".
I have never used threading before and understand that there are dangers involved. Can anyone provide any insight into this issue? NOTE: I've removed exception handling to make the code clearer.
Private Sub RunReport()
SetParameters()
SaveReportFile()
Dim operation As New ThreadStart(AddressOf SendEmail)
Dim theThread As New Thread(operation)
theThread.Start()
End Sub
Public Sub SendEmail()
Dim m As MAPI
m = New MAPI()
Dim email As String
For Each email In emailAddress
m.AddRecipientBCC(email)
Next email
m.AddAttachment(@"c:\temp\report.pdf")
m.SendMailPopup("Requested Report", "")
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个很晚的答案,但我想我还是会添加它,因为我刚刚遇到这个问题并且在其他地方找不到答案。
在开始使用之前,您需要将线程的公寓状态设置为 STA:
请注意,线程池中的线程(例如由BackgroundWorker 组件使用)是MTA。
A very late answer, but thought I'd add it anyway as I just encountered this and couldn't find an answer elsewhere.
You need to set your thread's appartment state to STA before it is started using:
Note that threads from the threadpool (e.g. used by BackgroundWorker component) are MTA.
我遇到了同样的错误(一般 MAPI 失败 [2]),并在调试早期遇到了这个解决方案; 但是,错误的原因是由于以管理员身份运行我的应用程序,而 Outlook 以我的用户身份运行。 我很难找到错误的原因,所以希望这能帮助与我进行相同搜索的人。
如果上述答案不能解决您的问题,请尝试在没有提升权限的情况下运行您的应用程序(如果可能),或者找到一种使用用户模拟来调用 MAPI 的方法。
I encountered this same error (General MAPI failure [2]) and came across this solution early in my debugging; however, the cause for my error was due to running my application as administrator while outlook was running as my user. I had a hard time finding the cause of my error so hopefully this will help someone on the same search as me.
If the above answer doesn't solve your problem, try running your application without elevated privileges if possible or find a way to call MAPI using user impersonation.