获取电子邮件附件的文件上传umbraco

时间:2015-12-22 19:15:03

标签: attachment email-attachments umbraco7

我正在创建一个包含一个表单的简单页面。它的代码如下:

@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的模板中。

由于

蒂帕

1 个答案:

答案 0 :(得分:0)

您的表单需要是'multipart / form-data'

<form enctype="multipart/form-data" method="post">
    <input type="file" name="fluld" id="fluld" />
    <input type="submit" value="Sub"/>
</form>