0

大家好,我还在学习 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);


                    }


                    }

                  }
4

1 回答 1

0

您必须在 txtfiles 上循环而不是变量文件夹。它应该看起来像这样

    foreach (var txtfile in txtfiles)
    {
        mail.Attachments.Add(new System.Net.Mail.Attachment(txtfile);
    }
于 2013-10-06T08:21:34.533 回答