创建UFL直接将报告邮寄为PDF

时间:2016-11-09 14:36:30

标签: c# visual-studio-2008 crystal-reports

我需要使用Visual Studio 2010在C#中创建一个用户函数库,允许在CR XI中查看报告的人直接将所述报告作为电子邮件发送。

我从来没有创建过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());
    }
}

1 个答案:

答案 0 :(得分: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 = "destination@email.com";
        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 = "destination@email.com";
        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("user@email.com", "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");
    }

希望这有助于其他人。