cls; function Send-MyEMail { <# .SYNOPSIS 用beidasoft@163.com发送邮件 .DESCRIPTION 用beidasoft@163.com发送邮件 .PARAMETER to 邮件接收者 .PARAMETER body 内容 .PARAMETER subject 标题 .PARAMETER ATTACHMENT 附件 .EXAMPLE PS C:\>Send-MyEMail -to "10086@qq.com" -subject "移动你好" -body "我是联通" .INPUTS System.String,System.Int32 .OUTPUTS System.String .NOTES Additional information about the function go here. .LINK about_functions_advanced .LINK about_comment_based_help #> [CmdletBinding()] [OutputType([System.Int32])] param( [Parameter(Position=0, Mandatory=$true)] [ValidateNotNullOrEmpty()] [System.String] $to, [Parameter(Position=1)] [ValidateNotNullOrEmpty()] [System.String] $subject, [Parameter(Position=2)] [ValidateNotNullOrEmpty()] [System.String] $body, [Parameter(Position=3)] [System.String] $attachment ) [String]$local:msg='' try { #密码加密串 $script:secureString=ConvertTo-SecureString -String "76492d1116743f0423413b16050a5345MgB8AGQASAB3AEkAQQBSAHoAYgBTAEEAQgByADgAZABhAGsAUQA3ADgAaQBWAFEAPQA9AHwAZAA2ADkANAAxADQAZgA 1ADUAZABiAGUAMQBmAGEAOAA2AGIAZABkADkANQBjADgAZgBiAGQAMABiADYAOABkADYAYwAyADQAYgA0AGYAZABjADgAMgA4ADcAMAAyAGMAYQAwADQANQA0AD QAYgAzADEAOQA5ADkAMQAyADkANQA=" -Key (1..16) #证书 $script:credential=New-Object system.Management.Automation.PSCredential("beidasoft",$secureString) if($attachment -eq $null -or $attachment -eq "" ){ Send-MailMessage -To $to -Body $body -Subject $subject -From "beidasoft@163.com" -UseSsl -Credential $credential -Encoding utf8 -Port 25 -SmtpServer "smtp.163.com" $msg="邮件发送成功!" } else{ if( test-path $attachment ){ Send-MailMessage -Attachments $attachment -To $to -Body $body -Subject $subject -From "beidasoft@163.com" -UseSsl -Credential $credential -Encoding utf8 -Port 25 -SmtpServer "smtp.163.com" $msg= "邮件发送成功!附件"+$attachment } else{ Send-MailMessage -To $to -Body $body -Subject $subject -From "beidasoft@163.com" -UseSsl -Credential $credential -Encoding utf8 -Port 25 -SmtpServer "smtp.163.com" $msg= "邮件发送成功!附件"+$attachment+"无效" } } } catch { $msg="出错了" }finally{ 'At'+([DateTime]::Now).toString()+' '+$msg|Write-Host -ForegroundColor Green } }; Send-MyEMail -to "121354381@qq.com" -subject "移动你好" -body "我是联通" -attachment "d:\s.txt"