从 C# 打开新的 Outlook 邮件

2024-01-22

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

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

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

  • 确保邮件正文是有效的 HTML
  • /n 不会给你一个你应该使用的新行<br> insted.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 C# 打开新的 Outlook 邮件 的相关文章