Currently in my program the data in crystal report is exported to pdf and send as email to clients.
But I need to send the pdf as password protected. This is my current code to send email with pdf attachment.
DataTable dt = new DataTable();
String str = "select * from table1 where (Num=@search) and (branch=@search1);";
SqlCommand xp = new SqlCommand(str, con);
xp.Parameters.Add("@search", SqlDbType.VarChar).Value = TextBox1.Text;
xp.Parameters.Add("@search1", SqlDbType.VarChar).Value = DropDownList1.SelectedValue;
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.SetDatabaseLogon("xxxxx", "xxxxxx");
crystalReport.SetDataSource(dt1);
CrystalReportViewer1.ReportSource = crystalReport;
CrystalReportViewer1.DataBind();
using (MailMessage mm = new MailMessage("xxxxxxx", "xxxxxxxx"))
{
mm.Subject = "Test";
mm.Attachments.Add(new Attachment(crystalReport.ExportToStream(ExportFormatType.PortableDocFormat), "Report.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.google.com";
NetworkCredential credential = new NetworkCredential();
credential.UserName = "xxxxxxxx";
credential.Password = "xxxxxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credential;
smtp.Port = 587;
smtp.EnableSsl = false;
smtp.Send(mm);
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"alertMessage",
"alert('message send successfully');", true);
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
alice yangPosted May 11, 2016, 4:03 AM
Bhuvanesh MohankumarPosted May 11, 2016, 1:35 AM
I suggest you to create a PDF using some external libraries and then make them as password protected, which is an option while generating a PDF file.
Just look at the "iTextSharp" assembly which is useful for PDF creation with security also.