如下图所示是Application
的问题。我试图添加
Outlook.Application Application = new Outlook.Application();
但没有任何改变
我将名称空间添加为using Outlook = Microsoft.Office.Interop.Outlook;
#region Send Email
private void SendEmail()
{
string subjectEmail = "Meeting has been rescheduled.";
string bodyEmail = "Meeting is one hour later.";
Outlook.MAPIFolder sentContacts = (Outlook.MAPIFolder)
this.Application.ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);
foreach (Outlook.ContactItem contact in sentContacts.Items)
{
if (contact.Email1Address.Contains("example.com"))
{
this.CreateEmailItem(subjectEmail, contact
.Email1Address, bodyEmail);
}
}
}
private void CreateEmailItem(string subjectEmail, string toEmail, string bodyEmail)
{
Outlook.Application Application = new Outlook.Application();
Outlook.MailItem eMail = (Outlook.MailItem)
this.Application.CreateItem(Outlook.OlItemType.olMailItem);
eMail.Subject = subjectEmail;
eMail.To = toEmail;
eMail.Body = bodyEmail;
eMail.Importance = Outlook.OlImportance.olImportanceLow;
((Outlook._MailItem)eMail).Send();
}
#endregion
答案 0 :(得分:1)
这行代码正在寻找一个名为Application
的类级别字段,我认为该字段不存在:
Outlook.MAPIFolder sentContacts = (Outlook.MAPIFolder)
this.Application.ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);
在方法中创建Application
的实例(就像你说的那样),然后从上面的代码中删除关键字this
:
Outlook.Application Application = new Outlook.Application();
Outlook.MAPIFolder sentContacts = (Outlook.MAPIFolder)
Application.ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);
如果您需要访问该方法之外的Application
,那么在类级别(在任何方法之外)创建Application
并在构造函数内实例化它(如果这适合您的情况)。