2016-11-09 20 views
0

C#でVisual Studio 2010を使用するユーザ関数ライブラリを作成する必要があります。これにより、CR XIのレポートを閲覧しているユーザーが直接レポートを電子メールで送信できます。UFLを作成してPDF形式のレポートを直接メールに送信

私はUFLを作成したことはありません。Crystal Reportsで作業したことはありません。異種バージョンがどのように互換性がないか、どれが正確に並べ替えることが重要か開発を完了する前に協力してください。

私が知る必要があることは...どこから始めるのですか?私はこれに似た質問をいくつか見たことがありますが、レポートを印刷するのではなく、レポートを印刷することがほとんどです。私はこれがStackの通常の内容に比べて少し矯正されていることを認識していますが、自分の足を私の下に置くことはあまりありません。どんな助けでも大歓迎です。

これまでのところ私はこれを持っています。最初のボタンをクリックすると、2番目のボタンが送信されるべきものが準備されますが、その間ずっと何も起こらないようです。私は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"); 
    } 

これは他の誰かを助けることを望みます。

関連する問題