从C#打开新的Outlook消息

时间:2013-10-02 13:15:54

标签: c# outlook html-email

我希望从我的程序中生成Outlook消息,我能够在程序内构建和发送,或者构建和保存,我想要构建然后显示以允许用户手动选择收件人AD列表...下面的代码是这里和其他教程网站的混合示例,但是我找不到只是构建然后“显示”电子邮件而不保存草稿或从程序中发送...

我也希望找到一种方法可以在电子邮件IE中创建UNC链接:写出用户文件夹\\ unc \ path \%USERNAME%或喜欢的路径

private void sendEmailOutlook(string savedLocation, string packageName)
    {
        try
        {
            Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

            oMsg.HTMLBody = "Attached is the required setup files for your <i><b>soemthing</i></b> deployment package.";
            oMsg.HTMLBody += "\nPlease save this file to your network user folder located.<br /><br/>\\\\UNC\\data\\users\\%USER%\\";
            oMsg.HTMLBody += "\nOnce saved please boot your Virtual machine, locate and execute the file at <br /> <br />\\\\UNC\\users\\%USER%\\";

            int pos = (int)oMsg.Body.Length +1;
            int attachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;

            Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add(savedLocation, attachType, pos, packageName);

            oMsg.Subject = "something deployment package instructions";
            oMsg.Save();

        }
        catch(Exception ex)
        {
            Console.WriteLine("Email Failed", ex.Message);
        }

1 个答案:

答案 0 :(得分:4)

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

oMsg.Subject = "something deployment package instructions";
oMsg.BodyFormat = OlBodyFormat.olFormatHTML;
oMsg.HTMLBody = //Here comes your body;
oMsg.Display(false); //In order to display it in modal inspector change the argument to true

关于您应该能够使用的文件夹的链接(如果您知道用户名):

<a href="C:\Users\*UserName*">Link</a>

许多公司的员工用户名都附加到地址条目(看起来像“John Doe(Jdoe)”,其中Jdoe是用户名)。 当您的用户选择收件人或尝试发送电子邮件时,您可以捕获这些事件,并执行类似

的操作
foreach (Outlook.Recipient r in oMsg.Recipients)
{
    string username = getUserName(r.Name);//  or r.AddressEntry.Name instead of r.Name
    oMsg.HTMLBody += "<a href='C:\\Users\\" + username  + "'>Link</a>"
}
oMsg.Save();
oMsg.Send();

其中getUserName()是仅提取userName的方法(可以使用子字符串或RegEx)。

  • 确保邮件正文是有效的HTML
  • / n不会为您提供使用<br> insted的新行。