Реализация службы для работы с Outlook
Цитата:
public partial class Service1 : ServiceBase
{
private System.Timers.Timer timer = null;
public Service1()
{
InitializeComponent();
timer = new System.Timers.Timer(30);//создаём объект таймера
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
MailAttach SaveAttach = new MailAttach();
SaveAttach.ThisApplication_NewMail();
}
protected override void OnStart(string[] args)
{
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
}
}
{
private System.Timers.Timer timer = null;
public Service1()
{
InitializeComponent();
timer = new System.Timers.Timer(30);//создаём объект таймера
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
MailAttach SaveAttach = new MailAttach();
SaveAttach.ThisApplication_NewMail();
}
protected override void OnStart(string[] args)
{
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
}
}
Код класса:
Цитата:
class MailAttach
{
Microsoft.Office.Interop.Outlook.Application application =
new Microsoft.Office.Interop.Outlook.Application();
public void ThisApplication_NewMail()
{
Microsoft.Office.Interop.Outlook.MAPIFolder inBox = application.ActiveExplorer()
.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook
.OlDefaultFolders.olFolderInbox);
Microsoft.Office.Interop.Outlook.Items inBoxItems = inBox.Items;
Microsoft.Office.Interop.Outlook.MailItem newEmail = null;
inBoxItems = inBoxItems.Restrict("[Unread] = true");
foreach (object collectionItem in inBoxItems)
{
newEmail = collectionItem as Microsoft.Office.Interop.Outlook.MailItem;
if (newEmail != null)
{
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail
.Attachments.Count; i++)
{
newEmail.Attachments.SaveAsFile
(@"C:\TestFileSave\" +
newEmail.Attachments.FileName);
}
}
}
}
}
}
{
Microsoft.Office.Interop.Outlook.Application application =
new Microsoft.Office.Interop.Outlook.Application();
public void ThisApplication_NewMail()
{
Microsoft.Office.Interop.Outlook.MAPIFolder inBox = application.ActiveExplorer()
.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook
.OlDefaultFolders.olFolderInbox);
Microsoft.Office.Interop.Outlook.Items inBoxItems = inBox.Items;
Microsoft.Office.Interop.Outlook.MailItem newEmail = null;
inBoxItems = inBoxItems.Restrict("[Unread] = true");
foreach (object collectionItem in inBoxItems)
{
newEmail = collectionItem as Microsoft.Office.Interop.Outlook.MailItem;
if (newEmail != null)
{
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail
.Attachments.Count; i++)
{
newEmail.Attachments.SaveAsFile
(@"C:\TestFileSave\" +
newEmail.Attachments.FileName);
}
}
}
}
}
}
Почему служба не обращается к классу?