Monday, February 27, 2006

 
Sending email using MSH part 2

Thanks to MOW at microsoft.public.windows.server.scripting newsgroup. There is an easier way to send email.


$smtp = new-object Net.Mail.SmtpClient("smtp.nospam.com")
$smtp.send("someone@nospam.com","someone@nospam.com", "Email using MSH", "This is easy.")


This example works well when the computer is within the subnet of the email SMTP server. However, I cannot run this at home, because there are more restrictions. For example, we need to enable SSL and also we need to supply credential to send messages outside our network. Here is another example to enable SSL and use credential.


$smtp = new-object Net.Mail.SmtpClient
$smtp.Host = "smtp.nospam.com"
$smtp.EnableSsl = $true
$smtp.Credentials = new-object Net.NetworkCredential("username", "password")
$smtp.send("someone@nospam.com","someone@nospam.com", "Email using MSH", "This is easy.")


If you need to send attachment and you also want to BCC(or CC) other people, you'll need to use another class: System.Net.Mail.MailMessage. Here is another example.


$smtp = new-object Net.Mail.SmtpClient
$smtp.Host = "smtp.nospam.com"
$smtp.EnableSsl = $true
$smtp.Credentials = new-object Net.NetworkCredential("username", "password")

$msg = new-object Net.Mail.MailMessage
$msg.From = "someone@nospam.com"
$msg.To.Add("someone@nospam.com")
$msg.Bcc.Add("someone@gmail.com")
$msg.Subject = "Full test"
$msg.Body = "This is a full test"
$att = new-object Net.Mail.Attachment("c:\testfile.txt")
$msg.Attachments.Add($att)

$smtp.Send($msg)

Saturday, February 25, 2006

 
Monad Beta 3 Available

I haven't played with Monad for a while and found that Beta 3 is available. You can download it from here.

Friday, February 24, 2006

 
Amazon Web Service

I got some experiences with Amazon Web Service when I created my Anime Reviews site. It's pretty neat that you can write a query to look up very detailed product information. Recently, I am thinking to buy a Garmin StreetPilot i3 from Amazon. The regular price is around $308.00, but I heard that sometimes Amazon would lower the price to as low as $199.00. However, the low price is only available for a short time. I don't want to sit in front of my computer and manually check the price myself. Wouldn't it be nice if I can have a script to check the price automatically? It would be nicer is the script if I write the script using MSH!

Ok, enough rambling. Let's get started. To use Amazon Web Service, the first thing to do is to create an account and obtain your own Access Key ID. You can create a free account here. After the account has been created successfully, you can log on to the site with your new account. Move the mouse cursor on top of the button that says "Your Web Services Account". A drop down menu would appear, click "View Access Key Identifiers". There are two keys, one is called "Access Key ID", the other is called "Secret Access Key". Make a note of the Access Key ID, this is the key you used for the web service. The next step is sending the Secret Access Key to me. :-) Ok, just kidding. You should NOT share Secret Access Key with anyone, it's only used to create signature. You should keep it confidential and not reveal it to anyone.

Amazon Web Service offers several ways to find product information. ItemSearch is used to search products by using keywords. Since we already know the product that we are interested in, we can just use ItemLookup. To use ItemLookup, you need two pieces of information, AWSAccessKeyId and ItemId. You already have the Access Key ID, all you need to know is the ItemId for the product. Each product offered on Amazon has a unique ID. If you look at a product page, it's called ASIN. You use ASIN as ItemId for the query. For books query, use ISBN as ItemId in your query. This is the query string for looking up information for the Garmin i3.


$url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService"
$url += "&AWSAccessKeyId=Your_AccessKeyID_Here"
$url += "&Operation=ItemLookup"
$url += "&ItemId=B000ACHVVE"
$url += "&ResponseGroup=Offers"


Note that this is for North America www.amazon.com. If you want to query product information for other Amazon sites. You need to change the first line accordingly. You can check here for information on how to modify the query string. Another point to note is that if you don't specify ResponseGroup in your query, the default response is "Request" and "Small" which don't have the price information. To get the current price, we use "Offers" as response group. Finally, we use .Net's Net.WebClient class to get the response from Amazon Web Service. The response from the server is an xml string. We cast it to XMLDocument for easy accessing the data. The amount returned from the query is in cents, so we divide it by 100 to get the dollar amount.


$rxml = [xml](new-object Net.WebClient).DownloadString("$url")
$price = $rxml.ItemLookupResponse.Items.Item.Offers.Offer.OfferListing.Price.Amount
$price = $price/100
echo $price


You might ask how did you come up with the offer price? You can check the documentation in Amazon Web Service web site, or you can save the response as an XML file and use your favorite XML editor/reader to examine the response. FireFox is just fine on parsing the XML file.


$sxml = (new-object Net.WebClient).DownloadString("$url")
echo $sxml > "c:\response.xml"

This page is powered by Blogger. Isn't yours?