0

I have feedback form on my site and I have <input type="file"> in my form, so sometimes it will be need to add attachment to email.
I created <input type="file"> in my form

 @Html.TextBoxFor(model => model.ProjectInformation, null, new { type = "file", @class = "input-file" }) 

then in my controller I create email and try to add attachment

    [HttpPost]
    public ActionResult Feedback(FeedbackForm Model)
    {
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.BodyEncoding = Encoding.UTF8;

        msg.From = new MailAddress("[email protected]", @Resources.Global.Feedback_Email_Title);
        msg.To.Add("[email protected]");

        string message = @Resources.Global.Feedback_Name + ": " + Model.Name + "\n"
                        + @Resources.Global.Feedback_Email + ": " + Model.Email + "\n"
                        + @Resources.Global.Feedback_Phone + ": " + Model.Phone + "\n"
                        + @Resources.Global.Feedback_Company + ": " + Model.Company + "\n\n"
                        + Model.AdditionalInformation;
        msg.Body = message;
        msg.IsBodyHtml = false;

        //Attachment
        if (Model.ProjectInformation != null)
        {
            HttpPostedFileBase attFile = Model.ProjectInformation;
            int attachFileLength = attFile.ContentLength;
            if (attachFileLength > 0)
            {
                string strFileName = Path.GetFileName(Model.ProjectInformation.FileName);
                Model.ProjectInformation.SaveAs(Server.MapPath(strFileName));
                Attachment attach = new Attachment(Server.MapPath(strFileName));                   
                msg.Attachments.Add(attach);
                string attach1 = strFileName;
            }
        }

        SmtpClient client = new SmtpClient("smtp.mail.ru", 25);
        client.UseDefaultCredentials = false;
        client.EnableSsl = false;

        try
        {
            client.Send(msg);
        }

        catch (Exception ex)
        {
        }

        FeedbackForm tempForm = new FeedbackForm();
        return View(tempForm);
    } 

but I think I need to delete attachment after sending and I try to add code here

        try
        {
            client.Send(msg);
            if (attach1 != null)
            File.Delete(Server.MapPath(attach1));
        }

but I get some mistakes

enter image description here

and

enter image description here

What should I do to fix that?

0

1 Answer 1

1

You should declare a variable before if (Model.ProjectInformation != null)

Something like this:

    string attach1;
    if (Model.ProjectInformation != null)
    {
        . . .
        if (attachFileLength > 0)
        {
            . . .
            attach1 = strFileName;
        }
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, it helped, but the second mistake i.sstatic.net/aDFxO.png is not fixed.
@Heidel Invalid namespace, you should use System.IO.File
I use using System.IO; but it doesnt help, the same mistake.
@Heidel You still have namespace conflict. Replace File.Delete() with System.IO.File.Delete(), compiler can't understand which File you would like to use.
@Heidel It's possible, that your if check won't set value for attach1 and compiler show error. Declare attach1 with assignment: string attach1 = null;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.