2

I'm using System.Mail to send emails from my current form. However, now I need the ability to upload files from my form and send them as attachments to the email. However, not every email will have a file attached.

I've looked around and from the examples I've seen it would mean that my code change would make it so that an attachment was required all the time. Is there any way in which I can change my code so that if no attachment is found, the email gets sent as normal?

The email address this is sent to is set in my umbraco back end.

Edit I've now created an Attachment object in my model but I've no idea how to pass the information from the view to the controller. Normally for a lot of things I'd use link like so

@Html.TextBoxFor(m => contactModel.Telephone, new { @class = "form-control", @placeholder = "Phone Number"})

But I cannot seem to find a similar @Html.for allowing file uploads. Am I mistaken in the assumption that there isn't one?

Is my train of thought even correct in that I can pass an attachment like this through MVC?

Controller:

 if (this.ModelState.IsValid)
            {
                System.Net.Mail.MailMessage msg = new MailMessage();
                msg.From = new MailAddress(contactModel.Email);
                msg.ReplyToList.Add(contactModel.Email);
                msg.IsBodyHtml = true;
                msg.Subject = "New Enquiry from Website Contact Form";



                msg.Body += "<p><b>New enquiry from:</b> " + contactModel.Name + "</p>";
                msg.Body += "<p><b>Email Address:</b> " + contactModel.Email + "</p>";
                msg.Body += "<p><b>Phone Number:</b> " + contactModel.Telephone + "</p>";
                msg.Body += "<p><b>Campus:</b> " + contactModel.Campus + "</p>";
                msg.Body += "<br/>";
                msg.Body += "<p><b>Enquiry:</b> " + contactModel.Message + "</p>";
                msg.Attachments.Add(contactModel.Attachment); // add attachement

                msg.To.Add(TargetEmail); //recipient e-mail
                msg.CC.Add(contactModel.Email); //copy the message to the sender

                SmtpClient smtp = new SmtpClient("localhost");

                smtp.Credentials = new System.Net.NetworkCredential("username", "password");


                smtp.Send(msg);
                msg.Dispose();

                if (this.TempData.ContainsKey("FormSuccess"))
                    this.TempData.Remove("FormSuccess"); //remove any previous form session variable

                this.TempData.Add("FormSuccess", (object)true);

                this.ModelState.Remove("Name");


            }

View Form:

using (Html.BeginUmbracoForm<ContactController>("HandleContact", new { TargetEmail = CurrentPage.recipientEmail, Subject = CurrentPage.subject }))
    {           
        @*<fieldset>*@
            <form role="form" class="contactform">
                <div class="col-md-6">
                    @Html.ValidationSummary("contactModel", true)

                    <div class="form-group">
                        @Html.TextBoxFor(m => contactModel.Name, new { @class = "form-control", @placeholder = "Name"})
                        @Html.ValidationMessageFor(m => contactModel.Name)
                    </div>

                    <div class="form-group">
                        @Html.TextBoxFor(m => contactModel.Email, new { @class = "form-control", @placeholder = "Email Address"})
                        @Html.ValidationMessageFor(m => contactModel.Email)
                    </div>

                    <div class="form-group">
                        @Html.TextBoxFor(m => contactModel.Telephone, new { @class = "form-control", @placeholder = "Phone Number"})
                        @Html.ValidationMessageFor(m => contactModel.Telephone)
                    </div>


                    <form action="demo_form.asp">
                    <input type="file" name="pic">

                    </form>

                        @Html.ValidationMessageFor(m => contactModel.Campus)
                    </div>
                </div>                      



                <div class="col-md-12">
                    <div class="form-group">
                        @Html.TextAreaFor(m => contactModel.Message, new { @class = "form-control message", @placeholder = "Message", @rows = "8"})
                        @Html.ValidationMessageFor(m => contactModel.Message)
                    </div>

                    <a href="https://www.casetrust.org.sg/Upload/pdf/1dummy.pdf" target="_blank"  class="btn btn-green">
                        <i class="fa fa-file-pdf-o"></i>
                        Download Pdf
                    </a>

                    <button class="btn btn-green pull-right">Submit</button>
                </div>
            </form>
        @*</fieldset>*@
    }
N0xus
  • 2,674
  • 12
  • 65
  • 126

1 Answers1

0

Look at my answer here on how to pass a model with a file.

Don't forget to add this to your view:

<input type="file" name="MyFile">
Community
  • 1
  • 1
Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58