USer hhhanulk
USer hhhanulk

Reputation: 11

File name is null of the mail attachment

I'm doing some code on ASP.NET MVC. I need to send an email with attachment. But the attachment name is null

here is my code. Note that I don't need to save the uploaded files inside the server.

mathod parameter :

List<HttpPostedFileBase> ScreenshotsOfIssueFiles

code:

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);
    }
}

Upvotes: 1

Views: 777

Answers (1)

DSR
DSR

Reputation: 4668

Try using

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

Then your code looks like. Edited

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);

Upvotes: 1

Related Questions