0
我在我的网站上有反馈表格,我的表格中有<input type="file">
,所以有时需要将附件添加到电子邮件中。
我在表单创建<input type="file">
将附件添加到电子邮件asp.net mvc 4
@Html.TextBoxFor(model => model.ProjectInformation, null, new { type = "file", @class = "input-file" })
然后在我的控制器创建电子邮件,尝试添加附件
[HttpPost]
public ActionResult Feedback(FeedbackForm Model)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.BodyEncoding = Encoding.UTF8;
msg.From = new MailAddress("[email protected]", @Resources.Global.Feedback_Email_Title);
msg.To.Add("[email protected]");
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));
Attachment attach = new Attachment(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);
}
,但我想我需要发送后删除附件,我尝试添加代码这里
try
{
client.Send(msg);
if (attach1 != null)
File.Delete(Server.MapPath(attach1));
}
,但我得到了一些错误
和
我应该怎么做才能解决这个问题?
谢谢,它帮助,但第二个错误http://i.stack.imgur.com/aDFxO.png不是固定的。 – Heidel
@ Heidel无效的命名空间,你应该使用'System.IO.File' – webdeveloper
我使用'使用System.IO;'但它没有帮助,同样的错误。 – Heidel