我正在使用Outlook使用C#编写电子邮件
我使用的代码如下:
object outlookApp = null;
object mailitem = null;
object att = null;
Type typeOutlook = Type.GetTypeFromProgID("Outlook.Application");
Type typeMailItem = null;
Type typeAttachment = null;
outlookApp = Activator.CreateInstance(typeOutlook);
object[] parameters = new object[1] { 0 };
mailitem = typeOutlook.InvokeMember("CreateItem", System.Reflection.BindingFlags.InvokeMethod, null, outlookApp, parameters);
typeMailItem = mailitem.GetType();
att = typeMailItem.InvokeMember("Attachments", System.Reflection.BindingFlags.GetProperty, null, mailitem, null);
typeAttachment = att.GetType();
parameters = new object[1] { sSubject };
typeMailItem.InvokeMember("Subject", System.Reflection.BindingFlags.SetProperty, null, mailitem, parameters);
parameters = new object[1] { "[email protected]" };
typeMailItem.InvokeMember("To", System.Reflection.BindingFlags.SetProperty, null, mailitem, parameters);
parameters = new object[4] { @"c:\somefile.txt", 1, 1, "somefile.txt" };
try
{
typeAttachment.InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, att, parameters);
}
catch (Exception exAtt) { }
object inspector = typeMailItem.InvokeMember("GetInspector", System.Reflection.BindingFlags.InvokeMethod, null, mailitem, null);
Type typInspector = inspector.GetType();
//typInspector.InvokeMember("Activate", System.Reflection.BindingFlags.InvokeMethod, null, inspector, null);
parameters = new object[1] { true };
typInspector.InvokeMember("Display", System.Reflection.BindingFlags.InvokeMethod, null, inspector, parameters);
//parameters = new object[1] { true };
//typeMailItem.InvokeMember("Display", System.Reflection.BindingFlags.InvokeMethod, null, mailitem, parameters);
问题是,在某些计算机上,对话框未显示在应用程序的前面。有什么想法吗?我似乎听不懂。如上所述,我尝试使用邮件中的显示和检查器中的显示,但这不能解决问题。非常感谢您的帮助!
无模式显示项目(MailItem.Display(false)
,然后通过调用Inspector
/ MaiLItem.GetInspector
激活Inspector.Activate
对象。
由于您在评论中表示您已经尝试过Display(true),以为我会在一段时间前发布我们尝试过的内容。
这是我们用于Outlook的简单模块之一,我记得它运行良好
[已在带有O365 / Outlook2016的Win10和Win7上进行了测试
请确保在VS“ Outlook Object 16”中添加COM引用叫
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
public Class Outlook
{
public void SendMail()
{
Outlook.Application outlookObj = null;
Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.To = "[email protected]";
oMailItem.Subject = $"Primary user confirmed, survey sent";
oMailItem.Body = $"Please find below {userEmail.Text}";
oMailItem.Display(true);
oMailItem.Send();
Marshal.ReleaseComObject(oApp);
Marshal.ReleaseComObject(oMailItem);
}
}