简体   繁体   English

Asp.Net 通过 MemoryStream 失败下载电子邮件附件

[英]Asp.Net download email attachment trough MemoryStream failure

On server side I have a file containing an xml serialization of a standard System.Net.Mail.MailMessage which may contain attachments.在服务器端,我有一个包含标准System.Net.Mail.MailMessage的 xml 序列化的文件,其中可能包含附件。 I designed a form to allow users to download these attachments.我设计了一个表单来允许用户下载这些附件。

Attachment download is controlled by the following code:附件下载由以下代码控制:

private byte[] ReadFully(Stream input)
{
    byte[] _buffer = new byte[16 * 1024];
    
    using (MemoryStream _ms = new MemoryStream())
    {
        int _read;
        
        while ((_read = input.Read(_buffer, 0, _buffer.Length)) > 0)
        {
            _ms.Write(_buffer, 0, _read);
        }

        return _ms.ToArray();
    }
}


protected void bntViewAttachment_Click(object sender, EventArgs e)
{
    string _filename = Request.QueryString["file"];

    if (!string.IsNullOrWhiteSpace(_filename))
    {
        MailMessage _message = MailMessageSerializer.Create(Path.Combine(Path.GetFullPath(Path.Combine(Server.MapPath("~"), OUTBOUND_FOLDER)), _filename));

        try
        {

            int _selected = lbAttachments.SelectedIndex;

            if (_selected != -1)
            {
                Attachment _attachment = _message.Attachments[_selected];

                byte[] _data = ReadFully(_attachment.ContentStream);

                if (_data.Length > 0)
                {
                    Response.Clear();
                    Response.ContentType = MimeMapping.GetMimeMapping(_attachment.Name);
                    Response.AddHeader("Content-Disposition", string.Format("attachment;  filename={0}", _attachment.Name));
                    Response.BinaryWrite(_data);
                    Response.End();
                }
                else 
                {
                    lblStatus.Text = "Attachment empty";
                }
            }
        }
        catch (Exception ex)
        {
            lblStatus.Text = "Exception : " + ex.Message;
        }
    }
}

The email message file is provided by Request.QueryString["file"] of course this's not the safest way to operate but right now I have a bigger problem since the download doesn't work at all.. since _data.Length == 0 all the time.电子邮件消息文件由Request.QueryString["file"] ,当然这不是最安全的操作方式,但现在我有一个更大的问题,因为下载根本不起作用..因为_data.Length == 0每时每刻。 lbAttachments is the ListBox showing on the form attachments found inside mail message. lbAttachments是显示在邮件消息中的表单附件上的ListBox

I can attach an example message xml with a single attachment file here .我可以在此处附加带有单个附件文件的示例消息 xml。

I'm pretty sure about almost any line of code since I used them in many other cases but for some reason it does not here.我对几乎所有代码行都非常确定,因为我在许多其他情况下使用过它们,但由于某种原因,这里没有。

Where's the error?错误在哪里?

For some reason the position on the ContentStream must be reset on to its origin.出于某种原因,必须将ContentStream上的位置重置为其原点。

Adding添加

input.Seek(0, SeekOrigin.Begin);

on top of在之上

private byte[] ReadFully(Stream input)

Will fix it.会修好。

Now the problem is probably the encoding of the stream wrote on the client.现在的问题可能是在客户端上写入的流的编码。 At a first glance looks like the exact attachment data that's contained inside the original mail xml file (and thus Base64 or something like that).乍一看,它看起来像是包含在原始邮件 xml 文件(以及Base64或类似内容)中的确切附件数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM