I got this error while trying to send document to an email in my site. Please how do I resolve this?
Server Error in '/' Application.
Access to the path 'G:\PleskVhosts\quirver.com\httpdocs\qrimg.jpg' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.UnauthorizedAccessException: Access to the path 'G:\PleskVhosts\quirver.com\httpdocs\qrimg.jpg' is denied. ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access. Source Error:
Line 261: var ImgUrl = Image1.ImageUrl;
Line 262: // Write image as file to folder.
Line 263: File.WriteAllBytes(Server.MapPath("qrimg.jpg"), QRBytes);
Line 264: // convert and set absolute url in image.
Line 265: Image1.ImageUrl = GetUrl("qrimg.jpg");
|
Source File: g:\PleskVhosts\quirver.com\httpdocs\printinvoicetemplate.aspx.cs Line: 263 Stack Trace:
[UnauthorizedAccessException: Access to the path 'G:\PleskVhosts\quirver.com\httpdocs\qrimg.jpg' is denied.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +435
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1242
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +146
System.IO.File.InternalWriteAllBytes(String path, Byte[] bytes, Boolean checkHost) +124
printinvoicetemplate.Button1_Click(Object sender, EventArgs e) in g:\PleskVhosts\quirver.com\httpdocs\printinvoicetemplate.aspx.cs:263
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +11596288
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +274
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1890
|
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4494.0
Here is my code (C#)
protected void Button1_Click(object sender, EventArgs e)
{
byte[] QRBytes = GetQRCodeBytes(ToAbsoluteUrl("/doctemp.aspx") + "?Id=" + Session["user"]);
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
var ImgUrl = Image1.ImageUrl;
// Write image as file to folder.
File.WriteAllBytes(Server.MapPath("qrimg.jpg"), QRBytes);
// convert and set absolute url in image.
Image1.ImageUrl = GetUrl("qrimg.jpg");
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
this.Panel1.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 PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
var worker = new XMLWorker(pipeline, true);
var xmlParse = new XMLParser(true, worker);
pdfDoc.Open();
xmlParse.Parse(sr);
xmlParse.Flush();
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
MailMessage mm = new MailMessage("myemail@gmail.com", this.TextBox1.Text);
mm.Subject = "Document";
mm.Body = "Please find Attached Document";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Document.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "relay-hosting.secureserver.net";
smtp.EnableSsl = false;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "myemail@gmail.com";
NetworkCred.Password = "xxxxxxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.Send(mm);
}
}
}
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Documrnt Sent!');", true);
}