从C#打开新Outlook邮件 [英] Open New Outlook Message from C#

查看:138
本文介绍了从C#打开新Outlook邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在我的计划之内,我能够构建并从程序中发送或建立并保存生成的Outlook邮件,我想是构建然后显示到允许用户手动选择收件人该广告列表...下面的代码是在这里样品等教程网站的查询股价但是,没有,我可以找到只是建立然后单击显示不保存草稿或在程序中发送它的电子邮件...



另外,我希望找到一种方法,我可以创建一个电子邮件IE内部的UNC链接:写一个路径Users文件夹\\unc\path\% USERNAME%或喜欢

 私人无效sendEmailOutlook(字符串savedLocation,字符串的packageName)
{

{
Microsoft.Office.Interop.Outlook.Application oApp =新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 =附件为您的<所需的安装文件; I>< B> soemthing< I> /;< / B>部署包。
oMsg.HTMLBody + =\\\
Please保存这个文件位于你的网络的用户文件夹和LT; BR />< BR /> \\\\UNC\\data\\ \\\users\\%USER%\\;
oMsg.HTMLBody + =\\\
Once保存,请启动虚拟机,定位和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 =部署的东西包装上的说明;
oMsg.Save();

}
赶上(异常前)
{
Console.WriteLine(邮件失败,ex.Message);
}


解决方案

  Microsoft.Office.Interop.Outlook.Application oApp =新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 =部署的东西包装上的说明;
oMsg.BodyFormat = OlBodyFormat.olFormatHTML;
oMsg.HTMLBody = //这里说到你的身体;
oMsg.Display(假); //为了在模式检查显示其更改参数为true



至于链接到文件夹你应该可以使用(如果你知道用户名):

 < A HREF =C:\ Users\ *用户名*>链接< / A> 



很多公司都连接到地址表项,其员工的用户名(看起来像李四( JDOE),其中JDOE是一个用户名)。
,当用户选择一个收件人或试图发送的电子邮件,你能赶上这些事件,像做

 的foreach(在oMsg.Recipients Outlook.Recipient R)
{
串的用户名= getUserName(r.Name); //或r.AddressEntry.Name代替r.Name
oMsg.HTMLBody + =&下; A HREF ='C:\\Users\\+用户名+'>连结&下; / A>中
}
oMsg.Save();
oMsg.Send();



其中, getUserName()是一种方法,只提取用户名(可能使用字符串或者正则表达式)。




  • 确保该邮件的身体是一个有效的HTML

  • / N不会给你,你应该用一个新行< BR> insted的


I am looking to generate an Outlook message from within my program, I am able to build and send from within the program or build and save, what I would like is to build then display to allow the user to manually select recipients from the AD listings... The code below is a mixup of samples here and other tutorial sites however none I can find just build then "display" the email without saving a draft or sending it from within the program...

also I am looking to find a way i can create a UNC link inside of an email IE: write out a path to the users folder \\unc\path\%USERNAME% or the likes

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

Regarding the link to the folder you should be able to use(in case that you know User Name):

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

A lot of companies have their employees user names attached to address entries (looks something like "John Doe(Jdoe)" where Jdoe is a username). when your user select a recipients or tries to send the email you could catch those event, and do something like

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() is a method that extracts only the userName (Could use substring or RegEx).

  • Make sure that mail's body is a valid HTML
  • /n won't give you a new line you should use <br> insted.

这篇关于从C#打开新Outlook邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