Powershell:System.Net.Mail.MailMessage
#mail server configuration
$smtpServer = "smtpServer"
$smtpUser = "smtpUser"
$smtpPassword = "smtpPassword "
#create the mail message
$mail = New-Object System.Net.Mail.MailMessage
#set the addresses
$MailAddress="MailAddress"
$MailtoAddress="MailtoAddress"
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
$mail.To.Add($MailtoAddress)
#set the content
$mail.Subject = "Hello PowerShell";
$mail.Priority  = "High"
$mail.Body = "Sending mail is easy!"
$filename="file"
$p_w_upload = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($p_w_upload)
#send the message
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
$smtp.Send($mail)
为了防止密码以明文形式出现在脚本中,可以使用Get-Credential cmdlet
$cred = (Get-Credential domain\user).GetNetworkCredential()
$smtp.Credentials = $cred
支持SSL
$smtp.EnableSsl = $True
 
Powershell:CDO.Message
$objMessage = new-object -com CDO.Message
$objMessage.From = "`"Krzysztof Pietrzak`" <[email]pkrzysz@nospam.pjwstk.edu.pl[/email]>"
$objMessage.To = "[email]pkrzysz@nospam.pjwstk.edu.pl[/email]"
$objMessage.Subject = " Message Subject"
$objMessage.TextBody = "Body of message"
# Send using SMTP
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
#SMTP Server
$objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.server.org"
#SMTP Server Port
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
#Authenticaztion 1-Baasic, 2-NTLM
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 2
#Use ssl
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 0
#Timeout
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
$objMessage.Configuration.Fields.Update()
$objMessage.Send()
 
Powershell:CDO.Message带有身份验证和SSL/TLS
$objMessage = new-object -com CDO.Message
$objMessage.From = "`"Krzysztof Pietrzak`" <[email]pkrzysz@nospam.pjwstk.edu.pl[/email]>"
$objMessage.To = "[email]pkrzysz@nospam.pjwstk.edu.pl[/email]"
$objMessage.Subject = " Message Subject"
$objMessage.TextBody = "Body of message"
# Send using SMTP
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
#SMTP Server
$objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "dfs2"
#SMTP Server Port
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
#Authenticaztion 1-Baasic, 2-NTLM
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
#UserID
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "yourUser"
#Password
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yourpassword"
#Use ssl
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
#Timeout
$objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
$objMessage.Configuration.Fields.Update()
$objMessage.Send()
VBScript:CDO.Message
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "[email]me@my.com[/email]"
objMessage.To = "[email]test@paulsadowski.com[/email]"
objMessage.TextBody = "This is some sample message text."
objMessage.Send
VBScript:CDO.Message带有附件和邮件的HTML
'The line below shows how to send using HTML included directly in your script
objMessage.HTMLBody = "<h1>This is some sample message html.</h1>"
'The line below shows how to send a webpage from a remote site
objMessage.CreateMHTMLBody "http://www.paulsadowski.com/wsh/"
'The line below shows how to send a webpage from a file on your machine
objMessage.CreateMHTMLBody "file://c|/temp/test.htm"
objMessage.Bcc = "[email]you@your.com[/email]"
objMessage.Cc = “[email]you2@your.com[/email]”
VBScript:CDO.MessageNTLM
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Temat"
objMessage.From = """nadawca"" [email]nadawca@domena.com[/email]"
objMessage.To = "[email]odbiorca1@domena1.com[/email]"
objMessage.TextBody = "Serwer "
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpServer"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoNTLM
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
VBScript:CDO.Message基本身份验证和SSL
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = """Me"" <[email]me@my.com[/email]>"
objMessage.To = "[email]test@paulsadowski.com[/email]"
objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "youruserid"
'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yourpassword"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = TRUE
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
 
VBScript:CDONTS,在2000下使用,XP和2003需注册相应dll
Dim strComputer, objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objMessage = CreateObject("CDONTS.NewMail")
set fs = CreateObject("Scripting.FileSystemObject")
set drive = fs.GetDrive("C:\")
strComputer = objNetwork.ComputerName
objMessage.Subject = "Daily report of C drive available space on  " & strComputer
objMessage.From = "[email]icil_sup@icil.net[/email]"
objMessage.To = "[email]allenzhang@icil.net[/email],[email]havingchan@icil.net[/email]"
strSpace = "Available Space on C:\:" & FormatNumber(drive.AvailableSpace/1024^2) & "MB"
objMessage.Body = Now & vbtab &  strComputer & vbtab & strSpace
objMessage.Send