Subscribe Now!

Enter your email address:

Monday, June 20, 2016

Send Crystal Report to PDF in Email Attachment

Export Crystal Report to PDF & send in Email as Attachment in ASP.Net using C# (MVC C#)

Send Crystal Report to PDF in Email Attachment


Name Spaces 

using System.Net.Http.Headers;
using System.IO;
using System.Net.Mail;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Net;
using System.Data;
using System.Configuration;





Method To send Email


 public ActionResult PrintJobOffer()
        {                      
               myReport.FileName = Server.MapPath("~/Report/Permanant.rpt");
                            myReport.Load();
                            myReport.SetParameterValue("FULLNAME", Data.FULLNAME);
                            myReport.SetParameterValue("NATIONALITY", Data.NATIONALITY);
                            myReport.SetParameterValue("PASSPLACEOFISSUE", Data.PASSPLACEOFISSUE);
                           
                            using (MailMessage message = new MailMessage())
                            {
                                string messageto = "amdi@gmail.com"; // to Email Address
                                message.From = new MailAddress("no_reply@med.com");
                                message.To.Add(new MailAddress(messageto));
                                message.CC.Add(new MailAddress("copy@domain.com"));
                                message.Subject = "Job Offer - HR Department";
                                message.Attachments.Add(new Attachment(myReport.ExportToStream(ExportFormatType.PortableDocFormat), "Permanant.pdf")); //Code For Generate Pdf and sending automatically

                                string strtpath = System.Web.HttpContext.Current.Server.MapPath("~");
                                Path.GetFullPath(strtpath).Replace(@"/", @"//");

                                System.Net.Mail.Attachment attachment1;
                                attachment1 = new System.Net.Mail.Attachment(strtpath + "/assets/images/Confidential_Agreement_Form.pdf");
                                attachment1.Name = "Confidential_Agreement_Form.pdf";
                                message.Attachments.Add(attachment1);

                                System.Net.Mail.Attachment attachment2;
                                attachment2 = new System.Net.Mail.Attachment(strtpath + "/assets/images/Prior_of_Arraiving.pdf");
                                attachment2.Name = "Prior_of_Arraiving.pdf";
                                message.Attachments.Add(attachment2);              
                         
                                message.IsBodyHtml = true;
                                message.Body = "Dear Candidate"; // Body Content
                                SmtpClient client = new SmtpClient();
                                client.Host = "10.90.10.10"; // Server Email Ip Address 
                                client.Port = 25; // Port For Email
                                client.Send(message);
For Gmail
NetworkCredential credential = new NetworkCredential();
        credential.UserName = "sender@gmail.com";
        credential.Password = "xxxxx";
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = credential;
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.Send(message);



                            }
                        }

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...