65.9K
CodeProject 正在变化。 阅读更多。
Home

在Outlook电子邮件签名中添加随机名言

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.97/5 (12投票s)

2011年11月29日

CPOL

3分钟阅读

viewsIcon

117169

downloadIcon

1873

在Outlook电子邮件签名中添加随机名言。

引言

这篇文章包含一些我想和大家分享的有趣内容。好吧,我们将讨论如何为我们的电子邮件签名添加随机引言。请注意,我们将使用 Microsoft Office Outlook 2007 作为我们的电子邮件客户端软件。

背景

我喜欢引言,尤其是励志或激励性的引言,并且我非常喜欢在电子邮件签名中使用引言。当我试图为我的电子邮件签名选择引言时,我感到很纠结!我应该选择哪一个呢?有大量的引言可供选择,但我无法选定一个。最终,我决定编写一个程序,该程序将随机地在我的 Outlook 电子邮件签名中添加引言。

那么,让我们从头开始。当我开始编写这个程序时,我有点困惑,因为我无法决定使用哪种方法:是开发一个插件(Add-In)、Windows 服务,还是一个简单的 Win32 窗体应用程序。所以最后我决定采用 Win32 应用程序。

如何做到

我使用非常基本的技术/方法来做到这一点。它们列在下面:

  1. 在 Microsoft Office Outlook 中创建签名。
  2. 在签名底部添加一个关键字 (#quotes#)。
  3. 创建当前签名的模板。
  4. 创建一个包含所有引言的 XML 文档。
  5. 最后,在 Outlook 运行时添加引言。

让我们更详细地讨论一下,并回顾上面的列表。

在 Microsoft Office Outlook 中创建签名

好的,首先,我们将创建一个签名文件;这非常简单。只需打开/运行 Microsoft Office Outlook,然后从“工具”菜单中选择“选项”。将出现一个包含多个选项卡的 Windows 窗体?选择“邮件格式”选项卡。您会找到一个名为“签名…”的按钮,在此选项卡下单击该按钮即可创建您的 Outlook 电子邮件签名。

在签名底部添加关键字 (#quotes#)

太棒了!您已创建签名,现在在签名底部添加关键字(“quotes”)。下图 A 显示了带有签名的关键字。

quotes-init.png

图 A 显示了带有签名的关键字。

创建当前签名的模板

现在我们将创建当前签名的模板。我们将使用此模板来随机选择引言并更新 Microsoft Office Outlook 签名文件。请注意,我在更新 Outlook 签名文件时遇到了很多问题,例如文本编码、字符串操作时出现乱码等。我非常希望能得到您关于如何妥善解决这些问题的建议。实际上,当前的 KOD 代码有点粗糙(我个人意见)。无论如何,下面提供了一个当前场景的示例代码片段。

public void SignatureRawText(int index, bool isStart)
{
    applicationDataDir = Environment.GetFolderPath
    (Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Signatures";
    
    signature = string.Empty;
    DirectoryInfo diInfo = new DirectoryInfo(applicationDataDir);
    if (diInfo.Exists)
    {
        FileInfo[] fiSignature = diInfo.GetFiles("*.htm");
        if (fiSignature.Length > 0)
        {
            applicationDir = Environment.CurrentDirectory;
            fileName = fiSignature[0].Name.Replace
(fiSignature[0].Extension, string.Empty);
            
            if (File.Exists(applicationDir + @"\template.htm"))
            {
                diInfo = null;
                diInfo = new DirectoryInfo(applicationDir);
                fiSignature = diInfo.GetFiles("*.htm");
                StreamReader streamReader = 
        new StreamReader(fiSignature[0].FullName, Encoding.Default);
                signature = streamReader.ReadToEnd();
                streamReader.Close();
            }
            else
            {
                File.Copy(fiSignature[0].FullName, applicationDir + "/template.htm");
            }
            if (!string.IsNullOrEmpty(signature))
            {
                try
                {
                    QuotesService32.Class.IQuotesSignature quotesSignature = 
                        new QuotesSignature();
                    
                    string strQuotes = Environment.NewLine
                                       + "<p style="
                                       + Convert.ToChar(34)
                                       + "font-family: Arial, Helvetica, sans-serif; 
                                       font-size: 10px; font-weight: normal; 
                    font-style: normal; 
                                       color: #008000; text-shadow: 2px 2px 8px #444"
                                       + Convert.ToChar(34)
                                       + ">"
                                       + quotesSignature.GetQuotes(index).Replace
                                       (Convert.ToString(index), "")
                                       + "</p>";
                    if (isStart)
                    {
                        signatureText = signature.Replace("#quotes#", strQuotes);
                    }
                    else
                    {
                        signatureText = signature.Replace("#quotes#", "")
                    }
                    byte[] seeds = System.Text.Encoding.ASCII.GetBytes(signatureText);
                    FileStream fileStream = new FileStream(applicationDataDir + "/" + 
                    fileName + ".htm", FileMode.Open, 
                    FileAccess.ReadWrite, FileShare.ReadWrite);
                    BinaryWriter binaryWriter = new BinaryWriter(fileStream);
                    if (fileStream.CanWrite)
                    {
                        for (int i = 0; i < seeds.Length; i++)
                        {
                            if ((seeds[i]) != Convert.ToByte('?'))
                            {
                                binaryWriter.Write(seeds[i]);
                            }
                        }
                        
                        binaryWriter.Flush();
                        binaryWriter.Close();
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
        }
    }
}

一个检查您的 Office Outlook 是否正在运行的函数

public bool IsOutlook7Running()
{
    bool retVal = false;
    try
    {
        foreach (Process availableProcess in Process.GetProcesses("."))
        {
            if (availableProcess.MainWindowTitle.Length > 0)
            {
                //Check the available process 
                if (availableProcess.ProcessName == "OUTLOOK")
                {
                    return true;
                }
            }
        }
    }
    catch (Exception exception)
    {
        retVal = false;
        throw exception;
    }
    return retVal;
}

创建一个包含所有引言的 XML 文档

我们将创建一个非常简单的 XML 文件,其中包含带有 ID 的所有引言。我使用 XPath 从 XML 文档中检索引言。下面提供了一个简单的 XML 模板。

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2011-11-26T15:32:29">
<T_QUOTES_TEXT>
<id_quotes_key>1</id_quotes_key>
<tx_quotes>&quot;What lies behind us and what lies before us are tiny matters 
compared to what lies within us.&quot; - Ralph Waldo Emerson</tx_quotes>
</T_QUOTES_TEXT>
<T_QUOTES_TEXT>
<id_quotes_key>2</id_quotes_key>
<tx_quotes>&quot;God, grant me the serenity to accept the things I cannot change, 
the courage to change the things I can, and the wisdom to know the difference.&quot; 
- Reinhold Niebuhr</tx_quotes>
</T_QUOTES_TEXT>
<T_QUOTES_TEXT>
<id_quotes_key>3</id_quotes_key>
<tx_quotes>&quot;Honesty is the first chapter in the book of wisdom.&quot; 
- Author Unknown</tx_quotes>
</T_QUOTES_TEXT>
<T_QUOTES_TEXT>
.........

一个从 XML 文档中检索引言的示例代码片段如下所示。

public string GetQuotes(int index)
{
    string Quotes = string.Empty;
    
    XPathNavigator xPathNavigator;
    XPathDocument xPathDocument;
    XPathNodeIterator xPathNodeIterator;
    String strExpression;
    applicationDir = Environment.CurrentDirectory;
    try
    {
        xPathDocument = new XPathDocument(applicationDir + 
        @"\Xml\T_QUOTES_TEXT.xml");
        xPathNavigator = xPathDocument.CreateNavigator();
        strExpression = "/dataroot/T_QUOTES_TEXT
        [./id_quotes_key=" + Convert.ToString(index) + "]";
        xPathNodeIterator = xPathNavigator.Select(strExpression);
        while (xPathNodeIterator.MoveNext())
        {
            Quotes += xPathNodeIterator.Current.Value;
        };
    }
    catch
    {
        Quotes = string.Empty;
    }
    
    return Quotes;
}

有关 XPath 的更多信息,您可以点击此链接

最后,在 Outlook 运行时添加引言

好了,我们几乎完成了!现在我们将创建一个简单的 Windows 应用程序,该应用程序将运行并作为系统托盘应用程序显示,带有以下菜单:

  1. Start
  2. 停止
  3. 设置
  4. 退出

图 B 显示了引言的添加,图 C 显示了此程序的运行状态。

quotes-add.png

图 B 显示了您的电子邮件签名中的输出/随机选择的引言。

running-apps.png

图 C 显示了应用程序的运行状态。

关注点

我还会做一些事情,比如使其通用化,进行 XML 数据操作,以及添加更多功能。

结论

我希望大家能理解这个场景。所以,如果您想在 Outlook 电子邮件签名中使用引言,现在您可以创建自己的应用程序了。

历史

  • 2011 年 11 月 29 日星期二:初始帖子
© . All rights reserved.