Skip to content

Zip Email attachment - not working - no documentation #34

@moisoiu

Description

@moisoiu

Hello,

Description:

We are trying to attach a zip file to the attachment from MemoryStream but for some reason in the e-mail it shows no_file_name_provided.
Version: 2.1.3
Environment : Production
Reproducing : Attach an zip file via MemoryStream or with System.Net.Mail.Attachment class and it won't add the type and the name.

Good to know:
If I download the "no_file_name_provided" and add an extension .zip I can access the resource.

I'm guessing it might be because of the Stream which is to expect System.Io.Stream and maybe it should expect

Example of code

using var attachmentsStream = new MemoryStream(attachment);
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Zip);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(attachmentsStream, ct);
attach.ContentDisposition!.FileName = "file.zip";

infoBipClient.SendMailAsync(
            new MailPropertiesModel
            {
                Receiver = model.Receiver,
                Subject = model.Subject,
                TemplateId = model.TemplateId
            },
            GetPlaceholdersSerialized(model),
            attach.ContentStream);

I've tried even more excentric approach, but still no success.

        var httpResponse = new HttpResponseMessage(HttpStatusCode.OK);
        httpResponse.Content = new StreamContent(attachmentsStream);
        httpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
        httpResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = "download.zip"
        };

        var isSent = await _infoBipClient.SendMailAsync(
            new MailPropertiesModel
            {
                Receiver = model.Receiver,
                Subject = model.Subject,
                TemplateId = model.TemplateId
            },
            GetPlaceholdersSerialized(model),
            await httpResponse.Content.ReadAsStreamAsync());


Could you please help us out what we are missing ?

Later edit:

After digging a bit more in the Source Code I saw this code

private HttpContent PrepareMultipartFormDataContent(RequestOptions options)
        {
            string boundary = "---------" + Guid.NewGuid().ToString().ToUpperInvariant();
            var multipartContent = new MultipartFormDataContent(boundary);
            foreach (var formParameter in options.FormParameters)
                multipartContent.Add(new StringContent(formParameter.Value), formParameter.Key);

            if (options.FileParameters != null && options.FileParameters.Count > 0)
                foreach (var fileParam in options.FileParameters)
                {
                    var fileStreamName = fileParam.Value is FileStream fileStream
                        ? Path.GetFileName(fileStream.Name)
                        : null;
                    var content = new StreamContent(fileParam.Value);
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    multipartContent.Add(content, fileParam.Key,
                        fileStreamName ?? "no_file_name_provided");
                }

            return multipartContent;
        }

Can there be an extension method for StreamContent ? This would solve all the cases and give the developers flexibility regarding what they want to sent, because I think there are developers that are using In Memory file generation instead of saving on the server.

Thanks,
Mircea

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions