PowerShell邮件自动转发加附件:.Net与Send-MailMessage孰优?
Hey there! Let's break down your questions clearly since you're just getting started with PowerShell—great job getting a working solution already, that's half the battle!
A. Which method is more recommended?
First, let's clarify the two approaches you mentioned and weigh their pros and cons:
1. Send-MailMessage + Hashtable (Splatting)
- Pros: This is the most beginner-friendly option.
Send-MailMessageis a dedicated PowerShell cmdlet with straightforward, self-documenting parameters (like-To,-From,-Attachment,-SmtpServer). Using splatting (storing parameters in a hashtable) makes your code cleaner and easier to maintain, especially when you have lots of parameters to manage. - Cons: A critical heads-up:
Send-MailMessageis marked as obsolete in PowerShell 7+. While it still works reliably in Windows PowerShell 5.1, it won't receive new features and might be phased out in future updates. So this isn't a long-term solution if you plan to upgrade to modern PowerShell later.
2. .NET Class Methods (e.g., System.Net.Mail.SmtpClient, System.Net.Mail.MailMessage)
- Pros: This approach offers far more flexibility. You have full control over every aspect of the email—custom headers, advanced HTML body formatting, multiple attachments, and fine-grained delivery settings. Unlike
Send-MailMessage, .NET mail classes aren't obsolete (thoughSmtpClienthas some legacy notes, you can use it reliably or switch to newer libraries likeMailKitfor even more control). It's the better choice for future-proof scripts or when you need advanced email functionality. - Cons: The syntax is a bit more verbose since you're working directly with .NET objects, which might feel overwhelming at first glance.
My Recommendation:
- If you're still learning and want something simple to maintain right now: Go with
Send-MailMessage+ splatting. It's perfect for getting the job done quickly without extra complexity. - If you want a script that's future-proof or needs advanced email control: Switch to the .NET method (or even better, check out the modern option in part B).
Quick example of splatting with Send-MailMessage:
$mailParams = @{ To = "recipient@example.com" From = "sender@example.com" Subject = "Forwarded: New Alert" Body = "Attached is the required document." SmtpServer = "smtp.yourserver.com" Attachment = "C:\path\to\your\file.pdf" } Send-MailMessage @mailParams
Example of the .NET method:
# Create the mail message object $mail = New-Object System.Net.Mail.MailMessage $mail.From = "sender@example.com" $mail.To.Add("recipient@example.com") $mail.Subject = "Forwarded: New Alert" $mail.Body = "Attached is the required document." # Add the specified attachment $attachment = New-Object System.Net.Mail.Attachment("C:\path\to\your\file.pdf") $mail.Attachments.Add($attachment) # Send via your SMTP server $smtp = New-Object System.Net.Mail.SmtpClient("smtp.yourserver.com") $smtp.Send($mail) # Clean up resources $attachment.Dispose() $mail.Dispose() $smtp.Dispose()
B. Are there better implementation options?
Absolutely! If you're working with Exchange Online (Office 365) or modern Outlook, the best long-term solution is to use the Microsoft Graph API. Here's why:
- It's the official, supported way to interact with Microsoft 365 services, replacing older APIs like EWS and SMTP-based methods.
- Uses secure OAuth 2.0 authentication instead of plaintext passwords or basic auth, which is more secure and compliant.
- Lets you directly monitor Outlook folders, forward emails, and manage attachments without setting up an external SMTP server.
Quick Overview of a Graph-based Workflow:
- Register an app in Azure AD to get the required API permissions (you'll need
Mail.ReadWriteandMail.Sendpermissions). - Install the Microsoft Graph PowerShell module:
Install-Module -Name Microsoft.Graph.Mail -Force - Authenticate to Graph with the necessary scopes:
Connect-MgGraph -Scopes "Mail.ReadWrite", "Mail.Send" - Use cmdlets like
Get-MgUserMessageto monitor your target folder, thenSend-MgUserMessageto forward the email with attachments programmatically.
Another low-effort alternative: Use Exchange Online Inbox Rules. You can create a rule directly in Exchange that automatically forwards emails matching your criteria and adds an attachment (note: attachment support here is limited to files stored in SharePoint/OneDrive, not local files).
内容的提问来源于stack exchange,提问作者mynameis

