Skip to content

Commit

Permalink
Add detecting of Default Gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
diev committed Jun 13, 2017
1 parent 62b2bd8 commit 0d60755
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 192 deletions.
16 changes: 7 additions & 9 deletions Mailer/Lib/App.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

Expand Down Expand Up @@ -33,19 +33,17 @@ class App
static AssemblyName assemblyName = assembly.GetName();
public static readonly string Name = assemblyName.Name;

// Major.Minor.Build.Revision
/// <summary>
/// Версия приложения вида 1.1.60805.0
/// Версия программы
/// </summary>
public static readonly Version Ver = assemblyName.Version;
/// <summary>
/// Дата версии приложения
/// Название и версия программы в виде строки (если есть - с номером построения)
/// </summary>
public static readonly DateTime Dated = DateTime.Parse(string.Format("201{0}-{1}-{2}", Ver.Build / 10000, Ver.Build % 10000 / 100, Ver.Build % 100));
/// <summary>
/// Строка названием и версией приложения
/// </summary>
public static readonly string Version = string.Format("{0} v{1}", Name, Ver); //Ver.ToString(2), Ver.Build, Ver.Revision

public static readonly string Version = string.Format("{0} v{1}", Name, Ver.ToString(3)) +
(Ver.Revision > 0 ? " build " + Ver.Revision.ToString() : string.Empty);

//public static readonly string attribute = (attribute == null) ? string.Empty : attribute;
static AssemblyDescriptionAttribute descriptionAttribute = AssemblyDescriptionAttribute.GetCustomAttribute(assembly, typeof(AssemblyDescriptionAttribute)) as AssemblyDescriptionAttribute;
public static readonly string Description = descriptionAttribute.Description;
Expand Down
2 changes: 1 addition & 1 deletion Mailer/Lib/AppTraceListener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

Expand Down
220 changes: 220 additions & 0 deletions Mailer/Lib/EMailMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

using System;
using System.Diagnostics;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;

