There are a lot of .txt files in it and with many names, how to send them to the mail? The code is listed below

CODE

using System.Net; using System.Net.Mail; SmtpClient client = new SmtpClient("smtp.mail.ru", 2525); // Π—Π΄Π΅ΡΡŒ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ смтп сСрвСр ΠΈ ΠΏΠΎΡ€Ρ‚, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΌΡ‹ Π±ΡƒΠ΄Π΅ΠΌ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ client.Credentials = new System.Net.NetworkCredential("login@mail.ru", "password"); // Π£ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ Π»ΠΎΠ³ΠΈΠ½ ΠΈ ΠΏΠ°Ρ€ΠΎΠ»ΡŒ для Π°Π²Ρ‚ΠΎΡ€ΠΈΠ·Π°Ρ†ΠΈΠΈ string msgFrom = "login@mail.ru"; // Π£ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ ΠΏΠΎΠ»Π΅, ΠΎΡ‚ ΠΊΠΎΠ³ΠΎ письмо string msgTo = "login@mail.ru"; // Π£ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ ΠΏΠΎΠ»Π΅, ΠΊΠΎΠΌΡƒ письмо Π±ΡƒΠ΄Π΅Ρ‚ ΠΎΡ‚ΠΏΡ€Π°Π²Π»Π΅Π½ΠΎ string msgSubject = "Письмо ΠΈΠ· c#"; // Π£ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ Ρ‚Π΅ΠΌΡƒ пиьсма string msgBody = String.Format("", ToString(), textBox1.Text); // Π’ΡƒΡ‚ ΠΌΡ‹ Ρ„ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ Ρ‚Π΅Π»ΠΎ письма MailMessage msg = new MailMessage(msgFrom, msgTo, msgSubject, msgBody); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ письмо, ΠΈΠ· всСго, Ρ‡Ρ‚ΠΎ сдСлали Π²Ρ‹ΡˆΠ΅ try { client.Send(msg); // ΠžΡ‚ΠΏΡ€Π°Π²Π»ΡΠ΅ΠΌ письмо } catch { } 

What do I need to add?

    2 answers 2

    This

     using System.Linq; using System.Net.Mime; using System.IO; .... Directory.GetFiles(folderWithFiles, "*.txt").ToList().ForEach( name => msg.Attachments.Add(new Attachment(name, MediaTypeNames.Text.Plain)) ); 

      And I would add archiving of files in ZIP (if this does not contradict the requirements of the task). Text files are very well archived. Look at this library here (standard GZip maykrosoft tools create an incorrect zip, so it’s better not to use it)

      http://dotnetzip.codeplex.com/