Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one asp.net page for sending files as an attachment.
I am selecting multiple files using file upload control and saving it in one folder and from that folder i m sending that files as an attachment with a mail.
This is working fine on google chrome and IE but on "Safari" browser files are getting copied with "0" byte size. If we are selected 1 single file as an attachment then its working fine in all the browsers.

Thanks in advance.

Code of Send Button

C#
using (MailMessage mailMessage = new MailMessage())
                {
                    mailMessage.From = new MailAddress("myname@domain.com");
                    mailMessage.Subject = txtSubject.Text.Trim();
                    mailMessage.Body = txtMessage.Text.Trim();
                    mailMessage.IsBodyHtml = true;
                    mailMessage.To.Add(new MailAddress(txtTo.Text.Trim()));


                    // Copy all seleted files to "EmailFiles" folder for sending as an attachment to mail.

                    if (FileUpload1.HasFile || FileUpload1.FileName !="")
                    {
                        foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
                        {
                            uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/EmailFiles/"), uploadedFile.FileName));                        
                        }
                    }
                    // Encrypt files

                    string encryptFilePath = Request.PhysicalApplicationPath + "EmailFiles\\";
                    string[] S2 = Directory.GetFiles(encryptFilePath);
                    foreach (string fileName in S2)
                    {
                        encryptFiles(fileName);
                    }
                    
                    // looping through all the files to send files as an attachment.
                    string UplodedfilesForEmail = Request.PhysicalApplicationPath + "EmailFiles\\";
                    string[] S1 = Directory.GetFiles(UplodedfilesForEmail);
                    foreach (string fileName in S1)
                    {
                        mailMessage.Attachments.Add(new Attachment(fileName));
                    }
                                        
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "mail.domain.com";
                    smtp.EnableSsl = true;
                    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential("emailID", "<password>");
                    NetworkCred.UserName = mailMessage.From.Address;
                    NetworkCred.Password = "<password>";
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                    { return true; };
                    smtp.Port = 25;
                    smtp.Send(mailMessage);
                }
Posted
Updated 17-Jun-15 4:17am
v3
Comments
F-ES Sitecore 17-Jun-15 10:25am    
What client-side component\html are you using to upload the files?
Nitin_Bharekar 18-Jun-15 1:00am    
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
Richard Deeming 17-Jun-15 11:04am    
You do realise that every email will have every single file that anyone has ever uploaded to this page attached to it?
Nitin_Bharekar 18-Jun-15 3:33am    
Hi Richard.
Sorry I m not getting you.
Richard Deeming 18-Jun-15 8:59am    
Every time someone uploads a file, you save it in the EmailFiles folder.

You then attach every file from the EmailFiles folder to the message.

Your code never deletes the uploaded files from that folder, so the next message will include the files from all previous messages as well.

Instead of copying the files to a physical location on your server before attaching to the email content, do it directly through memory stream.

Check the codeproject article to attach an uploaded file from memory stream to email attachment.

Creating In-Memory Mail Attachments[^]
 
Share this answer
 
Comments
Nitin_Bharekar 18-Jun-15 4:25am    
Hi Sreekanth,

Thank you for your valuable suggestion.
I check and tried same code of piece in my application but it didn't succeeded.
In case of Safari browser when i select multiple files as an attachment then content file size is showed as 0.
Sreekanth Mothukuru 18-Jun-15 4:33am    
Does the code work without the path being encrypted?
Nitin_Bharekar 18-Jun-15 5:54am    
No.

uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/EmailFiles/"), uploadedFile.FileName));

after executing above statement new file is saved with 0 bytes.
It seems to be like a safari browser issue mentioned at StackOverflow
http://stackoverflow.com/questions/7231054/file-input-size-issue-in-safari-for-multiple-file-selection[^]

Check the fiddler link on all browsers and see the "Size" attribute
http://jsfiddle.net/rHd26/6/[^]
 
Share this answer
 
Comments
Nitin_Bharekar 18-Jun-15 8:44am    
On 5.0.5 version of Safari Browser its working fine.
Latest version is having bug for multiple file upload.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900