Windows CE 3.0Windows CE 2.11Pocket PC 2002Windows MobileVisual Studio .NET 2002.NET 1.0Windows 2000Windows XP移动应用MFC中级开发Visual StudioWindowsC++.NETC#
使用系统托盘应用程序检查、发送和配置电子邮件






1.67/5 (5投票s)
2002年8月16日
3分钟阅读

162967

4008
本文演示了系统托盘应用程序的创建、进程的创建以及 XML 数据的读取和写入
引言
本文旨在演示系统托盘应用程序的创建,该应用程序使用上下文菜单。本文还解释了进程的创建以及 XML 数据的处理(读取和写入)。
为什么需要桌面邮件检查器?
让我坦白一些事情!几乎一天 24 小时都有大量的邮件涌入我。我有点疯狂地想检查邮件。我检查我的邮件,再检查,一遍又一遍地重复这个过程。我感觉让我的邮件客户端一直打开很烦人,我想做些什么来克服这个问题。
我想编写一个应用程序,它位于您的系统托盘上,并且足够方便,可以轻松地检查邮件而无需任何麻烦。另一件事是,我想编写一个系统托盘应用程序,并想在运行时创建进程,万岁!我发表了这篇文章。
该应用程序有两个类 - DesktopMailChecker
和mailDomain
类。
DesktopMailChecker
类有一个notifyicon
控件。状态区域中的图标是计算机后台运行的进程的快捷方式,例如病毒防护程序或音量控制。这些进程没有自己的用户界面。 NotifyIcon
提供了一种在此功能中进行编程的方法。 Icon定义出现在状态区域中的图标。图标的弹出菜单通过ContextMenu
和上下文菜单控件来寻址。 ContextMenu
类表示当用户右键单击控件或窗体的区域时可以显示的快捷菜单。此类还使用Process
类启动进程的创建。
MailDomain
类具有复选框控件来接收用户选项,并且结果在 XML 文件中更新。 XMLTextReader
和XMLTextWriter
用于在应用程序中读取和写入 XML 数据。
创建系统托盘应用程序之前要记住的事情......
- 在项目中包含
NotifyIcon
类。 - 使用
ContextMenu
类为NotifyIcon
类提供菜单 - 创建图标或使用图标到
NotifyIcon.Icon
代码和插图如下。
DesktopMailChecker
类包含创建进程的代码。
private void menuItem4_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Checking Mails","Message");
// Create a Process Object
Process myProcess = new Process();
// Get the process start information of outlook
ProcessStartInfo myProcessStartInfo =
new ProcessStartInfo("outlook.exe");
// Assign 'StartInfo' of outlook to 'StartInfo' of 'myProcess' object.
myProcess.StartInfo = myProcessStartInfo;
// Create a outlook
myProcess.Start();
createProc();
}
这是另一种方法,用于创建 Web 浏览器,以防您想通过浏览器界面检查邮件。
// This is another procedure that creates an browser process
private void createIEProc(String site)
{
Process myProcess = new Process();
// Get the process start information of iexplore
ProcessStartInfo myProcessStartInfo =
new ProcessStartInfo("iexplore.exe",site);
// Assign 'StartInfo' of outlook to 'StartInfo' of 'myProcess' object.
myProcess.StartInfo = myProcessStartInfo;
// Create a outlook
myProcess.Start();
}
MailDomain
类中的方法存储用户响应,邮件提供商站点打开的选项。 这是一个示例代码,说明了如何将 XML 数据读写到文件和从文件中读取。 这就是读取 xml 文件的方式。
private void MailDomain_Load(object sender, System.EventArgs e)
{
XmlTextReader reader = new XmlTextReader ("maillist.xml");
String list ="";
if(!reader.EOF)
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an Element
while (reader.MoveToNextAttribute()) // Read attributes
{
MessageBox.Show(reader.Value);
list=reader.Value+","+list;
}
break;
case XmlNodeType.DocumentType: // The node is a DocumentType
break;
default:
break;
}
}
.......
reader.Close();
}
else
{
// Do Something else
}
}
这段代码演示了写入 XML 文件。
private void updateXMLdoc()
{
String []tok;
tok = textBox1.Text.Split(',');
if(textBox1.Text!="")
{
XmlTextWriter xtw= new XmlTextWriter ("maillist.xml", null);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument(false);
xtw.WriteDocType("maildomain", null, null, null);
xtw.WriteComment("This file represents the mail sites the user uses");
xtw.WriteStartElement("Data");
for(int i=0;i<tok.Length;i++)
{
xtw.WriteStartElement("site", tok[i]);
xtw.WriteEndElement();
}
xtw.WriteEndElement();
//Write the XML to file and close the xtw
xtw.Flush();
xtw.Close();
}
}
更新
try
{
//Return a reference to the MAPI layer
oApp = new Outlook.Application();
oNameSpace= oApp.GetNamespace("MAPI");
// Logs on the user
oNameSpace.Logon(null,null,true,true);
//gets defaultfolder for my Outlook Outbox
oOutboxFolder = oNameSpace.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderOutbox);
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(
Outlook.OlItemType.olMailItem);
oMailItem.To = textBox1.Text;
oMailItem.Subject = textBox2.Text;
oMailItem.Body = textBox3.Text;
oMailItem.SaveSentMessageFolder = oOutboxFolder;
//The draft is saved
oMailItem.Save();
//adds it to the outbox
oMailItem.Send();
if(oMailItem.Sent || oOutboxFolder.UnReadItemCount!=0)
oOutboxFolder.MoveTo(oNameSpace.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderSentMail));
this.Close();
//this.Refresh();
}
catch
{
this.Dispose();
}
结论
本文解释了进程的创建,使用 processinfo 并创建由名称或sysID
指定的进程。 本文演示了系统托盘应用程序的创建。 此应用程序还演示了 XML 数据的读取和写入。 此应用程序演示了使用MSOutlook
发送邮件。