qq_26624753
qq_26624753
采纳率81.8%
2019-05-30 09:39

C#操作OutLook发邮件失败

20

调用olEMail.Send()报错: 0x80004004,网上查了一下,没解决问题。。。

还有就是email是富文本RichText格式的,添加正文附件的位置怎么调整?

求大神指导,代码块如下:

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.NameSpace nSpace = app.GetNamespace("MAPI");
            Microsoft.Office.Interop.Outlook.MailItem olEMail = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            Microsoft.Office.Interop.Outlook.Attachments olAttach = olEMail.Attachments; 
            olEMail.To = "Chun.Xiao.Liu@gds.ey.com";
            olEMail.Subject = "TestMail";
            olEMail.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatRichText;
            string stext = string.Empty;
            stext += "Adding A1 one more line"+ System.Environment.NewLine;
            stext += "Adding A2 one more line"+ System.Environment.NewLine;
            stext += "Adding A3 one more line"+ System.Environment.NewLine;
            stext += "Adding A4 one more line";
            olEMail.Body = stext;

            olAttach.Add("C:\\Text\\4.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olOLE, stext.IndexOf("A4"));
            olAttach.Add("C:\\Text\\3.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olOLE, stext.IndexOf("A3"));
            olAttach.Add("C:\\Text\\2.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olOLE, stext.IndexOf("A2"));
            olAttach.Add("C:\\Text\\1.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olOLE, stext.IndexOf("A1"));

            //olEMail.Display();
            olEMail.Send();
            olEMail = null;
            app = null;

就是附件的位置,不好定位

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 2年前

    outlook有没有安装好,重新引用下outlook的库,还有,你的系统和你的office还有你的程序的架构要一致,比如都是32bit或者都是64bit

    点赞 评论 复制链接分享