简体   繁体   English

VSTO Outlook 插件附加文件

[英]VSTO Outlook Addin attached files

I'm working on a VSTO Outlook add-in.我正在开发 VSTO Outlook 加载项。 One of the actions I want to add is for it to load 2 files from the ATTACHMENTS folder in the project folder:我要添加的操作之一是让它从项目文件夹中的 ATTACHMENTS 文件夹中加载 2 个文件:

  1. Load the HTML file in the email body在电子邮件正文中加载 HTML 文件
  2. Attach the PDF file to the reply.将 PDF 文件附加到回复中。

I've tried a few ways but I can't consistently find the files, even though they are marked with "Copy to output directory"="Copy always" Am I addressing them correctly in the code below or am I doing something wrong?我尝试了几种方法,但我无法始终如一地找到这些文件,即使它们被标记为“复制到输出目录”=“总是复制”我是在下面的代码中正确地处理它们还是我做错了什么? Another question, Is there a different between debug and release version that I should handle in the code, in regards, to the files path?另一个问题,关于文件路径,我应该在代码中处理的调试版本和发布版本之间是否存在不同?

            string sPathHtml = @"\ATTACHMENTS\VIA.htm" ;
            string sPathAttachments=  @"\ATTACHMENTS\VIA for RW Pricing team - user guide.pdf";
            string sViaHtml = System.IO.File.ReadAllText(sPathHtml);

=

public static void AskForViaTicket()
            {
            //GET EMAIL
            EMAIL=getEmail_FirstSelected();

            //VIA HTML
            string startupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            System.Diagnostics.Process.Start(startupPath);
            string sPathHtml = @"\ATTACHMENTS\VIA.htm" ;
            string sPathAttachments=  @"\ATTACHMENTS\VIA for RW Pricing team - user guide.pdf";
            string sViaHtml = System.IO.File.ReadAllText(sPathHtml);

            //REPLY
            Outlook.MailItem reply = EMAIL.ReplyAll();
            reply.HTMLBody = String.Concat("Hi ", EMAIL.SenderName.Split(',')[1], "\n", sViaHtml, "\n", reply.HTMLBody);
            reply.Attachments.Add(sPathAttachments);
            reply.SentOnBehalfOfName = "RW-PricingSupport@iqvia.com";
            reply.CC = "RW-PricingSupport@iqvia.com";
            reply.Display();
            }

You need to specify the absolute file path instead.您需要指定绝对文件路径。 The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.附件的来源可以是文件(由带有文件名的完整文件系统路径表示)或构成附件的 Outlook 项目。

You may try using the Environment.CurrentDirectory property which returns the fully qualified path of the current working directory.您可以尝试使用Environment.CurrentDirectory属性,它返回当前工作目录的完全限定路径。 Then you could add subfolders (relative path) to build the full file system path.然后您可以添加子文件夹(相对路径)来构建完整的文件系统路径。

Always use absolute path.始终使用绝对路径。

If the file are in a folder relative to the location of you addin dll, use Assembly.GetExecutingAssembly().CodeBase :如果文件位于相对于您添加 dll 的位置的文件夹中,请使用Assembly.GetExecutingAssembly().CodeBase

    //use CodeBase instead of Location because of Shadow Copy.
    string codebase = Assembly.GetExecutingAssembly().CodeBase;
    var vUri = new UriBuilder(codebase);
    string vPath = Uri.UnescapeDataString(vUri.Path + vUri.Fragment);
    string directory = Path.GetDirectoryName(vPath);
    if (!string.IsNullOrEmpty(vUri.Host)) directory = @"\\" + vUri.Host + directory;
    string sPathHtml = Path.Combine(directory, @"ATTACHMENTS\VIA.htm");
    string sPathAttachments =  Path.Combine(directory, @"ATTACHMENTS\VIA for RW Pricing team - user guide.pdf");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM