using System; using System.Data; using System.Windows.Forms; using FastReport; using FastReport.Utils; using FastReport.Export.Pdf; using FastReport.Export.Email; private void SendReport_Click(object sender, EventArgs e) { Config.ReportSettings.ShowProgress = false; //Disable progress window Report report1 = new Report(); //Create new report object report1.Load(Environment.CurrentDirectory + "/text.frx"); //Load report report1.Prepare(); //Prepare report PDFExport pdf = new PDFExport(); //Cteate PDF export EmailExport email = new EmailExport(); //Create Email export EmailsDataSet ds = new EmailsDataSet(); EmailsDataSetTableAdapters.CustomerTableAdapter adapter = new EmailsDataSetTableAdapters.CustomerTableAdapter(); adapter.Fill(ds.Customer); DataTable table = ds.Customer; foreach (DataRow row in table.Rows) { SendMessage(report1, pdf, email, row["Email"].ToString(), row["Name"].ToString()); } }这里我们首先创建报表对象,将模板加载到其中,创建导出到PDF和电子邮件。该报告将附在PDF格式的信件中。 接下来,我们创建一个数据源,用数据填充表。我们查看表格条目,并为每个条目发送一封信。当然,如果你有一个庞大的邮件列表,这种方法会很慢。但是你可以在信件的文本中插入客户的名字。 正如您所注意到的,我们已经将这封信发送到一个单独的方法 - SendMessage:
public void SendMessage(Report report, PDFExport pdf, EmailExport email, string recipient, string custName) { email.Account.Address = AddressFromTxt.Text; email.Account.Name = SenderNameTxt.Text; email.Account.Host = HostTxt.Text; email.Account.Port = Convert.ToInt16(PortTxt.Text); email.Account.UserName = UserNameTxt.Text; email.Account.Password = PasswordTxt.Text; email.Account.MessageTemplate = "Test"; email.Account.EnableSSL = true; email.Address = recipient; email.Subject = MailSubjectTxt.Text; email.MessageBody = custName is null? MessageTxt.Text : string.Format("Dear, {0}! {1}", custName, MessageTxt.Text); email.Export = pdf; //Set export type email.SendEmail(report); //Send email }对于Send to all按钮,让我们创建一个OnClick事件处理程序:
private void Send_to_all_Click(object sender, EventArgs e) { Config.ReportSettings.ShowProgress PDFExport(); //Cteate PDF export EmailExport email = new EmailExport(); //Create Email export = ds.Customer; foreach (DataRow row in table.Rows) { if (emails == "") emails ; MessageBox.Show(emails); }与前面的代码不同,在循环中我们添加地址以发送给变量,然后将其传递给消息发送方法。此方法比前一个方法工作得快得多,特别是如果邮件列表很大。为自己判断,在这里你发一封信,而不是一套。 因此,发送报表的任务减少为导出到电子邮件并从数据库向每个地址发送一封信。 购买FastReport.Net正版授权,请点击“咨询在线客服”哟!
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/2308.html
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/2308.html