我设法发送了一封带有附件的电子邮件,该附件位于特定位置并专门命名("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:NewLog.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
感谢您能想到的:)
尝试 For Each 循环,查找 System.IO.Directory.GetFiles() 中的所有文件
' ...
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)
' ...