随笔 - 24, 文章 - 0, 评论 - 1, 引用 - 0
数据加载中……

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"
$attachment = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($attachment)

#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`" <pkrzysz@nospam.pjwstk.edu.pl>"
$objMessage.To = "pkrzysz@nospam.pjwstk.edu.pl"
$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`" <pkrzysz@nospam.pjwstk.edu.pl>"
$objMessage.To = "pkrzysz@nospam.pjwstk.edu.pl"
$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 = "me@my.com"
objMessage.To = "test@paulsadowski.com"
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 = "you@your.com"
objMessage.Cc = “you2@your.com”

 

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"" nadawca@domena.com"
objMessage.To = "odbiorca1@domena1.com"
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"" <me@my.com>"
objMessage.To = "test@paulsadowski.com"
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,并安装SMTP服务,在Windows Server 2008上同样被支持,但是找不出不升级程序的理由,谁都不希望再去多装服务,麻烦不说,无形中还增加了一点安全风险,而且这种方法也不会被微软支持!http://www.jppinto.com/index.php/2009/03/install-cdonts-mail-component-and-smtp-on-windows-server-2008/

http://weblogs.asp.net/steveschofield/archive/2008/08/18/getting-cdonts-to-work-on-windows-server-2008-x64.aspx

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 = "icil_sup@icil.net"
objMessage.To = "allenzhang@icil.net,havingchan@icil.net"
strSpace = "Available Space on C:\:" & FormatNumber(drive.AvailableSpace/1024^2) & "MB"
objMessage.Body = Now & vbtab &  strComputer & vbtab & strSpace
objMessage.Send

 

补充:Powershell:System.Web.Mail.MailMessage (利用CDOSYS消息组件发送电子邮件,.net 1.0和.net 1.1)

[System.Reflection.Assembly]::LoadWithPartialName("System.Web")

$WebMailMessage = New-Object System.Web.Mail.MailMessage

$WebMailMessage.From = 'powershell@powershell.com'

$WebMailMessage.Subject = 'TEST'

$WebMailMessage.To = 'someone@email.com'

$WebMailMessage.Body = 'This is a test of System.Web.Mail.SMTPMail'

$file="C:\test.txt"

$attach=new-object system.web.mail.mailattachment($file)

$webmailmessage.attachments.add($attach)

[System.Web.Mail.SmtpMail]::smtpServer = 'smtp.server.com'

[System.Web.Mail.SmtpMail]::send($WebMailMessage)

posted on 2009-04-09 21:24 nicktang 阅读(645) 评论(1)  编辑 收藏 引用 所属分类: PowershellScript

只有注册用户登录后才能发表评论。