PowerShell - Send-MailMessage - 将邮件导出到文件

时间:2014-08-11 10:44:26

标签: email powershell

我使用Send-MailMessgae Cmdlet每季度发送包含内容的自动消息。 实际的电子邮件按预期工作,但我想将此电子邮件的副本保存到网络共享。

我尝试使用Tee-Object将其放在变量中然后输出文件以保存它但这不起作用。

我想我能够做到这一点的唯一方法就是创建一个Here String,其中包含输出的详细信息。

理想情况下,我希望它是.eml格式,因为我们正在通知用户该电子邮件所针对的项目。

我希望这是有道理的。

亲切的问候,

Nigel Tatschner

2 个答案:

答案 0 :(得分:0)

您实际上可以使用Tee-Object将数据直接发送到文件,而不是尝试同时使用Out-File和Tee-Object。

$logfile = 'C:\data\logfiles'

<data> | Tee-Object $logfile -Append

答案 1 :(得分:0)

您可以使用System.Net.Mail.MailMessage

    # Based on MailMessageExt
# By Allan Eagle | 11 Jan 2009
# http://www.codeproject.com/KB/IP/smtpclientext.aspx

######################################
function createMailMessage([string]$from, [string]$to, [string]$subject) {
    [System.Net.Mail.MailMessage]$msg = New-Object -TypeName "System.Net.Mail.MailMessage" -ArgumentList $from, $to
    [System.Text.Encoding]$enc = [System.Text.Encoding]::UTF8
    $msg.SubjectEncoding = $enc
    $msg.Subject = $subject
    $msg.BodyEncoding = $enc
    return $msg
}
######################################
function mailAddAttachment([System.Net.Mail.MailMessage]$msg, [string]$filePath) {
    $fileExists = Test-Path -path $filePath -pathtype leaf
    if ($fileExists) {
        $fileName = Split-Path $filePath -Leaf
        [System.Net.Mail.Attachment]$att = New-Object -TypeName "System.Net.Mail.Attachment" -ArgumentList $filePath
        $att.ContentType.MediaType = [System.Net.Mime.MediaTypeNames+Application]::Octet
        $att.ContentType.Name = $fileName
        $att.ContentDisposition.FileName = $fileName 
        $att.ContentDisposition.DispositionType = [System.Net.Mime.DispositionTypeNames]::Attachment
        $att.ContentDisposition.Inline = $false
        $att.TransferEncoding = [System.Net.Mime.TransferEncoding]::Base64
        $msg.Attachments.Add($att)
        $att = $null
        return $true
    } else {
        $txt = "File ""$filePath"" not found"
        Write-Output $txt
        return $false
    }
}
######################################
function saveMailMessage([System.Net.Mail.MailMessage]$msg, [string]$filePath) {
    $binding = [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic
    $mailMessageType = $msg.GetType()
    $emlFilePath = $filePath
    [System.Type]$scType = [System.Type]::GetType("System.Net.Mail.SmtpClient")
    [System.Net.Mail.SmtpClient]$smtpClient = New-Object -TypeName "System.Net.Mail.SmtpClient"
    $scType = $smtpClient.GetType()
    [System.Type]$booleanType = [System.Type]::GetType("System.Boolean")
    [System.Reflection.Assembly]$assembly = $scType.Assembly
    [System.Type]$mailWriterType = $assembly.GetType("System.Net.Mail.MailWriter")
    [System.IO.FileStream]$fileStream = New-Object -TypeName "System.IO.FileStream" -ArgumentList ($emlFilePath,[System.IO.FileMode]::Create)

    [System.Array]$typeArray = ([System.Type]::GetType("System.IO.Stream"))
    [System.Reflection.ConstructorInfo]$mailWriterConstructor = $mailWriterType.GetConstructor($binding ,$null, $typeArray, $null)
    [System.Array]$paramArray = ($fileStream)
    $mailWriter = $mailWriterConstructor.Invoke($paramArray)
    #get MailMessage.Send(MailWriter,Boolean,Boolean)
    $doubleBool = $true
    [System.Array]$typeArray = ($mailWriter.GetType(),$booleanType,$booleanType)
    [System.Reflection.MethodInfo]$sendMethod = $mailMessageType.GetMethod("Send", $binding, $null, $typeArray, $null)
    if ($null -eq $sendMethod) {
        $doubleBool = $false
        [System.Array]$typeArray = ($mailWriter.GetType(),$booleanType)
        [System.Reflection.MethodInfo]$sendMethod = $mailMessageType.GetMethod("Send", $binding, $null, $typeArray, $null)
    }
    #get MailWriter.Close()
    [System.Array]$typeArray = @()
    [System.Reflection.MethodInfo]$closeMethod = $mailWriterType.GetMethod("Close", $binding, $null, $typeArray, $null)
    #execute MailMessage.Send(MailWriter,Boolean,Boolean)
    [System.Array]$sendParams = ($mailWriter,$true)
    if ($doubleBool) {
        [System.Array]$sendParams = ($mailWriter,$true,$true)
    }
    $sendMethod.Invoke($msg,$binding,$null,$sendParams,$null)
    #execute MailWriter.Close()
    [System.Array]$closeParams = @()
    $closeMethod.Invoke($mailWriter,$binding,$null,$closeParams,$null)
}
######################################
function Get-ScriptDirectory {
    Split-Path -Parent $PSCommandPath
}
######################################

$smtpServer = "smtp.here.com"
$smtpPort = 25

######################################

#$fileDir = Convert-Path "."
$fileDir = Get-ScriptDirectory
$fileDir

[System.Text.Encoding]$enc = [System.Text.Encoding]::UTF8

$dt = Get-Date
$dtStart = Get-Date $dt -Hour 0 -Minute 0 -Second 0
$baseName = $fileDir+"\report_"+$dtStart.toString("yyyy-MM-dd") 
$repName = $baseName+".csv"
$emlName = $baseName+".eml"

[System.Collections.ArrayList]$rep = @()
$rep.Add("This is a report")
$rep.Add("----------------")
for ($i=0; $i -lt 100; $i++) { 
    $index = $rep.Add("Line $i")
}
Set-Content $repName $rep

$from = "Sender <Me@here.com>"
$to = "Recipient <Them@there.com>"
$subject = "This is a report generated here on "+$dtStart.toString("yyyy-MM-dd")
$body = "$subject`n`n$repName"

[System.Net.Mail.MailMessage]$msg = createMailMessage $from $to $subject
$msg.IsBodyHtml = $false
$msg.Body = $body
mailAddAttachment $msg $repName
$emlName

[System.Net.Mail.SmtpClient]$smtp = New-Object -TypeName "System.Net.Mail.SmtpClient" -ArgumentList $smtpServer, $smtpPort
# $smtp.Send($msg)

saveMailMessage $msg $emlName
$msg.Dispose()

Remove-Item $repName

Pause
相关问题