【发布时间】:2017-10-28 19:27:34
【问题描述】:
我有一份文件和电子邮件地址列表。
我正在尝试创建一种方法,我可以使用它来迭代抛出 emailAddresses 列表,并为每个创建一个新电子邮件,我 Outlook 并附加相应的文档。
创建 MailItem 是我卡住了。 在 MSDN 上找到了一个示例,该示例应该适用于 Office 2013 及更高版本。
这是我目前所拥有的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Outlook;
namespace Test_Invoice
{
class SendviaMail
{
string demofile = @"C:\Desktop\test-inv\1_Test Testesen_1_010.pdf";
public void Send()
{
MailItem eMail = new MailItem();
eMail.Subject = "Here is Your Invoice";
eMail.To = "test@testesen.dk";
eMail.Body = "Dette er en test mail for TestMailApp";
}
public void Send(string email, string filename)
{
}
}
}
我一直在尝试理解 MSDN 上的文档 并阅读这里的一些帖子。
据我所知,下一步是添加附件(演示文件) 如果我理解正确,我需要类似的东西
eMail.AttachmentAdd = demofile;
但这不起作用。
可能是我没有正确理解库。 查看来自 MSDN https://msdn.microsoft.com/en-us/library/bb644320.aspx 的这个示例
这段代码的结果:
using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace Test_Invoice
{
class SendviaMail
{
string demofile = @"C:\Desktop\test-inv\1_Test Testesen_1_010.pdf";
public void Send()
{
Outlook.MailItem mail = Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Subject = "Quarterly Sales Report FY06 Q4";
//MailItem eMail = new MailItem();
//eMail.Subject = "tilmelding og faktura";
//eMail.To = "test@testesen.dk";
//eMail.Body = "Dette er en test mail for TestMailApp";
//eMail.AttachmentAdd
}
public void Send(string email, string filename)
{
}
}
}
【问题讨论】:
-
MSDN 似乎提供了一个不错的例子。
-
我实际上已经看过了。可能只是我不明白。但 VS 无法识别 Application.CreateItem
-
这是我得到的错误:错误 CS0120 非静态字段、方法或属性“_Application.CreateItem(OlItemType)”需要对象引用
-
你能用
Application.CreateItem更新你的代码吗? -
你是对的 - 链接的例子是错误的 - 你需要构造一个 Outlook.Application 实例来让它工作。准备好您正在运行的 Outlook 实例将询问您的应用是否可以使用打开的实例。