简体   繁体   English

使用F#的Outlook互操作MailItem

[英]Outlook interop MailItem with F#

I am converting C# to F# for outlook email messages. 我正在将C#转换为F#以获取Outlook电子邮件消息。 This function will be called inside a try...with block if an exception occurs to email me. 如果发送电子邮件给我发送异常,将在try ... with block中调用此函数。 I normally use an SMTP mail service that works in the production environment only. 我通常使用仅在生产环境中工作的SMTP邮件服务。 I have to use Outlook in Dev. 我必须在Dev中使用Outlook。

open System.Net.Mail
open System.Runtime.InteropServices
open System.Reflection
open System.Collections
open System.Threading
open Microsoft.Office.Interop
open Microsoft.Office.Interop.Outlook

let sendEmail2 (sMessage:string) =
    let subject = "Load Failed at " + DateTime.Now.ToString() + "."
    let body = sMessage
    let app = new Outlook.ApplicationClass
    let mailItem = new app.CreateItem(OlItemType.olMailItem)
    //let mailItem = app.CreateItem(OlItemType.olMailItem)
    mailItem.Subject = subject
    mailItem.Body = body
    mailItem.To = "user@mycompany.com"
    mailItem.Send()

The issue is creating the MailItem. 问题是创建MailItem。 The original C# code did this 最初的C#代码就是这样做的

Outlook.Application app = new Outlook.Application();
Outlook.MailItem mailItem = app.CreateItem(Outlook.OlItemType.olMailItem);

I tried to use the C# code to the right of the equals sign, fully referencing the the interop assembly, etc. I even looked at an example from MSDN Word and Excel to reference the ApplicationClass. 我尝试使用等号右侧的C#代码,完全引用互操作程序集等。我甚至查看了MSDN Word和Excel中的一个示例来引用ApplicationClass。 I even thought to use MailItemClass but, it is protected. 我甚至认为使用MailItemClass,但它受到保护。

It always errors at the mailItem line with an error: 它总是在mailItem行出错,并带有错误:

Incomplete structured construct at or before this point in expression 表达式中此点或之前的不完整结构化构造

The error message indicates that there is a syntax error somewhere near the point where the error was reported. 该错误消息表明在报告错误的位置附近某处存在语法错误。 So, looking just at that, the first problem is that you are missing arguments when constructing an instance of Outlook.ApplicationClass . 因此,仅仅看一下,第一个问题是在构造Outlook.ApplicationClass实例时缺少参数。 It should be: 它应该是:

let app = new Outlook.ApplicationClass()

As already mentioned in the comments, there are other issues - assignment should be written using <- and app.CreateItem is a method call, so you do not need new there: 正如评论中已经提到的,还有其他问题 - 应该使用<-编写赋值<-app.CreateItem是一个方法调用,所以你不需要new

let mailItem = app.CreateItem(OlItemType.olMailItem)
mailItem.Subject <- subject
mailItem.Body <- body
mailItem.To <- "user@mycompany.com"

As a side-note, this is probably not an area where you'd get a lot of benefits from using F#, so if this is for work, it might be better to leave the code in C# and use F# for some more functional problems than for Office interop. 作为旁注,这可能不是一个你可以从使用F#中获得很多好处的领域,所以如果这是为了工作,最好将代码保留在C#中并使用F#来解决更多功能问题比办公室互操作。 But you can certainly do this in F# too. 但你也可以在F#中做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM