Hello Forum,
I tried to export an invoice on my web form to PDF and send PDF file as attachment to mail but I got an error. The screenshot below shows the error I got.
protected void Button1_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
MailMessage mm = new MailMessage("georgeakpan13@gmail.com", "ukosimons@gmail.com");
mm.Subject = "GridView Exported PDF";
mm.Body = "Invoice Exported PDF Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "GridViewPDF.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "georgeakpan13@gmail.com";
NetworkCred.Password = "etekamba233#";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
I set my image with this
<asp:Image ID="Image1" runat="server" ImageUrl="http://localhost:49893/Default.aspx/images/qr_code_red.png" Width="80px" Height="80px" />
I am using local server; Is this not the complete image Url ?
the error still shows after. Please is there no other way or a clear understaning on the issue with explanation on how solve this ?