2

I am using Crystal Report in ASP.NET. I want to send report as body in mail.

My code is as below, it can convert Crystal Report in to html. My Question is how I can put into a body of the mail?

    public void Button1_Click(object sender, EventArgs e)
    {      
        MemoryStream oStream; // using System.IO
        oStream = (MemoryStream)
        CrystalReportSource1.ReportDocument.ExportToStream(
        CrystalDecisions.Shared.ExportFormatType.HTML40 );
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "text/html";
        Response.BinaryWrite(oStream.ToArray());
        sendmail();    
    }

    private void sendmail()
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add("");
            mailMessage.From = new MailAddress("");
            mailMessage.Subject = "welcome";
            mailMessage.IsBodyHtml = true;

            mailMessage.Body =      /**/ What i can code here??????**?

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = "208.43.62.208";
            smtpClient.Port = 2525;
            smtpClient.Send(mailMessage);
            Response.End();
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.ToString());
        }
    }
NightFury
  • 13,436
  • 6
  • 71
  • 120
vikramm06
  • 23
  • 1
  • 7

1 Answers1

2

In your code sample you are using a MemoryStream for the report's HTML and then writing that stream to the Response. This will make the report display for the user (right?).

What you need to do is to use another stream-type, maybe a StreamReader and get a HTML-string that you can use in your mail instead.

Untested code sample:

MemoryStream oStream; // using System.IO
oStream = (MemoryStream)
CrystalReportSource1.ReportDocument.ExportToStream(
CrystalDecisions.Shared.ExportFormatType.HTML40 );

oStream.Position = 0;
var sr = new StreamReader(ms);
var html= sr.ReadToEnd();

sendmail(html); // Use html as your body

Have a look at this post to read more about getting a string from a MemoryStrea : How do you get a string from a MemoryStream?

Community
  • 1
  • 1
Björn
  • 3,098
  • 2
  • 26
  • 40
  • hey i got it., it's working., coming in mail., but i am getting spaces at the begging, when i open mail., is their any option to avoid it and only display only crystal report content. – vikramm06 Feb 10 '14 at 11:13
  • Great! Regarding the spaces I guess that depends on how Crystal Reports generate the HTML? Maybe you could remove some of the generated HTML code after it has been generated. I don't have Crystal Reports installed so I can't try it out. – Björn Feb 10 '14 at 12:54
  • fine, i exportedformat type into html32, now it's working fine., thanks – vikramm06 Feb 11 '14 at 07:02