【问题标题】:Outlook not showing in top of caller applicationOutlook 未显示在呼叫者应用程序的顶部
【发布时间】:2020-04-11 09:02:18
【问题描述】:

我正在使用 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] { "someone@stackoverflow.com" };
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(true);
  • 我指的是撰写电子邮件对话框。我已经尝试过 Display(true) - 它在示例中,在注释行中,但无论如何谢谢!
  • 好吧,我去年实现了一个类似的模块。在我的答案中发布下面的代码 sn-p 应该可以帮助您
  • 您好 MSantos,您能否确认下面的代码 sn-p 或任何其他答案是否帮助您解决了您的问题,如果是,请考虑点击绿色复选框接受它

标签: c# outlook late-binding


【解决方案1】:

无模式显示项目 (MailItem.Display(false)),然后通过调用 MaiLItem.GetInspector / Inspector.Activate 激活 Inspector 对象。

【讨论】:

    【解决方案2】:

    由于您在您的 cmets 中声明您已经尝试过 Display(true) ,所以我想我会发布我们前段时间尝试过的内容。

    这是我们用于 Outlook 的简单模块之一,我记得它运行良好

    在 Win10 和 Win7 上使用 O365 / Outlook2016 进行了测试

    请务必在 VS“Outlook 对象 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 = "xyz@gmail.com";
                        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);
    
                    }
    
               }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多