-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (77 loc) · 2.44 KB
/
Program.cs
File metadata and controls
96 lines (77 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using CommandLine;
using CommandLine.Text;
namespace SendPage
{
class Program
{
static void Main(string[] args)
{
// get command line options
var options = new Options();
if (CommandLine.Parser.Default.ParseArguments(args, options))
{
var html = ReadUrl(options.Url);
// send email
MailMessage message = new MailMessage() { Body = html, IsBodyHtml = true, };
if (!string.IsNullOrWhiteSpace(options.From)) { message.From = new MailAddress(options.From); }
if (!string.IsNullOrWhiteSpace(options.Subject)) { message.Subject = options.Subject; }
message.To.Add(options.To);
if (!string.IsNullOrWhiteSpace(options.Attachment))
{
Attachment attachment = new Attachment(options.Attachment);
message.Attachments.Add(attachment);
}
using (SmtpClient server = new SmtpClient())
{
if (!string.IsNullOrWhiteSpace(options.Login) && !string.IsNullOrWhiteSpace(options.Password))
{
server.Credentials = new System.Net.NetworkCredential(options.Login, options.Password);
}
server.Send(message);
}
}
}
public static string ReadUrl(string url)
{
using (WebClient client = new WebClient())
{
// read page
string htmlSource = client.DownloadString(url);
// inline css
var result = PreMailer.Net.PreMailer.MoveCssInline(htmlSource);
return result.Html;
}
}
// command line options
class Options
{
[Option('u', null, Required= true, HelpText = "Url")]
public string Url { get; set; }
[Option('a', null, Required= false, HelpText = "Attachment")]
public string Attachment { get; set; }
[Option('f', null, Required= false, HelpText = "From")]
public string From { get; set; }
[Option('t', null, Required= true, HelpText = "To")]
public string To { get; set; }
[Option('s', null, Required= false, HelpText = "Subject")]
public string Subject { get; set; }
[Option('l', null, Required= false, HelpText = "Login")]
public string Login { get; set; }
[Option('p', null, Required= false, HelpText = "Password")]
public string Password { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
}
}