排序,自动将附件添加到电子邮件,以及转发电子邮件

时间:2018-04-23 19:18:28

标签: powershell email email-attachments

我希望做的是一旦新电子邮件点击包含特定主题的文件夹。然后,该电子邮件将转发到另一个收件箱,并自动附加特定文件。到目前为止我能够拼凑的代码就是这个。 .Net方法是实现我的目标的适当方法,还是将Send-MailMessge用于hastable是更好的方法? 我是PowerShell代码的新手我能够让两种方法都能运行。但是想知道A.哪种方法更受欢迎。 B.有更好/更有效的方法吗?

#####Define Variables########
$fromaddress = "donotreply@fromemail.com"
$toaddress = "blah@toemail.com"
$bccaddress = "blah@bcc.com"
$CCaddress = "blah@cc.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = "C:\sendemail\test.txt"
$smtpserver = "smtp.labtest.com"

##############################
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)


(Example of the hashtable method 

$emailHashSplat = @{ To = $toAddress 
From = $fromAddress 
Subject =  $emailSubject 
Body = $emailBody SMTPServer = $smtpServer BodyAsHtml = 
$true Attachments = "C:\sendemail\test.txt" # Attachments =)

1 个答案:

答案 0 :(得分:0)

尽可能坚持使用Powershell命令,在某些情况下.NET可能会更快,但最好只在可能的情况下使用Powershell命令。

另外,请确保您的代码易于阅读和理解,使用带有splatting帮助的hastables,请参阅以下示例:Github Gist Link (Click Me!)

在链接失败的情况下复制代码。

### Script Global Settings
#Declare SMTP Connection Settings
$SMTPConnection = @{
    #Use Office365, Gmail, Other or OnPremise SMTP Relay FQDN
    SmtpServer = 'outlook.office365.com'

    #OnPrem SMTP Relay usually uses port 25 without SSL
    #Other Public SMTP Relays usually use SSL with a specific port such as 587 or 443
    Port = 587 
    UseSsl = $true    

    #Option A: Query for Credential at run time.
    Credential = Get-Credential -Message 'Enter SMTP Login' -UserName "emailaddress@domain.tld"

    <#
    #Option B: Hardcoded Credential based on a SecureString
    Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList @( 

        #The SMTP User Emailaddress
        "emailaddress@domain.tld"

        #The Password as SecureString encoded by the user that wil run this script!
        #To create a SecureString Use the folowing Command: Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString
        "Enter the SecureString here as a single line" | ConvertTo-SecureString
    ) 
    #> 
}

### Script Variables
#Declare Mailmessages.
$MailMessageA = @{
    From = "emailaddress@domain.tld"
    To = @(
        "emailaddress@domain.tld"
    )
    #Cc = @(
    #    "emailaddress@domain.tld"
    #)
    #Bcc = @(
    #    "emailaddress@domain.tld"
    #)

    Subject = 'Mailmessage from script'
    #Priority = 'Normal' #Normal by default, options: High, Low, Normal
    #Attachments = @(
        #'FilePath'    
    #)
    #InlineAttachments = @{
        #'CIDA'='FilePath'
    #} #For more information about inline attachments in mailmessages see: https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d

    BodyAsHtml = $true    
    Body = "Something Unexpected Occured as no Content has been Provided for this Mail Message!" #Default Message
}

### Script Start

#Retrieve Powershell Version Information and store it as HTML with Special CSS Class
$PSVersionTable_HTLM = ($PSVersionTable.Values | ConvertTo-Html -Fragment) -replace '<table>', '<table class="table">'

#Retrieve CSS Stylesheet
$CSS = Invoke-WebRequest "https://raw.githubusercontent.com/advancedrei/BootstrapForEmail/master/Stylesheet/bootstrap-email.min.css" | Select-Object -ExpandProperty Content

#Build HTML Mail Message and Apply it to the MailMessage HashTable
$MailMessageA.Body = ConvertTo-Html -Title $MailMessageA.Subject -Head "<style>$($CSS)</style>" -Body "
    <p>
        Hello World,    
    </p>

    <p>
        If your recieved this message then this script works.</br>
        </br>
        <div class='alert alert-info' role='alert'>
            Powershell version
        </div>
        $($PSVersionTable_HTLM)
    </P>
" | Out-String


#Send MailMessage
#This example uses the HashTable's with a technique called Splatting to match/bind the Key's in the HashTable with the Parameters of the command.
#Use the @ Symbol instead of $ to invoke Splatting, Splatting improves readability and allows for better management and reuse of variables
Send-MailMessage @SMTPConnection @MailMessageA