Richiamo di un allegato per la Mail



1. Creiamo nella nostra funzione in cui stiamo implementando la funzione di invio mail:

 

MailAttachment[] allegato = new MailAttachment[1];
allegato[0] = creaAllegato(parametro);

 

 

2. Creiamo la funzione necessaria per il ritorno del file allegato:

 

   private MailAttachment creaAllegato(string parametro)
        {
            ReportViewer viewer = new ReportViewer();
            viewer.ServerReport.ReportServerUrl = new Uri("http://192.168.0.29/ReportServer");
            viewer.ServerReport.ReportPath = "/PHSYS_Report/NomeReport";
            ReportParameter[] parameters = new ReportParameter[1];
            parameters[0] = new ReportParameter("IDUtente", parametro);
            viewer.ServerReport.SetParameters(parameters);
         
  // Impostare il formato di output del report a PDF
            viewer.ServerReport.Refresh();
            byte[] bytes = viewer.ServerReport.Render("PDF");

            // Scrivere i byte del report in un file PDF
            string Path = WebConfigurationManager.AppSettings["pathFisicaAssegnaVettore"];
            string FileName = "ListaPrelievo_" + parametro + ".pdf";
            System.IO.File.WriteAllBytes(Path + FileName, bytes);
            MailAttachment report = new MailAttachment(Path + FileName);
            return report;
        }