我使用rdlc报告系统创建了一个报告查看器。但是问题解决了,当我通过电子邮件发送此文件时,我发现了一个错误。我的程序是这样的:
首先,我创建了一个报告过程,其中显示了“订单”数据库的所有信息。当我运行应用程序时,该信息将我显示为桌面上的一个文件系统。这个文件,我想通过电子邮件发送。但是当我尝试发送此邮件时,发现了许多错误。
这是我的代码:-
public async Task<IActionResult> CheckOut(Order11 anOrder)
{
//other code
//report process
string mimetype = "";
int extension = 1;
var path = $"{this._webHostEnvironment.WebRootPath}\\Reports\\Report2.rdlc";
Dictionary<string, string> parameters = new Dictionary<string, string>();
var products = _db.Order.ToList();
LocalReport localReport = new LocalReport(path);
localReport.AddDataSource("DataSet1", products);
var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimetype);
var s= File(result.MainStream, "application/pdf");
//report process end
//Email Sender start
var email = anOrder.Email;
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Ghatia Bazar",
"[email protected]"));
message.To.Add(new MailboxAddress("pritom", email));
message.Subject = "Order Details";
message.Body = new TextPart("plain")
{
Text = "Hi,Thanks For Order.",
};
//add attach
MemoryStream memoryStream = new MemoryStream();
BodyBuilder bb = new BodyBuilder();
using (var wc = new WebClient())
{
bb.Attachments.Add("s",
wc.DownloadData("path"));
}
message.Body = bb.ToMessageBody();
//end attach
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, false);
client.Authenticate("[email protected]",
"MyPassword");
client.Send(message);
client.Disconnect(true);
}
//Email sender end
//other code
我也使用bb.Attachments.Add("s", result.MainStream);
而不是 bb.Attachments.Add("s", wc.DownloadData("path"));
当我使用它时,然后发现了意外的电子邮件。在此电子邮件的文件中,我找到了很多代码。所以现在 bb.Attachments.Add("s", wc.DownloadData("path"));
我正在使用此过程来附加文件。但在这里我发现了另一个错误。
这是我的输出:
我将如何解决此问题。如何通过电子邮件发送创建的文件。我仍然是初学者,请帮忙。
从代码中看,您似乎正在使用AspNetCore.Reporting库从RDLC文件生成报告。
该库的代码可从GitHub上的https://github.com/amh1979/AspNetCore.Reporting获得。
该库中的LocalReport类具有Execute
方法,该方法返回ReportResult
类的实例。
这两个类的代码位于https://github.com/amh1979/AspNetCore.Reporting/blob/master/AspNetCore.Reporting/LocalReport.cs
ReportResult
类具有一个属性MainStream
,该属性将报告的内容表示为字节数组。
现在,通过BodyBuilder附加到MimeMessage的附件支持文件内容的各种输入,例如Stream,byte []等。
有了这些知识,我认为您可以直接使用ReportResult.MainStream
将文件附加到BodyBuilder。
您可以按照以下步骤更改代码以使其正常工作。
var path = $"{this._webHostEnvironment.WebRootPath}\\Reports\\Report2.rdlc";
Dictionary<string, string> parameters = new Dictionary<string, string>();
var products = _db.Order.ToList();
LocalReport localReport = new LocalReport(path);
localReport.AddDataSource("DataSet1", products);
//Following line of code returns an instance of ReportResult class
var result = localReport.Execute(RenderType.Pdf, extension, parameters, mimetype);
var email = anOrder.Email;
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Ghatia Bazar", "[email protected]"));
message.To.Add(new MailboxAddress("pritom", email));
message.Subject = "Order Details";
message.Body = new TextPart("plain")
{
Text = "Hi,Thanks For Order.",
};
var bb = new BodyBuilder();
// Following line will attach the report as "MyFile.pdf".
// You can use filename of your choice.
bb.Attachments.Add("Myfile.pdf", result.MainStream);
message.Body = bb.ToMessageBody();
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, false);
client.Authenticate("[email protected]", "MyPassword");
client.Send(message);
client.Disconnect(true);
}
希望这可以帮助您解决问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句