我正在创建一个只有一个表单的简单页面。它的代码如下:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Net;
@using System.Net.Mail;
@{
if(IsPost)
{
//Way 1: to get attachment
var fileSavePath = "";
var uploadedFile = Request.Files[0];//Here not getting file name
var fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/media/" + fileName);
uploadedFile.SaveAs(fileSavePath);
FileInfo info = new FileInfo(fileSavePath);
string[] ext = fileName.Split('.');
//Way 2 :
var a = Request["fluld"];//Getting file name only
var b = Request.Files;//Getting null here
string d = Path.GetFullPath(Request["fluld"]);
string c = string.Empty;
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("xyz@gmail.com");
mail.To.Add("xyz@gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("filepath");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "******");
SmtpServer.EnableSsl = true;
//SmtpServer.Send(mail);
//MessageBox.Show("mail Send");
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
}
<form method="post">
<input type="file" name="fluld" id="fluld" />
<input type="submit" value="Sub"/>
</form>
我无法获取此Request.Files的电子邮件附件文件。请帮助我。有什么需要补充的吗?此代码在umbraco的模板中。
感谢
Dipa
您的表单需要是"多部分/表单数据"
<form enctype="multipart/form-data" method="post">
<input type="file" name="fluld" id="fluld" />
<input type="submit" value="Sub"/>
</form>