我需要使用textarea和图片上传字段来制作表单。当有人提交时,我希望它发送电子邮件(带文字来自textarea)带附件(从输入文件上传字段)给我。
我的简单形式如下:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
@Html.TextArea("Question");
<input type="file"/>
<input type="submit" value="Send" />
</fieldset>
}
我发现PHP脚本正在做类似的事情,但我怎么能在ASP.NET MVC中做(可能是用JavaScript)?
答案 0 :(得分:28)
以下是使用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>
}
对此代码的进一步改进是将实际发送邮件外部化到实现某个界面的存储库并使用DI来削弱控制器逻辑和邮件发送逻辑之间的耦合。
请注意,您还可以在web.config中配置SMTP设置:
<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);
}
答案 1 :(得分:3)
.NET中的MailMessage类应该能够为您处理:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx
或者您正在寻找更具体的实际代码? (例如,如何阅读文件并将其添加到附件中)?
答案 2 :(得分:1)
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);
}
}
答案 3 :(得分:0)
除了Daren的回答之外,您还不必在视图中对文件上传的输入和标签进行硬编码。把它打在那里:)
@Html.TextBoxFor(model => model.Attachment, new { type = "file" })
@Html.LabelFor(model => model.Attachment)