Link to home
Start Free TrialLog in
Avatar of MangoMarcus
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.HasFile)
        {
            string fileName = quality_certificate.FileName;
            quality_certificate.SaveAs(MapPath("fileuploads/" + 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("fileuploads/" + quality_certificate.FileName));

For example when its hard coded it works:

Attachment attachFile = new Attachment(Server.MapPath("fileuploads/logo.gif"));// THIS WORKS


The directory is fileuploads within the root of the web site.
Permissions set to 775.
identity impersonate="true"
Avatar of santanu30in
santanu30in
Flag of India image

you upload the file like
" quality_certificate.SaveAs(MapPath("fileuploads/" + fileName));"

But when you try to download this file you use
"Attachment attachFile = new Attachment(Server.MapPath("fileuploads/" + quality_certificate.FileName));"

it is better to use " fileName" in the place of  "quality_certificate.FileName" when u download the file.

because some time string "quality_certificate" dose not found due to protected class.


Avatar of MangoMarcus
MangoMarcus

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
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("fileuploads/" + quality_certificate.FileName
for download the file.
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/purchasingagreement.txt");
            string mailBody = System.IO.File.ReadAllText(fileName);

           
            mailBody = mailBody.Replace("##quality_certificate##", quality_certificate.FileName);                   // 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:\wwwroot\123456\test.company.co.uk\web\content" + fileName));
            myMessage.Attachments.Add(attachFile);

            SmtpClient mySmtpClient = new SmtpClient();
            mySmtpClient.Send(myMessage);

            FormPanel1.Visible = false;
            FormPanelSubmitted.Visible = true;
        }
       
       
    }
    // FILE UPLOAD BUTTON AND SAVE FILE TO SERVER
    protected void UploadCertificate(object sender, EventArgs e)
    {
        if (quality_certificate.HasFile)
        {
            string fileName = quality_certificate.FileName;
            quality_certificate.SaveAs(MapPath("fileuploads/" + fileName));
            LabelQC.Text = "File " + fileName + " has uploaded";
        }
        else
        {
            LabelQC.Text = "No file uploaded";
        }
    }
      }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of santanu30in
santanu30in
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial