Asp.net mvc 3 附有邮政MVC don';行不通
我有一个Asp.net mvc 3 附有邮政MVC don';行不通,asp.net-mvc-3,email,attachment,Asp.net Mvc 3,Email,Attachment,我有一个MVC3项目。在我的联系页面中,我想添加一个附件到客户发送的电子邮件的可能性 我正在使用邮政。它工作得很好,但我无法让附件工作 以下是我的代码中处理此问题的部分: 我有一个模型类: public class ContactEmail { [ScaffoldColumn(false)] public int id { get; set; } [Required(ErrorMessage = "Name is required!")] [DataType(D
MVC3项目
。在我的联系页面中,我想添加一个附件到客户发送的电子邮件的可能性
我正在使用邮政
。它工作得很好,但我无法让附件工作
以下是我的代码中处理此问题的部分:
我有一个模型类
:
public class ContactEmail
{
[ScaffoldColumn(false)]
public int id { get; set; }
[Required(ErrorMessage = "Name is required!")]
[DataType(DataType.Text)]
[DisplayName("Name")]
public string name { get; set; }
[Required(ErrorMessage = "Email Address is required!")]
[DataType(DataType.EmailAddress)]
[DisplayName("Email Address")]
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "Email Address is not valid.")]
public string email { get; set; }
[Required(ErrorMessage = "Country is required!")]
[DisplayName("Country")]
public string country { get; set; }
[DisplayName("Subject")]
public string subject { get; set; }
[Required(ErrorMessage = "Message is required!")]
[DisplayName("Message")]
[StringLength(1500)]
public string message { get; set; }
[ScaffoldColumn(false)]
public DateTime datetime { get; set; }
public HttpPostedFileBase attachment { get; set; }
这是我的邮政管理员
:
public class PostalController : Controller
{
[HttpPost]
public ActionResult Send(ContactEmail model)
{
if (ModelState.IsValid)
{
Random random = new Random();
var ticket = random.Next(1000000000, 2000000000);
dynamic courriel = new Email("Postal");
courriel.To = "support@coderfortraders.com";
courriel.Name = model.name;
courriel.Country = model.country;
courriel.Subject = model.subject;
courriel.From = model.email;
courriel.Message = model.message;
courriel.TicketId = ticket;
courriel.Attachment = model.attachment;
//if (model.Attachment != null && model.Attachment.ContentLength > 0)
//{
// var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName);
// courriel.Attachments.Add(attachment);
//}
//courriel.Attachment = model.Attachment;
courriel.Attach(new Attachment(model.attachment.InputStream, model.attachment.FileName));
Task task = courriel.SendAsync();
//courriel.Send();
return RedirectToAction("Sent", "Contact");
}
ViewData["MessageSent"] = false;
return View("~/Views/Contact/Contact.cshtml");
}
最后是我在Contact.cshtml视图中使用的表单
:
@using (Html.BeginForm("Send", "Postal"))
{
@Html.ValidationSummary(false)
<p>
@Html.LabelFor(model => model.name)
@Html.TextBoxFor(model => model.name, new { @size = 35 })
@Html.ValidationMessageFor(model => model.name, "*")
</p>
<p>
@Html.LabelFor(model => model.email)
@Html.TextBoxFor(model => model.email, new { @size = 35 })
@Html.ValidationMessageFor(model => model.email, "*")
</p>
<p>
@Html.LabelFor(model => model.subject)
@Html.DropDownList(
"subject",
new[] {
new SelectListItem {
Text = "NT Indicator Quote",
Value = "NT Indicator Quote"
},
new SelectListItem {
Text = "NT Strategy Quote",
Value = "NT Strategy Quote"
},
new SelectListItem {
Text = "Comments",
Value = "Comments"
},
new SelectListItem {
Text = "other",
Value = "other"
}
}
); // @Html.DropDownList(
@Html.ValidationMessageFor(model => model.subject, "*")
</p>
<p>
@Html.LabelFor(model => model.message)
@Html.TextAreaFor(model => model.message, new { @cols = 50, @rows = 10 })
@Html.ValidationMessageFor(model => model.message, "*")
</p>
<p>
@Html.LabelFor(model => model.attachment)
@Html.TextBoxFor(model => model.attachment, new { type = "file" })
@Html.ValidationMessageFor(model => model.attachment)
</p>
<p><input type="submit" id="submit" name="submit" value="Submit" /></p>
} <!-- End form -->
@使用(Html.BeginForm(“发送”、“邮寄”))
{
@Html.ValidationSummary(false)
@LabelFor(model=>model.name)
@TextBoxFor(model=>model.name,新的{@size=35})
@Html.ValidationMessageFor(model=>model.name,“*”)
@LabelFor(model=>model.email)
@Html.TextBoxFor(model=>model.email,新的{@size=35})
@Html.ValidationMessageFor(model=>model.email,“*”)
@LabelFor(model=>model.subject)
@Html.DropDownList(
“主题”,
新[]{
新建SelectListItem{
Text=“NT指标报价”,
Value=“NT指标报价”
},
新建SelectListItem{
Text=“NT策略报价”,
Value=“NT策略报价”
},
新建SelectListItem{
Text=“Comments”,
Value=“注释”
},
新建SelectListItem{
Text=“其他”,
Value=“其他”
}
}
);//@Html.DropDownList(
@Html.ValidationMessageFor(model=>model.subject,“*”)
@LabelFor(model=>model.message)
@TextAreaFor(model=>model.message,new{@cols=50,@rows=10})
@Html.ValidationMessageFor(model=>model.message,“*”)
@LabelFor(model=>model.attachment)
@Html.TextBoxFor(model=>model.attachment,新的{type=“file”})
@Html.ValidationMessageFor(model=>model.attachment)
}
我收到以下错误消息:
对象引用未设置为对象的实例。
描述:在执行当前web请求期间发生未经处理的异常。请查看堆栈跟踪,以了解有关错误及其在代码中起源的详细信息。异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
源错误: 第44行:courriel.Attach(新附件(model.Attachment.InputStream,model.Attachment.FileName)) 你能帮我找出这里的问题吗? 谢谢大家! 以下是来自视图源的HTML:
<form action="/Postal/Send?enctype=multipart%2Fform-data" method="post">
<div class="validation-summary-valid" data-valmsg-summary="true">
<ul>
<li style="display:none"></li>
</ul>
</div>
<p>
<label for="name">Name</label>
<input data-val="true" data-val-required="Name is required!" id="name" name="name" size="35" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="name" data-valmsg-replace="false">*</span>
</p>
<p>
<label for="email">Email Address</label>
<input data-val="true" data-val-regex="Email Address is not valid." data-val-regex-pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}" data-val-required="Email Address is required!" id="email" name="email" size="35" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="email" data-valmsg-replace="false">*</span>
</p>
<p>
<label for="subject">Subject</label>
<select id="subject" name="subject">
<option value="NT Indicator Quote">NT Indicator Quote</option>
<option value="NT Strategy Quote">NT Strategy Quote</option>
<option value="Comments">Comments</option>
<option value="other">other</option>
</select>
<span class="field-validation-valid" data-valmsg-for="subject" data-valmsg-replace="false">*</span>
</p>
<p>
<label for="message">Message</label>
<textarea cols="50" data-val="true" data-val-length="The field Message must be a string with a maximum length of 1500." data-val-length-max="1500" data-val-required="Message is required!" id="message" name="message" rows="10">
</textarea>
<span class="field-validation-valid" data-valmsg-for="message" data-valmsg-replace="false">*</span>
</p>
<p>
<label for="attachment">attachment</label>
<input id="attachment" name="attachment" type="file" value="" />
<span class="field-validation-valid" data-valmsg-for="attachment" data-valmsg-replace="true"></span>
</p>
<p>
<input type="submit" id="submit" name="submit" value="Submit" />
</p>
</form> <!-- End form -->
</div>
名称
*
电子邮件地址
*
主题
新界指标报价
新界策略报价
评论
其他
*
消息
*
附件
如果要上载文件,需要将enctype=“multipart/form data”
属性附加到表单:
@using (Html.BeginForm("Send", "Postal", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
...
}
也可以通过Haacked签出它。不幸的是,即使我将新的{enctype=“multipart/form data”}添加到form@user508945,当你浏览网页的源代码时,
标记生成的HTML是什么样子的?Thnak你非常喜欢Darin。我添加了FormMethod.Post,它神奇地工作了。