Search code examples
c#asp.net-mvcasp.net-coreasp.net-core-mvcrdlc

failed to send the file by Email


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",
                "[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

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.


Solution

  • 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", "[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);
    }
    

    I hope this will help you resolve your issue.