The. NET method has been used for sending emails for a long time. It is easy and error-prone because of the built-in powershell method. Recently, some people have encountered problems in using it.
# Define the function Sendmail ($ mailaddr, $ body) {$ MSG = new-Object System. net. mail. mailmessage $ MSG. to. add ($ mailaddr) $ MSG. from = new-Object System. net. mail. mailaddress ("email sending account @ xxx.com", "display name", [system. text. encoding]: getencoding ("gb2312") $ MSG. subject = "title" $ MSG. subjectencoding = [system. text. encoding]: getencoding ("gb2312") $ MSG. body = $ body # $ attachments = new-Object System. net. mail. attachment ("D: \ Documents ents \ xxxx . Zip ") # create an attachment # $ msg. attachments. Add ($ attachments) # Add an attachment. Multiple English names can be added, and only one Chinese name can be included. $ MSG. bodyencoding = [system. text. encoding]: getencoding ("gb2312") $ MSG. isbodyhtml = $ false # Send an HTML-format email # $ MSG. priority = [system. net. mail. mailpriority]: high $ client = new-Object System. net. mail. smtpclient ("SMTP address") $ client. usedefaultcredentials = $ false $ client. credentials = new-Object System. net. networkcredential ("account @ xxx.com", "password") Try {$ client. send ($ MSG)} catch [Exception] {$ ($ _. exception. message) $ mailaddr }}# send email Sendmail $ mailaddr $ body
Zookeeper
Powershell sends emails through SMTP