C# 带有附件的表单上载和电子邮件发送
我需要制作带有文本区和图像上传字段的表单。当有人提交它时,我希望它向我发送电子邮件(带有文本区域中的文本)和附件(来自输入文件上载字段) 我的简单表单如下所示:C# 带有附件的表单上载和电子邮件发送,c#,forms,asp.net-mvc-3,razor,C#,Forms,Asp.net Mvc 3,Razor,我需要制作带有文本区和图像上传字段的表单。当有人提交它时,我希望它向我发送电子邮件(带有文本区域中的文本)和附件(来自输入文件上载字段) 我的简单表单如下所示: @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> @Html.TextArea("Question"); <input type="file
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
@Html.TextArea("Question");
<input type="file"/>
<input type="submit" value="Send" />
</fieldset>
}
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
@text区域(“问题”);
}
我发现PHP脚本正在做类似的事情,但是我如何在ASP.NET MVC中做到这一点(可以使用JavaScript)?在.NET中的MailMessage类应该能够为您处理这一点:
或者您正在寻找更具体的实际代码?(例如,如何读取文件并将其添加到附件)?这里有一个使用gmail SMTP的示例,但是如果您有自己的SMTP服务器,您可以轻松地修改代码 一如既往,我将从视图模型开始:
public class QuestionViewModel
{
[Required]
public string Question { get; set; }
public HttpPostedFileBase Attachment { get; set; }
}
然后控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new QuestionViewModel());
}
[HttpPost]
public ActionResult Index(QuestionViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
using (var client = new SmtpClient("smtp.gmail.com", 587))
{
client.EnableSsl = true;
client.Credentials = new NetworkCredential("someaccount@gmail.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("fromaddress@gmail.com");
mail.To.Add("toaddress@gmail.com");
mail.Subject = "Test mail";
mail.Body = model.Question;
if (model.Attachment != null && model.Attachment.ContentLength > 0)
{
var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName);
mail.Attachments.Add(attachment);
}
client.Send(mail);
}
return Content("email sent", "text/plain");
}
}
最后是一个观点:
@model QuestionViewModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<div>
@Html.LabelFor(x => x.Question)
@Html.TextAreaFor(x => x.Question)
</div>
<div>
<label for="attachment">Attachment</label>
<input type="file" name="attachment" id="attachment"/>
</div>
<input type="submit" value="Send" />
</fieldset>
}
除了Daren的答案,您不必在视图中硬编码文件上传的输入和标签。把这个打进去:)
是的,我正在寻找更具体的东西-如何从表单中读取文件并将其作为附件添加到电子邮件中,然后发送此电子邮件。对不起,太慢了,但很高兴有人给你答案:)toaddress?我不知道蟾蜍穿裙子!如果您试图使用$.ajax来传递
HttpPostedFileBase
文件,该怎么办?我试图通过一个简单的$.ajax调用来实现,但是我的控制器上有一个空文件。是我缺少的内容类型吗?@AdrianoRR,使用AJAX上传文件非常困难。为了上传文件,有些组件需要使用multipart/formdata
enctype。我不知道这个困难。是的,我已经在使用multipart/form data
,如果我使用正常的提交操作,这很好,但是如果我使用jquery处理.submit事件,就不行了。我会检查你提到的这个组件。谢谢,很酷的样品达林。帮助我前进了很多!
<system.net>
<mailSettings>
<smtp from="fromaddress@gmail.com" deliveryMethod="Network">
<network
enableSsl="true"
host="smtp.gmail.com"
port="587"
userName="someaccount@gmail.com"
password="secret"
/>
</smtp>
</mailSettings>
</system.net>
using (var client = new SmtpClient())
{
var mail = new MailMessage();
mail.To.Add("toaddress@gmail.com");
mail.Subject = "Test mail";
mail.Body = model.Question;
if (model.Attachment != null && model.Attachment.ContentLength > 0)
{
var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName);
mail.Attachments.Add(attachment);
}
client.Send(mail);
}
if (model.Attachment != null && model.Attachment.ContentLength > 0)
{
foreach (HttpPostedFileBase item in fileUploader)
{
var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName);
mail.Attachments.Add(attachment);
}
}
@Html.TextBoxFor(model => model.Attachment, new { type = "file" })
@Html.LabelFor(model => model.Attachment)