2
Reply

send email with password protected pdf

Sivajihero Hero

Sivajihero Hero

May 11 2016 1:22 AM
264
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);
}

Answers (2)