asp.net mvc 4来自网站的信件添加附件

时间:2013-05-21 14:35:40

标签: asp.net-mvc asp.net-mvc-4

我的网站上有反馈表,看起来像 enter image description here

我为我的表单创建了模型

 using System;
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.Linq;
 using System.Web;

 namespace CorePartners_Site2.Models
 {
     public class FeedbackForm
     {
    public string Name { get; set; } 
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Company { get; set; }
    public string AdditionalInformation { get; set; }

    [FileExtensions(Extensions = "doc,txt,pdf")]
    public HttpPostedFileBase ProjectInformation { get; set; }
     }
 }

并创建了视图

@using (Html.BeginForm("Feedback", "Home", FormMethod.Post, new { id = "feedback-form" }))
{
    @Html.TextBoxFor(model => model.Name, null, new { @class = "text-field" })                   
    @Html.TextBoxFor(model => model.Email, null, new { @class = "text-field" })                   
    @Html.TextBoxFor(model => model.Phone, null, new { @class = "text-field" })
    @Html.TextBoxFor(model => model.Company, null, new { @class = "text-field" })
    @Html.TextAreaFor(model => model.AdditionalInformation, new { cols="1", rows="1" })              
    @Html.TextBoxFor(model => model.ProjectInformation, null, new { type="file", @class="input-file" })
    <a href="#" class="link1" onclick="document.getElementById('feedback-form').submit()"><em><b>Send</b></em></a>
}

我想知道我的表单是否可以与a一起提交,而不是<input type="submit" />

我不知道如何依附信, 我试图制作

   [HttpGet]
    public ActionResult Feedback()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Feedback(FeedbackForm Model)
    {

        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.BodyEncoding = Encoding.UTF8;

        msg.From = new MailAddress(Model.Email, @Resources.Global.Feedback_Email_Title);
        msg.To.Add("tayna-anita@mail.ru");

        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));
                MailAttachment attach = new MailAttachment(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);
    }

但它在msg.Attachments.Add(attach);中显示错误,似乎无效。

1 个答案:

答案 0 :(得分:1)

回答你的第一个问题是的,你可以使用锚标记来代替提交输入。

你想要用这样的javascript / jquery禁用标签默认行为,然后让它提交表单:

$(function () {
    $('a.something').on("click", function (e) {
        e.preventDefault();
        $('feedback-form').submit();
    });
});

您收到的错误是因为您使用的错误对象类型为msg.attachments.add()方法。您需要使用Attachment对象而不是MailAttachment对象。

这样的事情:

Stream attachmentStream = File.OpenRead (file);
streams.Add (attachmentStream);
mail.Attachments.Add (new Attachment (attachmentStream, Path.GetFileName (file));