Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Mail;
- using System.Net;
- using System.Configuration;
- using HtmlAgilityPack;
- namespace WebscrapeMailer
- {
- class Program
- {
- static void Main()
- {
- // Store configurable values from App.config.
- string webURL1 = ConfigurationManager.AppSettings["config.webURL1"];
- string webURL2 = ConfigurationManager.AppSettings["config.webURL2"];
- string webURL3 = ConfigurationManager.AppSettings["config.webURL3"];
- string URL1Node = ConfigurationManager.AppSettings["config.URL1Node"];
- string URL2Node = ConfigurationManager.AppSettings["config.URL2Node"];
- string URL3Node = ConfigurationManager.AppSettings["config.URL3Node"];
- string mailTo = ConfigurationManager.AppSettings["config.MailTo"];
- string mailFrom = ConfigurationManager.AppSettings["config.MailFrom"];
- string mailSubject = ConfigurationManager.AppSettings["config.MailSubject"];
- string mailServer = ConfigurationManager.AppSettings["config.MailServer"];
- // Create WebClient object for first webscrape.
- WebClient scrape1 = new WebClient();
- string scrape1Value;
- // Download HTML from first webpage and parse text at specified node
- // then give string to first scrape value.
- string htmlCode1 = scrape1.DownloadString(webURL1);
- HtmlAgilityPack.HtmlDocument doc1 = new HtmlAgilityPack.HtmlDocument();
- doc1.LoadHtml(htmlCode1);
- HtmlNode node1 = doc1.DocumentNode.SelectSingleNode(URL1Node);
- scrape1Value = (node1.InnerText);
- // Do it again for second URL.
- WebClient scrape2 = new WebClient();
- string scrape2Value;
- string htmlCode2 = scrape2.DownloadString(webURL2);
- HtmlAgilityPack.HtmlDocument doc2 = new HtmlAgilityPack.HtmlDocument();
- doc2.LoadHtml(htmlCode2);
- HtmlNode node2 = doc2.DocumentNode.SelectSingleNode(URL2Node);
- scrape2Value = (node2.InnerText);
- // Last time.
- WebClient scrape3 = new WebClient();
- string scrape3Value;
- string htmlCode3 = scrape2.DownloadString(webURL3);
- HtmlAgilityPack.HtmlDocument doc3 = new HtmlAgilityPack.HtmlDocument();
- doc3.LoadHtml(htmlCode3);
- HtmlNode node3 = doc3.DocumentNode.SelectSingleNode(URL3Node);
- scrape3Value = (node3.InnerText);
- // Set the variables for the mailer fields from config file.
- MailAddress to = new MailAddress(mailTo);
- MailAddress from = new MailAddress(mailFrom);
- MailMessage mail = new MailMessage(from, to);
- mail.Subject = mailSubject;
- mail.Body =
- "Start of web scrape 1:"
- + Environment.NewLine
- + scrape1Value
- + Environment.NewLine
- + "Start of web scrape 2:"
- + Environment.NewLine
- + scrape2Value
- + "Start of web scrape 3:"
- + Environment.NewLine
- + scrape3Value;
- // Create the smtp object.
- SmtpClient smtp = new SmtpClient();
- // Get relay details, then send.
- smtp.Host = mailServer;
- smtp.Port = 25;
- smtp.Send(mail);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement