Source Code for Blog Digest for C#: BlogDigest.cs in Java

Drawing QR-Code in Java Source Code for Blog Digest for C#: BlogDigest.cs

Listing 15.2 Source Code for Blog Digest for C#: BlogDigest.cs
Encode QR Code JIS X 0510 In Java
Using Barcode encoder for Java Control to generate, create QR Code image in Java applications.
www.OnBarcode.com
QR Code JIS X 0510 Decoder In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
using using using using using using using System; System.Xml; System.Text; System.Net; System.Collections; BlogApps_05; System.Web.Mail;
Data Matrix 2d Barcode Maker In Java
Using Barcode printer for Java Control to generate, create Data Matrix image in Java applications.
www.OnBarcode.com
Painting Code-39 In Java
Using Barcode drawer for Java Control to generate, create Code 39 Extended image in Java applications.
www.OnBarcode.com
b C D
Barcode Drawer In Java
Using Barcode drawer for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Code 128B Creator In Java
Using Barcode creation for Java Control to generate, create ANSI/AIM Code 128 image in Java applications.
www.OnBarcode.com
namespace BlogApps_15 { public class BlogDigest { string username; string password; string to_address; string from_address; string smtp_server; IList subs = new ArrayList(); string auth_key = "http://schemas.microsoft.com /cdo/configuration/smtpauthenticate"; string username_key = "http://schemas.microsoft.com/cdo/configuration/sendusername"; string password_key = "http://schemas.microsoft.com/cdo/configuration/sendpassword";
UCC.EAN - 128 Generator In Java
Using Barcode maker for Java Control to generate, create EAN / UCC - 14 image in Java applications.
www.OnBarcode.com
Making UPC Shipping Container Symbol ITF-14 In Java
Using Barcode drawer for Java Control to generate, create UPC Case Code image in Java applications.
www.OnBarcode.com
static void Main(string[] args) { BlogDigest blogDigest = new BlogDigest(); blogDigest.InitFromXml(); blogDigest.run(); } public void run() { System.Console.WriteLine("BlogDigest for C#"); IFeedParser feedParser = new AnyFeedParser(); DateTime since = DateTime.Now.AddDays(-5.0); StringBuilder sb = new StringBuilder(); foreach (string sub in subs) {
Paint QR Code 2d Barcode In None
Using Barcode creation for Office Excel Control to generate, create QR Code JIS X 0510 image in Microsoft Excel applications.
www.OnBarcode.com
Scan Quick Response Code In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
J 1)
Encoding QR In None
Using Barcode generation for Online Control to generate, create QR Code JIS X 0510 image in Online applications.
www.OnBarcode.com
Generating PDF417 In .NET
Using Barcode drawer for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
1! 1#
PDF 417 Printer In VB.NET
Using Barcode encoder for .NET Control to generate, create PDF417 image in Visual Studio .NET applications.
www.OnBarcode.com
EAN13 Recognizer In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sub); request.IfModifiedSince = since;
USS-128 Generator In VS .NET
Using Barcode printer for Reporting Service Control to generate, create EAN128 image in Reporting Service applications.
www.OnBarcode.com
Creating UPC Code In .NET
Using Barcode generator for Reporting Service Control to generate, create UPC-A Supplement 5 image in Reporting Service applications.
www.OnBarcode.com
try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); IDictionary feedMap = feedParser.ParseFeed(response.GetResponseStream());
Data Matrix Generation In None
Using Barcode printer for Software Control to generate, create ECC200 image in Software applications.
www.OnBarcode.com
Draw Code-128 In Objective-C
Using Barcode maker for iPad Control to generate, create Code 128 Code Set B image in iPad applications.
www.OnBarcode.com
sb.Append("-----------------------------\n");
Painting Code 128 Code Set C In Objective-C
Using Barcode creator for iPhone Control to generate, create USS Code 128 image in iPhone applications.
www.OnBarcode.com
Encode USS-128 In VS .NET
Using Barcode generator for .NET Control to generate, create EAN / UCC - 14 image in .NET applications.
www.OnBarcode.com
Sending a daily blog digest by email
sb.Append("Blog: "); sb.Append(feedMap["title"]); sb.Append("\n\n"); IList itemsList = (IList)feedMap["items"]; foreach (IDictionary itemMap in itemsList) { DateTime pubDate = (DateTime)itemMap["pubDate"]; if (itemMap["pubDate"] == null) { pubDate = (DateTime)itemMap["dc:date"]; } if (pubDate > since) { string title = (string)itemMap["title"]; string content = (string)itemMap["description"]; if (content == null) { content = (string)itemMap["content:encoded"]; } sb.Append("Title: "); sb.Append((String)itemMap["title"]); sb.Append("\n"); sb.Append(itemMap["link"]); sb.Append("\n\n"); } }
} catch (WebException e) { HttpWebResponse res = (HttpWebResponse)e.Response; if (res != null) { if (res.StatusCode == HttpStatusCode.NotModified) { System.Console.WriteLine("Not modified: "+sub); } } else { System.Console.WriteLine( "ERROR: connecting to destination server "); System.Console.WriteLine(e.Message); System.Console.WriteLine(e.StackTrace); } }
} string text = sb.ToString(); if (text.Length > 0) { try { MailMessage Message = new MailMessage(); Message.To = to_address; Message.From = from_address; Message.Subject = "Daily Blog Digest"; Message.Body = text; Message.Fields.Add(auth_key, "1"); if (username != null && password != null) { Message.Fields.Add(username_key, username); Message.Fields.Add(password_key, password); }
The code for Blog Digest for C#
SmtpMail.SmtpServer = smtp_server; SmtpMail.Send(Message);
} catch(Exception ex) { Console.WriteLine("ERROR: " + ex.ToString() ); while( ex.InnerException != null ) { Console.WriteLine( "ERROR" + ex.InnerException.ToString() ); ex = ex.InnerException; } }
} } private void InitFromXml() { // . . . } } }
BlogDigest.cs starts with the familiar preamble of using statements. We include the BlogApps_05 namespace b so that we can use the AnyFeedParser class, and we include the System.Web.Mail namespace C so that we can use .NET s built-in support for sending mail via SMTP protocol. Just inside the BlogDigest class D, we define fields for each of the parameters we will read from the configuration file. We use a list to hold the newsfeed URL strings E. We also include keys that we ll use later for setting email connection authentication F, username G, and password H. In the main method of the class, we create an instance of the class called the InitFromXml() method to read the configuration file I and then we use the run() method to start processing. In the run() method, we first create an instance of the AnyFeedParser class J and create a DateTime object set to one day in the past 1) . Next, we create a StringBuilder, which we ll use to build the digest email message. 1! Using a foreach loop 1@ , we iterate through each of the subscriptions specified in the configuration file. In the loop, we create an HttpWebRequest to fetch the subscription s newsfeed 1# and then set the IfModifiedSince property to perform an HTTP conditional GET. Next, inside a try block, we attempt to get the response from the web server 1$. If the newsfeed has not been modified since the since date, the call to request.getResponse() will throw an exception. If that happens, we ll move on to process the next subscription. If we get a response, we hand the response stream to the feed parser 1%.
Sending a daily blog digest by email
Once we have parsed a newsfeed, we add a header to the email message with the newsfeed s title 1^ and loop through the items in the newsfeed 1&. For each item in the newsfeed, we get the publication date 1* . If we don t find it under the key pubDate 1( , we look for a Dublin Core date using the key dc:date. If the item s date is more recent than the since date 2), we add the item s title 2! and link 2@ to the email message. Still inside the foreach loop, we have a catch block to catch any web exceptions that occur during the processing of one subscription. If the response status code is HTTP_NOT_MODIFIED 2# , we print out a Not Modified message and proceed to the next feed. Otherwise, we print out the exception 2$ and continue with the next subscription. Once we finish processing subscriptions and we re out of the foreach loop, we convert the StringBuilder to a string 2%. If that string is not empty, we create a mail message 2^ and set its to 2& and from 2* fields based on configuration parameters. We set the text of the message 2( using the email message we built. If the configuration specified a mail server username and password 3) , we set those fields in the message too. Finally, we send the message on its way 3! . Note that we set up and send the message in a try-catch block, and we take special care to print out any exceptions that are thrown 3@ . The .NET System. Web.Mail classes do not have the best exception handling, and sometimes the only way to debug problems with them is to examine the inner exceptions.
Copyright © OnBarcode.com . All rights reserved.