{"id":32969,"date":"2010-04-05T16:58:45","date_gmt":"2010-04-05T15:58:45","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=32969"},"modified":"2024-11-06T11:23:24","modified_gmt":"2024-11-06T03:23:24","slug":"%e4%bd%bf%e7%94%a8powershell%e9%80%9a%e8%bf%87smtp%e5%8f%91%e9%80%81%e9%82%ae%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=32969","title":{"rendered":"\u4f7f\u7528PowerShell\u901a\u8fc7Smtp\u53d1\u9001\u90ae\u4ef6"},"content":{"rendered":"<p>\u501f\u52a9.NET\u6846\u67b6\uff0c\u4f7fPowerShell\u5177\u6709\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u529f\u80fd\uff0cPowrShell\u53ef\u901a\u8fc7Smtp\u53d1\u9001\u90ae\u4ef6\uff0c\u4ee5\u4e0b\u662f\u4ee3\u7801\uff1a <\/p>\n<pre>$comments = @'\nauthor:fuhj(powershell@live.cn ,http:\/\/txj.shell.tor.hu)\nexample:\nsend-mail -toAddress user@domain.com -subject \"Powershell Testing Mail \" -body \"This is a test mail form powershell\" -file \"C:\\powershellmailfile.txt\"\n-toName -body and -file are all optional.\nuse double quotes for the name parameters ie; -body \"Proper Content\"\n'@\nfunction send-mail{\n\tparam(\n\t     [string]$toAddress   = $(throw \"toAddress must be set\")\n\t    ,[string]$Subject     = $(throw \"subject must be set\")\n\t    ,[string]$body        = \"\"\n\t    ,[string]$file        = \"\")\n#mail server configuration\n\t$smtpServer   = \"smtp.live.com\"\n\t$smtpUser     = \"powershell@live.cn\"\n\t$smtpPassword = \"P@ssWord\"\n\t$sslNeed      =$true #SMTP server needs SSL should set this attribute\n\t$MailAddress  =\"powershell@live.cn\"\n\t$fromName     = \"fuhj\"\n\t$replyTo      = \"powershell@live.cn\"\n#create the mail message\n\t$mail = New-Object System.Net.Mail.MailMessage\n#set the addresses\n\t$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress,$fromName)\n\t$mail.To.Add($toAddress)\n#set the content\n\t$mail.Subject = $Subject\n\t$mail.Priority  = \"High\"\n\t$mail.Body = $Body\n\t$filename= $file\n\t$attachment = new-Object System.Net.Mail.Attachment($filename)\n\t$mail.Attachments.Add($attachment)\n#send the message\n\t$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer\n\t$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword\n\t$smtp.EnableSsl = $sslNeed;\n\ttry{\n\t\t$smtp.Send($mail)\n\t\techo 'Ok,Send succed!'\n\t}\n\tcatch \n\t{\n\t\techo 'Error!Filed!'\n\t}\n}<\/pre>\n<p>\u6267\u884c\u7684\u6548\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/04\/powershellmail1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" title=\"powershellmail\" style=\"border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px\" height=\"118\" alt=\"powershellmail\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/04\/powershellmail_thumb.png\" width=\"644\" border=\"0\"\/><\/a> <\/p>\n<p>\u6536\u5230\u7684\u90ae\u4ef6\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/04\/powershellreceviedmail1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"powershellreceviedmail\" style=\"border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px\" height=\"142\" alt=\"powershellreceviedmail\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/04\/powershellreceviedmail_thumb1.png\" width=\"644\" border=\"0\"\/><\/a> <\/p>\n<p>\u4f5c\u8005: \u4ed8\u6d77\u519b<br \/>\u7248\u6743\uff1a\u672c\u6587\u7248\u6743\u5f52\u4f5c\u8005\u6240\u6709<br \/>\u8f6c\u8f7d\uff1a\u6b22\u8fce\u8f6c\u8f7d\uff0c\u4e3a\u4e86\u4fdd\u5b58\u4f5c\u8005\u7684\u521b\u4f5c\u70ed\u60c5\uff0c\u8bf7\u6309\u8981\u6c42\u3010\u8f6c\u8f7d\u3011\uff0c\u8c22\u8c22<br \/>\u8981\u6c42\uff1a\u672a\u7ecf\u4f5c\u8005\u540c\u610f\uff0c\u5fc5\u987b\u4fdd\u7559\u6b64\u6bb5\u58f0\u660e\uff1b\u5fc5\u987b\u5728\u6587\u7ae0\u4e2d\u7ed9\u51fa\u539f\u6587\u8fde\u63a5\uff1b\u5426\u5219\u5fc5\u7a76\u6cd5\u5f8b\u8d23\u4efb <br \/>\u4e2a\u4eba\u7f51\u7ad9: <a href=\"http:\/\/txj.shell.tor.hu\">http:\/\/txj.shell.tor.hu<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u501f\u52a9.NET\u6846\u67b6\uff0c\u4f7fPowerShell\u5177\u6709\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u529f\u80fd\uff0cPowrShell\u53ef\u901a\u8fc7Smtp\u53d1\u9001\u90ae\u4ef6\uff0c\u4ee5\u4e0b\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[43],"class_list":["post-32969","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/32969"}],"collection":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=32969"}],"version-history":[{"count":10,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/32969\/revisions"}],"predecessor-version":[{"id":33818,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/32969\/revisions\/33818"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=32969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=32969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=32969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}