文件名为邮件附件的null

时间:2014-09-24 12:06:53

标签: c# asp.net-mvc

我在ASP.NET MVC上做了一些代码。我需要发送附件的电子邮件。但附件名称为空

这是我的代码。 请注意,我不需要将上传的文件保存在服务器中。

mathod参数:

List<HttpPostedFileBase> ScreenshotsOfIssueFiles

代码:

List<Attachment> screenshotAttachments = new List<Attachment>();
foreach (var files in ScreenshotsOfIssueFiles)
{
    if (files != null && files.ContentLength > 0)
    {
        var attachment = new Attachment(files.InputStream, MediaTypeNames.Application.Octet);
        ContentDisposition disposition = attachment.ContentDisposition;
        disposition.FileName = Path.GetFileName(files.FileName);
        disposition.DispositionType = DispositionTypeNames.Attachment;
        screenshotAttachments.Add(attachment);
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用

var attachment = new Attachment(files.InputStream, files.FileName, MediaTypeNames.Application.Octet);

然后你的代码看起来像。编辑

List<Attachment> screenshotAttachments = new List<Attachment>();
    foreach (var files in ScreenshotsOfIssueFiles)
    {
        if (files != null && files.ContentLength > 0)
        {
            var attachment = new Attachment(files.InputStream, files.FileName, MediaTypeNames.Application.Octet);
            ContentDisposition disposition = attachment.ContentDisposition;
            disposition.FileName = files.FileName;
            disposition.DispositionType = DispositionTypeNames.Attachment;
            screenshotAttachments.Add(attachment);