2014-09-21 53 views
1

我正在使用PDFsharp生成作業報告。我需要將該報告自動附加到傳出的電子郵件中。現在,當用戶以角度模式查看作業信息時,他們會單擊Email並生成報告,並顯示一個新的模式並顯示電子郵件輸入字段。如何設置電子郵件控制器以在JobSetupPdfs文件夾中查找正確的PDF?如何將生成的PDF附加到.NET SMTP?

PdfController

public string Get(int id = 1) 
    { 

     JobDataAdapter adapter = new JobDataAdapter(); 
     Job job = new Job(); 
     job = adapter.GetJob(id); 
     if (job == null) 
     { 
      return string.Empty; 
     } 

     try 
     { 
     // Create a new PDF document 
      PdfDocument document = new PdfDocument(); 
      document.Info.Title = "Created with PDFsharp"; 

     gfx.DrawString("Texas Exterior Systems", HeadingFont, XBrushes.Black, 
       new XRect(0, 50, page.Width, page.Height), 
       XStringFormats.TopCenter); 

      gfx.DrawString("Job Setup Sheet", BodyFont, XBrushes.Black, 
       new XRect(0, 80, page.Width, page.Height), 
       XStringFormats.TopCenter); 

     var filename = string.Format(@"C:\Users\texas_000\Desktop\TexasExterior\TexasExterior\JobSetupPdfs\{0}.pdf", job.JobName); 
      document.Save(filename); 
     } 
     catch (Exception ex) 
     { 
      Debug.Print(ex.ToString()); 
     } 
     return string.Empty; 


    } 

郵件控制器

[Authorize] 
    public ActionResult EmailPdf(string To, string Cc, string comments, string Bcc, HttpPostedFileBase fileUploader) 
    { 

     try 
     { 
      SmtpClient client = new SmtpClient("mail.texasicecarving.com", 8889); 
      //client.EnableSsl = true; 
      client.Timeout = 100000; 
      client.DeliveryMethod = SmtpDeliveryMethod.Network; 
      client.UseDefaultCredentials = false; 
      client.Credentials = new NetworkCredential("[email protected]", "******"); 
      MailMessage msg = new MailMessage(); 
      if (fileUploader != null) 
      { 
       string fileName = Path.GetFileName(fileUploader.FileName); 
       msg.Attachments.Add(new Attachment(fileUploader.InputStream, fileName)); 
      } 
      msg.To.Add(new MailAddress("[email protected]")); 
      msg.From = new MailAddress("[email protected]"); 
      // msg.Subject = name + " " + subject; 
      msg.Body = comments; 

      //MailAddress Ccopy = new MailAddress(user.Email); 
      //msg.CC.Add(Ccopy); 

      //MailAddress Bcopy = new MailAddress(Bcc); 
      //msg.Bcc.Add(Bcopy); 

      client.Send(msg); 
      Console.WriteLine("Successfully Sent Message."); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     return View(); 
    } 

角控制器

$scope.EmailPdf = function() { 
    $http.get('/api/Pdf/{id}').success(function() { 
     $scope.PrintPreviewModal(); 
    }); 
} 

更新 OK,所以我把它安裝使用正確的路徑。現在我如何讓它附上剛剛生成的PDF?我是否需要爲PDF添加時間戳並讓msg.Attachments在該文件夾中附加最新的PDF?

msg.Attachments.Add(new Attachment(@"C:\Users\texas_000\Desktop\TexasExterior\TexasExterior\JobSetupPdfs\Restoration.pdf")); 
+2

您可以添加一個流的內容作爲附件。 PDFsharp可以將PDF文件保存到流中。 – 2014-09-22 15:28:10

+0

@ user3919120請參閱上文,使用文件流作爲附件。出於調試目的,在開發和附加文件時保存文件可能會更容易。但真正的生產方式是使用流。 – NinjaMid76 2014-09-24 15:22:22

回答

相關問題