Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. can't get multiple files when attachment through stream class.

can't get multiple files when attachment through stream class.

Scheduled Pinned Locked Moved ASP.NET
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pujafaldu
    wrote on last edited by
    #1

    Hi... here i have send mail with attachment file. i have used stream class for sending attachment. Here is code...... public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, string strFileName, Stream FileStream,bool bCheck) { bResult = false; MailAddress from = new MailAddress(Config.MailFrom); MailAddress to = new MailAddress(strToEmail); MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; Attachment attachFile = new Attachment(FileStream, strFileName); mail.Attachments.Add(attachFile); if (bCheck == true) { SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); bResult = true; } bResult = true; } this works fine when there is only one file to attach. but if i want to attach multiple files then it overwrite to first file with second. how can i get more then one file in FileStream? kindly help to solvethis issue. Thanks. Regards, PUJA FALDU

    M G 2 Replies Last reply
    0
    • P pujafaldu

      Hi... here i have send mail with attachment file. i have used stream class for sending attachment. Here is code...... public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, string strFileName, Stream FileStream,bool bCheck) { bResult = false; MailAddress from = new MailAddress(Config.MailFrom); MailAddress to = new MailAddress(strToEmail); MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; Attachment attachFile = new Attachment(FileStream, strFileName); mail.Attachments.Add(attachFile); if (bCheck == true) { SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); bResult = true; } bResult = true; } this works fine when there is only one file to attach. but if i want to attach multiple files then it overwrite to first file with second. how can i get more then one file in FileStream? kindly help to solvethis issue. Thanks. Regards, PUJA FALDU

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      pujafaldu wrote:

      but if i want to attach multiple files then it overwrite to first file with second.

      How are you attaching multple files? The code only seems to work as far as there is one attachment. In fact your method should look like:

      public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, List<string> PlstFileNames, List<Stream> PlstStreams)
      {
      MailAddress from = new MailAddress(Config.MailFrom);
      MailAddress to = new MailAddress(strToEmail);

      		MailMessage mail = new MailMessage(from, to);
      		mail.Subject = strSubject;
      		mail.Body = strbody;
      		mail.IsBodyHtml = true;
      		mail.Sender = from;
      		
      		for(int LiCount = 0; LiCount < PlstFileNames.Count; LiCount++)
      		{
      			Attachment attachFile = new Attachment(PlstStreams\[LiCount\], PlstFileNames\[LiCount\]);
      			mail.Attachments.Add(attachFile);
      		}
      
      		SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp);
      		SmtpMail.Send(mail);
      	}
      

      Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      P 1 Reply Last reply
      0
      • M Manas Bhardwaj

        pujafaldu wrote:

        but if i want to attach multiple files then it overwrite to first file with second.

        How are you attaching multple files? The code only seems to work as far as there is one attachment. In fact your method should look like:

        public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, List<string> PlstFileNames, List<Stream> PlstStreams)
        {
        MailAddress from = new MailAddress(Config.MailFrom);
        MailAddress to = new MailAddress(strToEmail);

        		MailMessage mail = new MailMessage(from, to);
        		mail.Subject = strSubject;
        		mail.Body = strbody;
        		mail.IsBodyHtml = true;
        		mail.Sender = from;
        		
        		for(int LiCount = 0; LiCount < PlstFileNames.Count; LiCount++)
        		{
        			Attachment attachFile = new Attachment(PlstStreams\[LiCount\], PlstFileNames\[LiCount\]);
        			mail.Attachments.Add(attachFile);
        		}
        
        		SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp);
        		SmtpMail.Send(mail);
        	}
        

        Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

        P Offline
        P Offline
        pujafaldu
        wrote on last edited by
        #3

        my question is here! List PlstStreams , how can i create group of stream for multiple files. i have code for stream in another file which pass above values. string strAttach = string.Empty; string strSendAttach = string.Empty; string strFileName = string.Empty; string strFileAttachment = string.Empty; Stream FileStream; bool bFlag = false; bool bCheck = false; HttpFileCollection hfc = Request.Files; if (hfc.Count != 0) { for (int i = 0; i < hfc.Count; i++) { if (!string.IsNullOrEmpty(hfc[i].FileName)) { FileStream = hfc[i].InputStream; strFileAttachment = hfc[i].FileName; if (objMailSend.SendMailToExternalUser(ValidetEmailId, txtMailBody.Text, lblSubject.Text, strFileAttachment, FileStream, bCheck)) { lblMessage.Visible = true; lblMessage.Text = "Mail has been sent successfully."; lblMessage.CssClass = "succLabel"; } } } } Thanks. Regards, PUJA FALDU

        M 1 Reply Last reply
        0
        • P pujafaldu

          Hi... here i have send mail with attachment file. i have used stream class for sending attachment. Here is code...... public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, string strFileName, Stream FileStream,bool bCheck) { bResult = false; MailAddress from = new MailAddress(Config.MailFrom); MailAddress to = new MailAddress(strToEmail); MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; Attachment attachFile = new Attachment(FileStream, strFileName); mail.Attachments.Add(attachFile); if (bCheck == true) { SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); bResult = true; } bResult = true; } this works fine when there is only one file to attach. but if i want to attach multiple files then it overwrite to first file with second. how can i get more then one file in FileStream? kindly help to solvethis issue. Thanks. Regards, PUJA FALDU

          G Offline
          G Offline
          Gary Stafford
          wrote on last edited by
          #4

          I realize this doesn't answer your code-specific question, but is zipping the files an option? It will provide better speed and reliably, and limit your attachments to a single file. You can use the System.IO.Compression class to compress the stream of files. Advantages of zipping files for email: Zip or unzip a file[^]

          Regards, Gary

          1 Reply Last reply
          0
          • P pujafaldu

            my question is here! List PlstStreams , how can i create group of stream for multiple files. i have code for stream in another file which pass above values. string strAttach = string.Empty; string strSendAttach = string.Empty; string strFileName = string.Empty; string strFileAttachment = string.Empty; Stream FileStream; bool bFlag = false; bool bCheck = false; HttpFileCollection hfc = Request.Files; if (hfc.Count != 0) { for (int i = 0; i < hfc.Count; i++) { if (!string.IsNullOrEmpty(hfc[i].FileName)) { FileStream = hfc[i].InputStream; strFileAttachment = hfc[i].FileName; if (objMailSend.SendMailToExternalUser(ValidetEmailId, txtMailBody.Text, lblSubject.Text, strFileAttachment, FileStream, bCheck)) { lblMessage.Visible = true; lblMessage.Text = "Mail has been sent successfully."; lblMessage.CssClass = "succLabel"; } } } } Thanks. Regards, PUJA FALDU

            M Offline
            M Offline
            Manas Bhardwaj
            wrote on last edited by
            #5

            May be you can do this:

            string strAttach = string.Empty;
            string strSendAttach = string.Empty;
            string strFileName = string.Empty;
            string strFileAttachment = string.Empty;
            Stream FileStream;
            bool bFlag = false;
            bool bCheck = false;

            HttpFileCollection hfc = Request.Files;
            objMailSend.SendMailToExternalUser(ValidetEmailId, txtMailBody.Text, lblSubject.Text, hfc);

            public void SendMailToExternalUser(string strToEmail, string strbody, string strSubject, HttpFileCollection PobjFileCollection)
            {
            MailAddress from = new MailAddress(Config.MailFrom);
            MailAddress to = new MailAddress(strToEmail);

            		MailMessage mail = new MailMessage(from, to);
            		mail.Subject = strSubject;
            		mail.Body = strbody;
            		mail.IsBodyHtml = true;
            		mail.Sender = from;
            		
            		for(int LiCount = 0; LiCount < PobjFileCollection.Count; LiCount++)
            		{
            			Attachment attachFile = new Attachment(PobjFileCollection\[LiCount\].InputStream, PobjFileCollection\[LiCount\].FileName);
            			mail.Attachments.Add(attachFile);
            		}
            
            		SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp);
            		SmtpMail.Send(mail);
            	}
            

            Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

            P 1 Reply Last reply
            0
            • M Manas Bhardwaj

              May be you can do this:

              string strAttach = string.Empty;
              string strSendAttach = string.Empty;
              string strFileName = string.Empty;
              string strFileAttachment = string.Empty;
              Stream FileStream;
              bool bFlag = false;
              bool bCheck = false;

              HttpFileCollection hfc = Request.Files;
              objMailSend.SendMailToExternalUser(ValidetEmailId, txtMailBody.Text, lblSubject.Text, hfc);

              public void SendMailToExternalUser(string strToEmail, string strbody, string strSubject, HttpFileCollection PobjFileCollection)
              {
              MailAddress from = new MailAddress(Config.MailFrom);
              MailAddress to = new MailAddress(strToEmail);

              		MailMessage mail = new MailMessage(from, to);
              		mail.Subject = strSubject;
              		mail.Body = strbody;
              		mail.IsBodyHtml = true;
              		mail.Sender = from;
              		
              		for(int LiCount = 0; LiCount < PobjFileCollection.Count; LiCount++)
              		{
              			Attachment attachFile = new Attachment(PobjFileCollection\[LiCount\].InputStream, PobjFileCollection\[LiCount\].FileName);
              			mail.Attachments.Add(attachFile);
              		}
              
              		SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp);
              		SmtpMail.Send(mail);
              	}
              

              Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

              P Offline
              P Offline
              pujafaldu
              wrote on last edited by
              #6

              Hey.... Lots of Thanks.......................... as ur suggestion ,i have made changes and it works fine.. :) Regards, PUJA FALDU

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups