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