namespace Lib
{
class EmailMessage : MailMessage
{
public EmailMessage()
{
}

public EmailMessage(string recipients, string subject, string body, string[] files,
string mode, string list)
{
this.From = new MailAddress("noreply", App.Name, Encoding.UTF8);

this.To.Add(recipients.Replace(';', ','));
// this.CC
// this.Bcc
// this.ReplyToList;

// this.IsBodyHtml = true;
// this.Priority = MailPriority.High;

if (subject.Length > 0)
{
int m = mode.IndexOf(subject[0]);
if (m > -1)
{
this.Subject = SubjBuilder(m, subject);
}
else
{
this.Subject = subject;
}
}

if (body.Length > 0)
{
if (mode.IndexOf(body[0]) > -1)
{
string[] bodyList = body.Split(list.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
body = string.Empty;
foreach (string item in bodyList)
{
int m = mode.IndexOf(item[0]);
if (m > -1)
{
this.Body += BodyBuilder(m, item);
}
}
}
else
{
this.Body = body;
}
}

foreach (string file in files)
{
FileInfo fi = new FileInfo(file.Trim());
if (fi.Exists)
{
Attachment attachment = new Attachment(fi.FullName);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = fi.CreationTime;
disposition.ModificationDate = fi.LastWriteTime;
disposition.ReadDate = fi.LastAccessTime;
this.Attachments.Add(attachment);
}
else
{
Trace.TraceWarning("Attachment file " + fi.FullName + " not found!");
}
}
}
static string SubjBuilder(int mode, string item)
{
int line = -1; // the last line by default
string file = item.Substring(1);

int pos = item.LastIndexOf(':');
if (pos > 2) // *C:\
{
file = item.Substring(1, pos - 1);
int.TryParse(item.Substring(pos + 1), out line);
}

Encoding enc = GetModeEncoding(mode);
return GetSubjContent(file, enc, line);
}

static string BodyBuilder(int mode, string item)
{
int lines = 0; // all content
string file = item.Substring(1);

int pos = item.LastIndexOf(':');
if (pos > 2) // *C:\
{
file = item.Substring(1, pos - 1);
int.TryParse(item.Substring(pos + 1), out lines);
}

Encoding enc = GetModeEncoding(mode);
return GetBodyContent(file, enc, lines);
}

static Encoding GetModeEncoding(int mode)
{
switch (mode)
{
case 0: // "-"
return Encoding.GetEncoding(866);

case 1: // "="
return Encoding.GetEncoding(1251);

default: // "*"
return Encoding.UTF8;
}
}

static string GetSubjContent(string file, Encoding enc, int line)
{
if (!File.Exists(file))
{
return string.Format("файл {0} не найден", file);
}

string[] content = File.ReadAllLines(file, enc);

if (line > 0)
{
return content[line - 1];
}
else
{
return content[content.Length + line];
}
}

static string GetBodyContent(string file, Encoding enc, int lines)
{
if (!File.Exists(file))
{
return string.Format("---- файл {0} не найден ----\n", file);
}

if (lines == 0) // entire file
{
return File.ReadAllText(file, enc);
}

if (lines == 1 || lines == -1)
{
return GetSubjContent(file, enc, lines);
}

string[] content = File.ReadAllLines(file, enc);
int L = content.Length;

int n = lines > 0 ? lines : -lines;
n = (n < L) ? n : L;
StringBuilder sb = new StringBuilder(n * 100);
sb.AppendFormat("---- файл {0}", file);

if (lines > 0)
{
if (n < L)
{
sb.AppendFormat(", первые {0} из {1} строк ----", n, L);
sb.AppendLine();
}
else
{
sb.AppendLine(" ----");
}

for (int i = 0; i < n; i++)
{
sb.AppendLine(content[i]);
}

}
else
{
if (n < L)
{
sb.AppendFormat(", последние {0} из {1} строк ----", n, L);
sb.AppendLine();
}
else
{
sb.AppendLine(" ----");
sb.AppendLine();
}

for (int i = L - n; i < L; i++)
{
sb.AppendLine(content[i]);
}
}
FileInfo fi = new FileInfo(file);
sb.AppendFormat("---- eof {0}, {1}B, {2} ----", file, fi.Length, fi.LastWriteTime);
sb.AppendLine();
sb.AppendLine();

return sb.ToString();
}
}
}
51 changes: 5 additions & 46 deletions Mailer/Lib/Mail.cs → Mailer/Lib/EMailSender.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,17 @@
// Copyright (c) 2016 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading;

namespace Lib
{
class EmailMessage : MailMessage
{
public EmailMessage() { }

public EmailMessage(string recipients, string subject, string body, string[] files)
{
this.From = new MailAddress("noreply", App.Name, Encoding.UTF8);

this.To.Add(recipients.Replace(';', ','));
// this.CC
// this.Bcc
// this.ReplyToList;

this.Subject = subject;
this.Body = body;
// this.IsBodyHtml = true;
// this.Priority = MailPriority.High;

foreach (string file in files)
{
FileInfo fi = new FileInfo(file.Trim());
if (fi.Exists)
{
Attachment attachment = new Attachment(fi.FullName);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = fi.CreationTime;
disposition.ModificationDate = fi.LastWriteTime;
disposition.ReadDate = fi.LastAccessTime;
this.Attachments.Add(attachment);
}
else
{
Trace.TraceWarning("Attachment file " + fi.FullName + " not found!");
}
}
}
}

class EmailServer
class EmailSender
{
public string Host;
public int Port = 25;
Expand All @@ -63,13 +22,13 @@ class EmailServer

public int Timeout = 5;

public EmailServer(string host, int port, bool ssl, string user, string pass)
public EmailSender(string host, int port, bool ssl, string user, string pass)
{
Host = host;
Host = string.IsNullOrEmpty(host) ? Gateway.DefaultGateway() : host;
Port = port;
Ssl = ssl;
Username = user;
Password = pass;
Password = Lib.Password.Decode(pass);
}

public void SendWait(EmailMessage email)
Expand Down
41 changes: 41 additions & 0 deletions Mailer/Lib/Gateway.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2017 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

using System.Net.NetworkInformation;

namespace Lib
{
public class Gateway
{
public static string DefaultGateway()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
if (adapter.OperationalStatus != OperationalStatus.Up)
continue;
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
continue;

IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
if (adapterProperties == null)
continue;

GatewayIPAddressInformationCollection gateways = adapterProperties.GatewayAddresses;
if (gateways.Count > 0)
{
foreach (GatewayIPAddressInformation address in gateways)
{
string gw = address.Address.ToString();
if (gw.Equals("0.0.0.0"))
continue;

return gw;
}
}
}
return null;
}
}
}
2 changes: 1 addition & 1 deletion Mailer/Lib/Password.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

Expand Down
4 changes: 3 additions & 1 deletion Mailer/Mailer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
<ItemGroup>
<Compile Include="Lib\App.cs" />
<Compile Include="Lib\AppTraceListener.cs" />
<Compile Include="Lib\Mail.cs" />
<Compile Include="Lib\Gateway.cs" />
<Compile Include="Lib\EmailSender.cs" />
<Compile Include="Lib\EmailMessage.cs" />
<Compile Include="Lib\Password.cs" />
<Compile Include="Parameters.cs" />
<Compile Include="Program.cs" />
Expand Down
Loading

0 comments on commit 0d60755

Please sign in to comment.