c# - 我可以发送图像和 rar 文件,但这些文件已被 asp.net 损坏?

标签 c# asp.net email email-attachments

我正在使用 asp.net 和 c#.net 发送带有大附件(最大 10mb)的邮件,这就是我可以转换文件的原因,.txt、.doc、.xls 文件可以完美发送,但图像和 rar文件损坏是什么问题请给我任何建议, 我的代码是

 DataSet ds = SqlHelper.ExecuteDataset(con, "usp_GetEmailSettings", Session["UserID"].ToString());
                message.To.Add(ds.Tables[0].Rows[0]["Email"].ToString());
                message.CC.Add(ds.Tables[1].Rows[0]["EmailID"].ToString());
                message.Subject = ds.Tables[0].Rows[0]["Email_Subject"].ToString();
                message.From = new System.Net.Mail.MailAddress(ds.Tables[1].Rows[0]["EmailID"].ToString());
                message.Body = ds.Tables[0].Rows[0]["Email_Body"].ToString() +
                                       "<br/><br/> <font size='2.0em'>Submission Number : " +filename+"<br/> DBA Name : " +txtDBAName.Text + "<br/> Insured Name : " +TxtInsured.Text + "<br/> Additional Comments : " + txtcomment.Value ;
                message.IsBodyHtml = true;
                string attachId;
                System.Net.Mail.Attachment at;
 // Get the HttpFileCollection and Attach the Multiple files
                HttpFileCollection hfc = Request.Files;
                if (hfc.Count > 0)
                {
                    for (int i = 0; i < hfc.Count; i++)
                    {
                        HttpPostedFile hpf = hfc[i];
                        if (hpf.ContentLength > 0)
                        {
                            if (i == 0)
                            {
                                string[] ext = System.IO.Path.GetFileName(hpf.FileName).Split('.');
                                attachId = filename + "." + ext[1];
                                at = new System.Net.Mail.Attachment(fluuploader.FileContent, attachId);
                            }

                            else
                            {
                                string[] ext = System.IO.Path.GetFileName(hpf.FileName).Split('.');
                                attachId = filename + "(" + i + ")" + "." + ext[1];
                                at = new System.Net.Mail.Attachment(fluuploader.FileContent, attachId);
                            }
                            at.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
                           // at.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;                       
                            message.Attachments.Add(at);
                        }

                    }
                }
 smtp.Timeout = 9999999;
                smtp.Send(message);  

web.config 我的代码是

<httpRuntime executionTimeout="240" maxRequestLength="20480"/>

at.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit; 我可以在该行中给出评论不发送大文件但一切正常但我已发送大文件最大值10mb,请给我建议

最佳答案

除非幕后发生了一些黑魔法,否则我认为您需要将 fluuploader.FileContent 换成 hpf.InputStream。此外,我发现将 In​​putStream 的 Position 设置为 0 会有所帮助。for 循环中的最终代码应如下所示:

    HttpPostedFile hpf = hfc[i];

    if (hpf.ContentLength > 0)
    {
        hpf.InputStream.Position = 0;

        if (i == 0)
        {
            string[] ext = System.IO.Path.GetFileName(hpf.FileName).Split('.');
            attachId = filename + "." + ext[1];
            at = new System.Net.Mail.Attachment(hpf.InputStream, attachId);
        }

        else
        {
            string[] ext = System.IO.Path.GetFileName(hpf.FileName).Split('.');
            attachId = filename + "(" + i + ")" + "." + ext[1];
            at = new System.Net.Mail.Attachment(hpf.InputStream, attachId);
        }

        message.Attachments.Add(at);
    }

关于c# - 我可以发送图像和 rar 文件,但这些文件已被 asp.net 损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10679532/

相关文章:

excel - 从单元格引用插入动态超链接并将其放在复制的范围之上

python - django更改电子邮件地址-确认密码

JAVA 电子邮件消息 - 剪辑引用行

c# 如何获取我的列表框项并在我按下检查列表框时快速添加它

c# - Setter 在构造函数之前调用

c# - 如何从后面的代码打开模式?

c# - 如何在 Asp.net MVC 6 中使用 NHaml(找不到模板错误)

asp.net - 在 ASP.NET 中使用嵌入式标准 HTML 表单

c# - 在 .Net 中使用 HP laserJet 10xx 进行打印

c# - 防止在 Visual Studio 中加载符号