3

我希望从我的程序中生成 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);
        }
4

1 回答 1

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();

wheregetUserName()是一种仅提取用户名的方法(可以使用子字符串或正则表达式)。

  • 确保邮件的正文是有效的 HTML
  • /n 不会给你一个你应该使用<br>insted 的新行。
于 2013-10-03T04:33:52.620 回答