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"
C#

Avatar of undefined
Last Comment
santanu30in

8/22/2022 - Mon
santanu30in

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.


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
santanu30in

can u tell me what is the "quality_certificate" used in your code
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
santanu30in

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.
MangoMarcus

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/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
santanu30in

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.