MangoMarcus
asked on
C# ASP.net attach file to email
II have an asp.net form with a FileUpload function. This section is working correctly:
protected void UploadCertificate(object sender, EventArgs e)
{
if (quality_certificate.HasFi le)
{
string fileName = quality_certificate.FileNa me;
quality_certificate.SaveAs (MapPath(" fileupload s/" + fileName));
LabelQC.Text = "File " + fileName + " has uploaded";
}
else
{
LabelQC.Text = "No file uploaded";
}
}
However when I try to retreive this file on submitting the form, it will not attach to the email presumably because the MapPath line is incorrect? Any advice welcome!
Attachment attachFile = new Attachment(Server.MapPath( "fileuploa ds/" + quality_certificate.FileNa me));
For example when its hard coded it works:
Attachment attachFile = new Attachment(Server.MapPath( "fileuploa ds/logo.gi f"));// THIS WORKS
The directory is fileuploads within the root of the web site.
Permissions set to 775.
identity impersonate="true"
protected void UploadCertificate(object sender, EventArgs e)
{
if (quality_certificate.HasFi
{
string fileName = quality_certificate.FileNa
quality_certificate.SaveAs
LabelQC.Text = "File " + fileName + " has uploaded";
}
else
{
LabelQC.Text = "No file uploaded";
}
}
However when I try to retreive this file on submitting the form, it will not attach to the email presumably because the MapPath line is incorrect? Any advice welcome!
Attachment attachFile = new Attachment(Server.MapPath(
For example when its hard coded it works:
Attachment attachFile = new Attachment(Server.MapPath(
The directory is fileuploads within the root of the web site.
Permissions set to 775.
identity impersonate="true"
ASKER
But is fileName not just a local variable within
protected void UploadCertificate
and therefore is not recognised when used outside of the scope?
Kind Regards
protected void UploadCertificate
and therefore is not recognised when used outside of the scope?
Kind Regards
can u tell me what is the "quality_certificate" used in your code
if "quality_certificate" is a asp.net "fileupload" controller then the control become blank when u use
Server.MapPath("fileupload s/" + quality_certificate.FileNa me
for download the file.
Server.MapPath("fileupload
for download the file.
ASKER
Hi Santanu
The quality_certificate is the ID of the file upload control in the aspx page, here is the code behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls ;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls. WebParts;
using System.Net.Mail;
using System.IO;
public partial class purchasingagreement : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName = Server.MapPath("App_Data/p urchasinga greement.t xt");
string mailBody = System.IO.File.ReadAllText (fileName) ;
mailBody = mailBody.Replace("##qualit y_certific ate##", quality_certificate.FileNa me); // FILE UPLOAD
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Agreement";
myMessage.Body = mailBody;
myMessage.From = new MailAddress("me@company.co .uk", "Company");
myMessage.To.Add(new MailAddress("me@company.co .uk", "Company"));
Attachment attachFile = new Attachment(Server.MapPath( "C:\wwwroo t\123456\t est.compan y.co.uk\we b\content" + fileName));
myMessage.Attachments.Add( attachFile );
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessag e);
FormPanel1.Visible = false;
FormPanelSubmitted.Visible = true;
}
}
// FILE UPLOAD BUTTON AND SAVE FILE TO SERVER
protected void UploadCertificate(object sender, EventArgs e)
{
if (quality_certificate.HasFi le)
{
string fileName = quality_certificate.FileNa me;
quality_certificate.SaveAs (MapPath(" fileupload s/" + fileName));
LabelQC.Text = "File " + fileName + " has uploaded";
}
else
{
LabelQC.Text = "No file uploaded";
}
}
}
}
}
The quality_certificate is the ID of the file upload control in the aspx page, here is the code behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Net.Mail;
using System.IO;
public partial class purchasingagreement : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName = Server.MapPath("App_Data/p
string mailBody = System.IO.File.ReadAllText
mailBody = mailBody.Replace("##qualit
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Agreement";
myMessage.Body = mailBody;
myMessage.From = new MailAddress("me@company.co
myMessage.To.Add(new MailAddress("me@company.co
Attachment attachFile = new Attachment(Server.MapPath(
myMessage.Attachments.Add(
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessag
FormPanel1.Visible = false;
FormPanelSubmitted.Visible
}
}
// FILE UPLOAD BUTTON AND SAVE FILE TO SERVER
protected void UploadCertificate(object sender, EventArgs e)
{
if (quality_certificate.HasFi
{
string fileName = quality_certificate.FileNa
quality_certificate.SaveAs
LabelQC.Text = "File " + fileName + " has uploaded";
}
else
{
LabelQC.Text = "No file uploaded";
}
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
" quality_certificate.SaveAs
But when you try to download this file you use
"Attachment attachFile = new Attachment(Server.MapPath(
it is better to use " fileName" in the place of "quality_certificate.FileN
because some time string "quality_certificate" dose not found due to protected class.