Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email 收集txt文件并通过电子邮件发送_Email_C# 4.0_Email Attachments - Fatal编程技术网

Email 收集txt文件并通过电子邮件发送

Email 收集txt文件并通过电子邮件发送,email,c#-4.0,email-attachments,Email,C# 4.0,Email Attachments,嗨,伙计们,我还在学习c#我需要以下方面的帮助我试图搜索目录并收集txt文件,然后将它们作为附件发送给电子邮件。 { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("******

嗨,伙计们,我还在学习c#我需要以下方面的帮助我试图搜索目录并收集txt文件,然后将它们作为附件发送给电子邮件。 {

                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                    mail.From = new MailAddress("******");
                    mail.To.Add("********");
                    mail.Subject = "Test Mail - 1";
                    mail.Body = "mail with attachment";


                    string folder = @"C:\files";
                    string[] txtfiles = Directory.GetFiles(folder, "*.txt");


                    *foreach( txtfiles in folder )*  
                      // this where my problem lies im trying to loop through directory                   //files and then add as attachment

                    {

                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment(folder);
                    mail.Attachments.Add(attachment);

                    SmtpServer.Port = 587;
                    SmtpServer.Credentials = new System.Net.NetworkCredential("*****", "******");
                    SmtpServer.EnableSsl = true;

                    SmtpServer.Send(mail);


                    }


                    }

                  }

您必须在txtfiles上循环而不是变量文件夹

                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                    mail.From = new MailAddress("******");
                    mail.To.Add("********");
                    mail.Subject = "Test Mail - 1";
                    mail.Body = "mail with attachment";


                    string folder = @"C:\files";
                    string[] txtfiles = Directory.GetFiles(folder, "*.txt");


                    *foreach( txtfiles in folder )*  
                      // this where my problem lies im trying to loop through directory                   //files and then add as attachment

                    {

                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment(folder);
                    mail.Attachments.Add(attachment);

                    SmtpServer.Port = 587;
                    SmtpServer.Credentials = new System.Net.NetworkCredential("*****", "******");
                    SmtpServer.EnableSsl = true;

                    SmtpServer.Send(mail);


                    }


                    }

                  }
    foreach (var txtfile in txtfiles)
    {
        mail.Attachments.Add(new System.Net.Mail.Attachment(txtfile);
    }