首页 > 解决方案 > 我想使用 PowerShell 发送加密邮件

问题描述

我想使用 Powershell 使用 S/MIME 标准加密邮件。我能够发送带有附件的邮件(没有来自 powershell 的加密)。但是当我尝试使用以下代码对其进行加密时。它不起作用,因为它只是删除文件并发送未加密的正文。我正在使用 powershell 版本 5。出于测试目的,我想使用以前生成的证书,而不是从某个地方获取。请就我的问题提出建议,或者是否有更好的方法。

$email = "*********@gmail.com" 
$pass = "*******" 

$smtpServer = "smtp.gmail.com" 


$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 
$smtp.EnableSsl = $true 
$msg.From = "$email"  
$msg.To.Add("*****b@***.com") 
$msg.BodyEncoding = [system.Text.Encoding]::Unicode 
$msg.SubjectEncoding = [system.Text.Encoding]::Unicode 
$msg.Headers.Add('MIME-Version','asd')                                                                                                                                          

$msg.Headers.Add("From","**abc**")                                                                                                                                             
$msg.Headers.Add("To","**xyz**")                                                                                                                                              
$msg.Headers.Add("Date","11-11-1111")                                                                                                                                           
$msg.Headers.Add("Subject","asfasdd")
$msg.HeadersEncoding =[system.Text.Encoding]::UTF8
$msg.IsBodyHTML = $true  
$msg.Subject = "Test mail from PS" 
$msg.Body = "<h2> Test mail from PS </h2> 
</br> 
Hi there 
" 
$file= get-item -Path "E:\siddhant\abc.txt" 
$SMTP.Credentials = New-Object System.Net.NetworkCredential("$email", "$pass"); 
$Certname="helloo"
$Cert = New-SelfSignedCertificate -certstorelocation E:\siddhant -dnsname $Certname


$pw = ConvertTo-SecureString -String "Pazzword" -Force -AsPlainText
$thumbprint = $Cert.Thumbprint

$MIMEMessage = New-Object system.Text.StringBuilder 
$MIMEMessage.AppendLine("MIME-Version: 1.0") | Out-Null 
$MIMEMessage.AppendLine("Content-Type: multipart/mixed; boundary=unique-boundary-1") | Out-Null 
$MIMEMessage.AppendLine() | Out-Null
$MIMEMessage.AppendLine("This is a multi-part message in MIME format.") | Out-Null
$MIMEMessage.AppendLine("--unique-boundary-1") | Out-Null
$MIMEMessage.AppendLine("Content-Type: text/plain") | Out-Null
$MIMEMessage.AppendLine("Content-Transfer-Encoding: 7Bit") | Out-Null
$MIMEMessage.AppendLine()
$MIMEMessage.AppendLine($Body) | Out-Null
$MIMEMessage.AppendLine() | Out-Null

$MIMEMessage.AppendLine("--unique-boundary-1") | Out-Null
$MIMEMessage.AppendLine("Content-Type: application/octet-stream; name="+ $file.Name) | Out-Null
$MIMEMessage.AppendLine("Content-Transfer-Encoding: base64") | Out-Null
$MIMEMessage.AppendLine("Content-Disposition: attachment; filename="+ $file.Name) | Out-Null
$MIMEMessage.AppendLine() | Out-Null

[Byte[]] $binaryData = [System.IO.File]::ReadAllBytes($file)
[string] $base64Value = [System.Convert]::ToBase64String($binaryData, 0, $binaryData.Length)
[int] $position = 0
while($position -lt $base64Value.Length)
{
[int] $chunkSize = 100
if (($base64Value.Length - ($position + $chunkSize)) -lt 0)
{
    $chunkSize = $base64Value.Length - $position
}
$MIMEMessage.AppendLine($base64Value.Substring($position, $chunkSize))
$MIMEMessage.AppendLine()
$position += $chunkSize;
}

$MIMEMessage.AppendLine("--unique-boundary-1--") | Out-Null

[Byte[]] $BodyBytes = [System.Text.Encoding]::ASCII.GetBytes($MIMEMessage.ToString())
$ContentInfo =  New-Object -TypeName System.Security.Cryptography.Pkcs.ContentInfo -            
ArgumentList(,$BodyBytes) 
$CMSRecipient = New-Object System.Security.Cryptography.Pkcs.CmsRecipient $Cert 
$EnvelopedCMS = New-Object System.Security.Cryptography.Pkcs.EnvelopedCms $ContentInfo 
$EnvelopedCMS.Encrypt($CMSRecipient) 
[Byte[]] $EncryptedBytes = $EnvelopedCMS.Encode() 
$MemoryStream = New-Object System.IO.MemoryStream @(,$EncryptedBytes) 
$AlternateView = New-Object System.Net.Mail.AlternateView($MemoryStream, "application/pkcs7-mime;     
smime-type=enveloped-data;name=smime.p7m") 
$msg.AlternateViews.Add($AlternateView)

$smtp.Send($msg)

标签: powershellemailencryptionsmtpsmime

解决方案


推荐阅读