-1

I am created a report viewer using rdlc report system. but the problem is got, when I send this file by email then I found an error. My program is something like that:-

at first, I created a reporting process, which shows my "Order" database's all information. that information shows me as one file system on my desktop when I run my application. this file, I want to send by email. but when I trying to send this, I found many errors.

Here is my code:-


   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",
                "pri@gmail.com"));
                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("pri@gmail.com",
                    "MyPassword");
                    client.Send(message);
                    client.Disconnect(true);
                }


                //Email sender end


//other code

I also use bb.Attachments.Add("s", result.MainStream); instead of bb.Attachments.Add("s", wc.DownloadData("path")); when I use this, then I found an unexpected email. In this email's file, I found a lot of code. so now bb.Attachments.Add("s", wc.DownloadData("path")); I am using this process to attach a file. but here I found a different error.

Here is my output:-

enter image description here

How I will solve this problem.How I send my created file by email. I am still a beginner, please help.

Pritom Sarkar
  • 2,154
  • 3
  • 11
  • 26
  • What's the purpose of code block `using (var wc = new WebClient())` ? You don't need it. – Chetan Jan 04 '21 at 09:44
  • https://stackoverflow.com/questions/37853903/can-i-send-files-via-email-using-mailkit – Chetan Jan 04 '21 at 09:46
  • `wc.DownloadData("path"));` are you sure you want the quotes there...? – Franz Gleichmann Jan 04 '21 at 09:47
  • @FranzGleichmann I want my created file to send by mail. I using ``` wc.DownloadData("path")); ``` for sending mail. – Pritom Sarkar Jan 04 '21 at 09:50
  • yeah. i get that. i got that when i read the code. my question is: do you want to download it to a file named `"path"`, or to the filepath you declared in your _variable_ `path`...? – Franz Gleichmann Jan 04 '21 at 09:53
  • @ChetanRanpariya please clarify the code, where should change my code for sending the file. – Pritom Sarkar Jan 04 '21 at 09:53
  • @FranzGleichmann I declare a variable "path".here path i use for creating file process. ``` wc.DownloadData("path")); ``` and here is used just my experiment to know that it's work or not for send mail – Pritom Sarkar Jan 04 '21 at 09:57
  • @FranzGleichmann and I don't want download to the filename path – Pritom Sarkar Jan 04 '21 at 10:02
  • " and I don't want download to the filename path" - and that's what you're ***doing*** by using `"path"` instead of `path`. – Franz Gleichmann Jan 04 '21 at 10:02
  • @FranzGleichmann Now i am using a path instead of "path".error gone, but in this file, I can not see my database information which i want – Pritom Sarkar Jan 04 '21 at 10:11
  • @FranzGleichmann This Email file shows a lot of code. which is not expected – Pritom Sarkar Jan 04 '21 at 10:13
  • You can attach the file by doing `bb.Attachments.Add("Myfile.pdf", result.MainStream.Stream);` – Chetan Jan 04 '21 at 10:32
  • @ChetanRanpariya i found a red error below "Stream". error is "byte[] does not contain the definition of a 'stream'" – Pritom Sarkar Jan 04 '21 at 10:36
  • Sorry, I think `.Stream` is not needed... you can try `bb.Attachments.Add("Myfile.pdf", result.MainStream);` – Chetan Jan 04 '21 at 10:38
  • @ChetanRanpariya Thanks a lot, works perfectly. if you answer my question, I will accept your answer :) – Pritom Sarkar Jan 04 '21 at 10:42
  • I think you are trying to use `AspNetCore.Reporting` for generating reports. This library is last updated in 2018 and it is not being developed actively. You should look for some other option for creating reportes in asp.net core. – Chetan Jan 04 '21 at 10:42
  • @ChetanRanpariya you are right, can you tell me please what other option I should follow for reports as an intermidiate level. – Pritom Sarkar Jan 04 '21 at 10:45

1 Answers1

1

From the code it looks like you are using AspNetCore.Reporting library for generating reports from RDLC files.

Code of this library is available on GitHub at https://github.com/amh1979/AspNetCore.Reporting

LocalReport class from this library has Execute method which return an instance of ReportResult class.

Code of both these classes is located at https://github.com/amh1979/AspNetCore.Reporting/blob/master/AspNetCore.Reporting/LocalReport.cs

ReportResult class has a Property MainStream which represents the content of the report as Byte Array.

Now, attachment to MimeMessage via BodyBuilder support various inputs for file contents such as Stream, byte[] etc.

With this knowledge, I think you can directly use ReportResult.MainStream to attach file to the BodyBuilder.

You can change your code as following to make it working.

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", "pri@gmail.com"));
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("pri@gmail.com", "MyPassword");
    client.Send(message);
    client.Disconnect(true);
}

I hope this will help you resolve your issue.

Chetan
  • 6,711
  • 3
  • 22
  • 32