Advertisement
THEiNTRANETS

Webscrape Emailer

Apr 24th, 2014
2,703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Mail;
  6. using System.Net;
  7. using System.Configuration;
  8. using HtmlAgilityPack;
  9.  
  10. namespace WebscrapeMailer
  11. {
  12.     class Program
  13.     {
  14.         static void Main()
  15.         {
  16.             // Store configurable values from App.config.
  17.             string webURL1 = ConfigurationManager.AppSettings["config.webURL1"];
  18.             string webURL2 = ConfigurationManager.AppSettings["config.webURL2"];
  19.             string webURL3 = ConfigurationManager.AppSettings["config.webURL3"];
  20.             string URL1Node = ConfigurationManager.AppSettings["config.URL1Node"];
  21.             string URL2Node = ConfigurationManager.AppSettings["config.URL2Node"];
  22.             string URL3Node = ConfigurationManager.AppSettings["config.URL3Node"];
  23.             string mailTo = ConfigurationManager.AppSettings["config.MailTo"];
  24.             string mailFrom = ConfigurationManager.AppSettings["config.MailFrom"];
  25.             string mailSubject = ConfigurationManager.AppSettings["config.MailSubject"];
  26.             string mailServer = ConfigurationManager.AppSettings["config.MailServer"];
  27.        
  28.             // Create WebClient object for first webscrape.
  29.             WebClient scrape1 = new WebClient();
  30.            
  31.             string scrape1Value;
  32.            
  33.         // Download HTML from first webpage and parse text at specified node
  34.         // then give string to first scrape value.
  35.         string htmlCode1 = scrape1.DownloadString(webURL1);
  36.             HtmlAgilityPack.HtmlDocument doc1 = new HtmlAgilityPack.HtmlDocument();
  37.             doc1.LoadHtml(htmlCode1);
  38.             HtmlNode node1 = doc1.DocumentNode.SelectSingleNode(URL1Node);
  39.            
  40.         scrape1Value = (node1.InnerText);
  41.  
  42.             // Do it again for second URL.
  43.         WebClient scrape2 = new WebClient();
  44.  
  45.             string scrape2Value;
  46.  
  47.             string htmlCode2 = scrape2.DownloadString(webURL2);
  48.             HtmlAgilityPack.HtmlDocument doc2 = new HtmlAgilityPack.HtmlDocument();
  49.             doc2.LoadHtml(htmlCode2);
  50.             HtmlNode node2 = doc2.DocumentNode.SelectSingleNode(URL2Node);
  51.            
  52.         scrape2Value = (node2.InnerText);
  53.            
  54.         // Last time.
  55.         WebClient scrape3 = new WebClient();
  56.  
  57.             string scrape3Value;
  58.  
  59.             string htmlCode3 = scrape2.DownloadString(webURL3);
  60.             HtmlAgilityPack.HtmlDocument doc3 = new HtmlAgilityPack.HtmlDocument();
  61.             doc3.LoadHtml(htmlCode3);
  62.             HtmlNode node3 = doc3.DocumentNode.SelectSingleNode(URL3Node);
  63.            
  64.         scrape3Value = (node3.InnerText);      
  65.                      
  66.            
  67.         // Set the variables for the mailer fields from config file.
  68.         MailAddress to = new MailAddress(mailTo);
  69.             MailAddress from = new MailAddress(mailFrom);
  70.             MailMessage mail = new MailMessage(from, to);
  71.             mail.Subject = mailSubject;            
  72.             mail.Body =
  73.                 "Start of web scrape 1:"
  74.                 + Environment.NewLine
  75.                 + scrape1Value
  76.                 + Environment.NewLine
  77.                 + "Start of web scrape 2:"
  78.                 + Environment.NewLine
  79.                 + scrape2Value                
  80.                 + "Start of web scrape 3:"
  81.                 + Environment.NewLine                
  82.                 + scrape3Value;
  83.  
  84.         // Create the smtp object.
  85.             SmtpClient smtp = new SmtpClient();
  86.            
  87.         // Get relay details, then send.
  88.         smtp.Host = mailServer;
  89.             smtp.Port = 25;
  90.  
  91.         smtp.Send(mail);
  92.            
  93.         }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement