2016-11-09 74 views
0

我需要在Visual Studio 2010中創建一個C#用戶函數庫,這將允許有人在CR XI中查看報告以電子郵件的形式直接發送報告。創建UFL將報告直接郵寄爲PDF

我從來沒有創建了UFL,我從來沒有使用Crystal Reports的工作,我充分認識到不同的版本是多麼的不兼容的,這是多麼重要的精確理清我會哪一個在能夠完成任何開發之前進行合作。

我需要知道的是......我甚至從哪開始呢?我已經看到類似這樣的一些問題,但大多數都涉及打印報告,而不是通過電子郵件發送。我認識到這與Stack的通常內容相比有點補救,但我沒有太多的運氣讓我的腳在我身下,我自己。任何幫助都將不勝感激。

這是我到目前爲止。點擊第一個按鈕可以預先點擊第二個按鈕應該發送的內容,但似乎並沒有發生任何事情。我被告知System.Web.Mail.SmtpMail已過時,但還沒有找到任何更好的工作。

private void button1_Click(object sender, EventArgs e) 
{ 
    cryRpt = new ReportDocument(); 
    cryRpt.Load("CrystalReport1.cs"); 
    crystalReportViewer1.ReportSource = cryRpt; 
    crystalReportViewer1.Refresh(); 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     ExportOptions CrExportOptions; 
     DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions(); 
     PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions(); 
     CrDiskFileDestinationOptions.DiskFileName = pdfFile; 
     CrExportOptions = cryRpt.ExportOptions; 
     CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; 
     CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; 
     CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions; 
     CrExportOptions.FormatOptions = CrFormatTypeOptions; 
     cryRpt.Export(); 

     sendmail(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 

private void sendmail() 
{ 
    try 
    { 
     SmtpMail.SmtpServer.Insert(0, "your hostname"); 
     MailMessage Msg = new MailMessage(); 
     Msg.To = "to address here"; 
     Msg.From = "from address here"; 
     Msg.Subject = "Crystal Report Attachment "; 
     Msg.Body = "Crystal Report Attachment "; 
     Msg.Attachments.Add(new MailAttachment(pdfFile)); 
     System.Web.Mail.SmtpMail.Send(Msg); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 
+0

我認爲我們正在尋找類似於此的東西 http://www.crystalkeen.com/tools/mail.html – Eiketsu

回答

0

我發現了一種通過Outlook和smtp完成此操作的方法。如果Outlook運行的是而不是,則會打開一個實例,發送電子郵件,然後關閉。如果它已經在運行,它將使用當前實例執行相同的操作,只要程序和Outlook都以相同級別的特權運行即可。

private void button1_Click_1(object sender, EventArgs e) 
{ 
    { 
     cryRpt = new ReportDocument(); 
     cryRpt.Load("CrystalReport1.rpt"); 
     crystalReportViewer1.ReportSource = cryRpt; 
     crystalReportViewer1.Refresh(); 
    } 
    try 
    { 
     ExportOptions CrExportOptions; 
     DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions(); 
     PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions(); 
     CrDiskFileDestinationOptions.DiskFileName = pdfFile; 
     CrExportOptions = cryRpt.ExportOptions; 
     CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; 
     CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; 
     CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions; 
     CrExportOptions.FormatOptions = CrFormatTypeOptions; 
     cryRpt.Export(); 
     sendmail(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 

private void sendmail() 
{ 
    Outlook.Application app = null; 

    if (Process.GetProcessesByName("OUTLOOK").Length > 0) 
    { 
     app = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; 
    } 
    else 
    { 
     app = new Outlook.Application(); 
    } 

    try 
    { 
//In case of Outlook 
     Outlook.TaskItem tsk = (Outlook.TaskItem)app.CreateItem(Outlook.OlItemType.olTaskItem); 
     Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; 
     Outlook.AddressEntry currentUser = app.Session.CurrentUser.AddressEntry; 

     mail.Attachments.Add(pdfFile); 
     mail.Subject = "This is the subject"; 
     mail.To = "[email protected]"; 
     mail.Body = "mail with attachment"; 
     mail.Importance = Outlook.OlImportance.olImportanceHigh; 
     mail.Display(false); 
     mail.Send(); 

//In case of smtp - This bit is a bit more temperamental. Suggestions for improvement are welcome. 
     MailMessage mail = new MailMessage(); 
     mail.Subject = "This is the subject"; 
     mail.From = new MailAddress(UserPrincipal.Current.EmailAddress); 
     mail.To = "[email protected]"; 
     mail.Body = "mail with attachment"; 
     Attachment attachment; 
     attachment = new Attachment(pdfFile); 
     mail.Attachments.Add(attachment); 

     SmtpClient SmtpServer = new SmtpClient 
     { 
      "smtp.office365.com", 
      Host = "outlook.office365.com", 
      Port = 587, 
      EnableSsl = true, 
      DeliveryMethod = SmtpDeliveryMethod.Network, 
      UseDefaultCredentials = false, 
      Credentials = new NetworkCredential("[email protected]", "Password") 
      Credentials = CredentialCache.DefaultNetworkCredentials 
      }; 

      SmtpServer.Send(mail); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 
} 

    public static Microsoft.Office.Interop.Outlook.Application GetActiveOutlookApplication() 
    { 
     return (Microsoft.Office.Interop.Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application"); 
    } 

希望這可以幫助別人。