我对PowerShell不太了解,但是我在Stack Overflow的这里找到了有关如何发送带有附件的电子邮件的代码。我想知道是否可以对其进行一些更改,以便我可以发送两个附件而不是一个。我想不使用ZIP或RAR进行压缩就发送文件:
附件:“ C:\ Users \ ricar \ Desktop \ impressora.txt”“ C:\ Users \ ricar \ Desktop \ impressora2.txt”
$Username = "[email protected]";
$Password = "mypassword";
$path = "C:\Users\ricar\Desktop\impressora.txt";
function Send-ToEmail([string]$email, [string]$attachmentpath){
$message = new-object Net.Mail.MailMessage;
$message.From = "[email protected]";
$message.To.Add($email);
$message.Subject = "Hello how are you";
$message.Body = "Is this really going to happen?????";
$attachment = New-Object Net.Mail.Attachment($attachmentpath);
$message.Attachments.Add($attachment);
$smtp = new-object Net.Mail.SmtpClient("smtp.sapo.pt", "587");
$smtp.EnableSSL = $true;
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message);
write-host "Mail Sent" ;
$attachment.Dispose();
}
Send-ToEmail -email "[email protected]" -attachmentpath $path;
我会-attachmentpath
通过使用字符串数组作为参数[string[]]
。您还可以省去创建单独Net.Mail.Attachment
对象的步骤,因为该对象已经包含在Net.Mail.MailMessage
您已有的基础对象中。
例:
$Username = "[email protected]"
$Password = "mypassword"
$path = "C:\Users\ricar\Desktop\impressora.txt","C:\Users\ricar\Desktop\impressora2.txt"
function Send-ToEmail([string]$email, [string[]]$attachmentpath){
$message = new-object Net.Mail.MailMessage
$message.From = "[email protected]"
$message.To.Add($email)
$message.Subject = "Hello how are you"
$message.Body = "Is this really going to happen?????"
$attachmentpath | foreach {$message.Attachments.Add($_)}
$smtp = new-object Net.Mail.SmtpClient("smtp.sapo.pt", "587")
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$smtp.send($message)
write-host "Mail Sent"
}
Send-ToEmail -email "[email protected]" -attachmentpath $path
另外,在PowerShell中,无需在每行的末尾添加分号。如果符合当前代码样式准则,我只会使用它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句