邮件排序、自动添加附件、转发邮件

Sorting, automatically adding an attachment to an email, and forwarding an email

我希望做的是一旦一封新电子邮件到达包含特定主题的文件夹。然后该电子邮件被转发到另一个收件箱,并自动附加一个特定文件。到目前为止,我能够拼凑的代码是这样的。 .Net 方法是实现我的目标的适当方法,还是将 Send-MailMessge 用于 hastable 更好的方法? 我是 PowerShell 代码的新手,我可以使用这两种方法。但想知道 A. 首选哪种方法。 B. 有没有better/more有效的方法?

#####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 =)

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

还要确保您的代码容易被其他人阅读和理解,使用 hastables 和 splatting 将对此有所帮助,请参见以下示例:Github Gist Link (Click Me!)

代码副本,以防link失败。

### 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