RSS

使用PS原生脚本发邮件

02 Jun
   1: #mail server configuration
   2: $smtpServer = "smtp.live.com"
   3: $smtpUser = "xunyang@live.com"
   4: $smtpPassword = "P@ssw0rd" 
   5:  
   6: #create the mail message
   7: $mail = New-Object System.Net.Mail.MailMessage 
   8:  
   9: #set the addresses
  10: $MailAddress="xunyang@live.com"
  11: $MailtoAddress="xunyang@live.com"
  12: $mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
  13: $mail.To.Add($MailtoAddress) 
  14:  
  15: #set the content
  16: $mail.Subject = "Hello Yang Xun";
  17: $mail.Priority  = "High"
  18: $mail.Body = "Sending mail is from powershell!"
  19: $filename="C:\Users\xunyang\Desktop\powershellmail.txt"
  20: $attachment = new-Object System.Net.Mail.Attachment($filename)
  21: $mail.Attachments.Add($attachment) 
  22:  
  23: #send the message
  24: $smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
  25: $smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
  26: $smtp.Send($mail)

效果图

Advertisements
 
1 Comment

Posted by on June 2, 2009 in Powershell

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: