在文件夹中发送多个附件,而不仅仅是特定文件名...通过电子邮件发送

时间:2012-10-10 15:49:45

标签: vb.net email-attachments

我已设法发送一封电子邮件,其附件位于特定位置并且具体命名(“C:\ New \ Log.txt”)

但是,我希望能够发送一封包含给定文件夹中所有附件的电子邮件,而不管它们的名称是什么。 使用my.settings在项目的其他地方配置所有变量设置,我希望文件夹目标类似,即my.settings.fileloc1文件的位置

以下是我目前的代码。我很确定它会涉及到getfiles,但我正在空着......请帮忙!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New  _
        Net.NetworkCredential(My.Settings.SMTPuser, My.Settings.SMTPuser)
        SmtpServer.Port = My.Settings.SMTPPort
        SmtpServer.Host = My.Settings.SMTPHost
        mail = New MailMessage()
        mail.From = New MailAddress(My.Settings.from)
        mail.To.Add(My.Settings.recipient)
        mail.Subject = My.Settings.subject
        mail.Body = My.Settings.body
        Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment("C:\New\Log.txt")
        '^^The above needs to be an actual file
        '^^I want it to select all files in a given folder and attach them!
        mail.Attachments.Add(Attach)
        SmtpServer.Send(mail)
        MsgBox("Mail Sent")
    Catch ex As Exception
        MsgBox("Email Settings are either incomplete or incorrect" & vbNewLine & "Please see below details:" & vbNewLine & vbNewLine & ex.ToString)
    End Try

End Sub

感谢你能提出的任何事情:)

2 个答案:

答案 0 :(得分:2)

尝试For Each循环查找S ystem.IO.Directory.GetFiles()

中的所有文件

答案 1 :(得分:0)

' ...
For Each filePath As String In Directory.GetFiles(My.Settings.FileLoc1)
    Dim Attach As New Net.Mail.Attachment(filePath)
    mail.Attachments.Add(Attach)
Next
SmtpServer.Send(mail)
' ...