Sendmail Function

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][email protected][/email]>"
$objMessage.To = "[email][email protected][/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][email protected][/email]>"
$objMessage.To = "[email][email protected][/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][email protected][/email]"
objMessage.To = "[email][email protected][/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][email protected][/email]"
objMessage.Cc = “[email][email protected][/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][email protected][/email]"
objMessage.To = "[email][email protected][/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][email protected][/email]>"
objMessage.To = "[email][email protected][/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][email protected][/email]"
objMessage.To = "[email][email protected][/email],[email][email protected][/email]"
strSpace = "Available Space on C:\:" & FormatNumber(drive.AvailableSpace/1024^2) & "MB"
objMessage.Body = Now & vbtab &  strComputer & vbtab & strSpace
objMessage.Send
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章