如何通过关键字搜索文件然后将其附加到电子邮件中?

时间:2017-04-13 12:45:31

标签: windows powershell csv email

我是否可以使用检测到并附加到电子邮件的文件名作为我的电子邮件主题行的文本?

Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -ErrorAction SilentlyContinue

$file = "C:\Users\me\myfilename_123.csv"
$mailboxdata = (Get-MailboxStatistics | select DisplayName, TotalItemSize,TotalDeletedItemSize, ItemCount, LastLoggedOnUserAccount, LastLogonTime)
$mailboxdata | Export-Csv "$file"
$smtpServer = "192.168.1.100"
$att = New-Object Net.Mail.Attachment($file)
$msg = New-Object Net.Mail.MailMessage
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg.From = "my@email.com"
$msg.To.Add("your@email.com")
$msg.To.Add("another@email.com")
$msg.Subject = "Notification from email server"
$msg.Body = "Attached is the email server mailbox report"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

##### Search directory for keyword "myfilename", attaches the file found which
##### has a filename that includes this keyword

# $file = SEARCH "C:\Users\me\"
# IF filename contains "myfilename" ATTACH as $file

##### Message subject is set to the filename of the attachment 

$msg.Subject = "("myfilename_123)"

0 个答案:

没有答案