Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
i am having issues sending multiple files as an attachment to email address, the sending is successful, but the file attachments are 0Kb!

below is my code:

C#
foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
            {
                if (postedFile != null && postedFile.ContentLength > 0)
                {
                    string filename = Path.GetFileName(postedFile.FileName);
                    string contentType = postedFile.ContentType;
                    int size = FileUpload1.PostedFile.ContentLength;
                    using (Stream fs = postedFile.InputStream)
                    {
                        using (var br = new BinaryReader(fs))
                        {
                            byte[] bytes = br.ReadBytes((Int32) fs.Length);
                            DataAccessLayer.SaveFile(filename, contentType, transNumber, size, bytes);
                            Sendmail(email, filename);
                        }
                    }
                }
               
            }



Email code:

C#
public void Sendmail(string mailTo, string subject)
    {
        if(FileUpload1.HasFiles)
        {

            try
            {
                var mail = new MailMessage();
                var smtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("removed", "Interview study material");
                mail.To.Add(mailTo);
                mail.Subject = subject;
                mail.Body = "Please find attachment document!";
                string strFileName =
                    Path.GetFileName(FileUpload1.PostedFile.FileName);
                var attachFile = new Attachment(FileUpload1.PostedFile.InputStream, strFileName);
                mail.Attachments.Add(attachFile);

                smtpServer.Port = 587;
                smtpServer.Credentials = new NetworkCredential("removed", "removed");
                smtpServer.EnableSsl = true;
                smtpServer.Send(mail);

            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
Posted
Updated 29-Aug-15 4:11am
v2
Comments
Afzaal Ahmad Zeeshan 29-Aug-15 11:11am    
Try checking the bytes being sent yourself, debug the application.
Uwakpeter 29-Aug-15 14:36pm    
yes i have debug it myself, in the foreach loop, it is only the file in the first iteration that has contentLength, subsequent iterations contentLength are all 0kb, pls any hints on how to resolve this?

1 solution

 
Share this answer
 

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



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