简体   繁体   中英

VSTO Outlook Addin attached files

I'm working on a VSTO Outlook add-in. One of the actions I want to add is for it to load 2 files from the ATTACHMENTS folder in the project folder:

  1. Load the HTML file in the email body
  2. Attach the PDF file to the reply.

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.

You may try using the Environment.CurrentDirectory property which returns the fully qualified path of the current working directory. 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 :

    //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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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