我可以发送图像和rar文件,但使用asp.net文件已损坏?

时间:2012-05-21 04:33:58

标签: 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,请给我一些建议

2 个答案:

答案 0 :(得分:6)

除非在幕后发生一些黑魔法,否则我认为您需要将fluuploader.FileContent换成hpf.InputStream。另外,我发现将InputStream的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);
    }

答案 1 :(得分:3)

试试这段代码。这应该工作我附加了近25MB的zip文件

public static void CreateMessageWithAttachment(string server)
    {
        // Specify the file to be attached and sent.
        // This example assumes that a file named Data.xls exists in the
        // current working directory.
        string file = "data.xls";
        // Create a message and set up the recipients.
        MailMessage message = new MailMessage(
           "jane@contoso.com",
           "ben@contoso.com",
           "Quarterly data report.",
           "See the attached spreadsheet.");

        // Create  the file attachment for this e-mail message.
        Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
        // Add time stamp information for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(file);
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
        disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
        // Add the file attachment to this e-mail message.
        message.Attachments.Add(data);

        //Send the message.
        SmtpClient client = new SmtpClient(server);
        // Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;

  try {
          client.Send(message);
        }
        catch (Exception ex) {
          Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", 
                ex.ToString() );              
        }
        // Display the values in the ContentDisposition for the attachment.
        ContentDisposition cd = data.ContentDisposition;
        Console.WriteLine("Content disposition");
        Console.WriteLine(cd.ToString());
        Console.WriteLine("File {0}", cd.FileName);
        Console.WriteLine("Size {0}", cd.Size);
        Console.WriteLine("Creation {0}", cd.CreationDate);
        Console.WriteLine("Modification {0}", cd.ModificationDate);
        Console.WriteLine("Read {0}", cd.ReadDate);
        Console.WriteLine("Inline {0}", cd.Inline);
        Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
        foreach (DictionaryEntry d in cd.Parameters)
        {
            Console.WriteLine("{0} = {1}", d.Key, d.Value);
        }
        data.Dispose();
    }

如果您收到错误,请回复