Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
diev committed Jun 28, 2021
1 parent f742fbe commit e3a149d
Show file tree
Hide file tree
Showing 20 changed files with 476 additions and 415 deletions.
35 changes: 18 additions & 17 deletions Mailer/Lib/App.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2016-2021 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

Expand All @@ -13,7 +13,7 @@ namespace Lib
/// <summary>
/// Properties of application
/// </summary>
class App
public class App
{
#region Info
/// <summary>
Expand All @@ -27,38 +27,41 @@ class App
/// <summary>
/// Файл записи лога приложения
/// </summary>
public static string Log = Path.ChangeExtension(App.Exe, string.Format("{0:yyyyMMdd}.log", DateTime.Now));
public static string Log = Path.ChangeExtension(Exe, string.Format("{0:yyyyMMdd}.log", DateTime.Now));

static Assembly assembly = Assembly.GetCallingAssembly();
static AssemblyName assemblyName = assembly.GetName();
public static readonly string Name = assemblyName.Name;
static readonly Assembly _assembly = Assembly.GetCallingAssembly();
static readonly AssemblyName _assemblyName = _assembly.GetName();
public static readonly string Name = _assemblyName.Name;

// Major.Minor.Build.Revision
/// <summary>
/// Версия программы
/// </summary>
public static readonly Version Ver = assemblyName.Version;
public static readonly Version Ver = _assemblyName.Version;
/// <summary>
/// Название и версия программы в виде строки (если есть - с номером построения)
/// </summary>
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;
static readonly AssemblyDescriptionAttribute descriptionAttribute =
Attribute.GetCustomAttribute(_assembly, typeof(AssemblyDescriptionAttribute)) as AssemblyDescriptionAttribute;
public static readonly string Description = descriptionAttribute.Description;

static AssemblyCompanyAttribute companyAttribute = AssemblyCompanyAttribute.GetCustomAttribute(assembly, typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
static readonly AssemblyCompanyAttribute _companyAttribute =
Attribute.GetCustomAttribute(_assembly, typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
/// <summary>
/// Компания разработчика приложения
/// </summary>
public static readonly string Company = companyAttribute.Company;
public static readonly string Company = _companyAttribute.Company;

static AssemblyCopyrightAttribute copyrightAttribute = AssemblyCopyrightAttribute.GetCustomAttribute(assembly, typeof(AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute;
static readonly AssemblyCopyrightAttribute _copyrightAttribute =
Attribute.GetCustomAttribute(_assembly, typeof(AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute;
/// <summary>
/// Авторские права на приложение
/// </summary>
public static readonly string Copyright = copyrightAttribute.Copyright;
public static readonly string Copyright = _copyrightAttribute.Copyright;
#endregion Info

#region Paths
Expand Down Expand Up @@ -151,16 +154,14 @@ public static string ExitMessage(int code, string msg)
StringBuilder sb = new StringBuilder();
if (code > 0)
{
sb.Append("Exit ");
sb.Append(code);
sb.Append("Exit ").Append(code);
}
if (msg != null)
{
sb.Append(": ");
sb.Append(msg);
sb.Append(": ").Append(msg);
}
return sb.ToString();
}
#endregion Exit
}
}
}
14 changes: 7 additions & 7 deletions Mailer/Lib/AppTraceListener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2016-2021 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

Expand All @@ -9,7 +9,7 @@

namespace Lib
{
class AppTraceListener : DefaultTraceListener
public class AppTraceListener : DefaultTraceListener
{
public static TraceSwitch TraceConsole = new TraceSwitch("TraceConsole", "Trace level for console in config", "Error");
public static TraceSwitch TraceLog = new TraceSwitch("TraceLog", "Trace level for log in config", "Verbose");
Expand All @@ -20,7 +20,7 @@ public AppTraceListener()

public AppTraceListener(string file)
{
base.LogFileName = file;
LogFileName = file;

string path = Path.GetDirectoryName(file);
if (!Directory.Exists(path))
Expand All @@ -31,15 +31,15 @@ public AppTraceListener(string file)

public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
{
this.TraceEvent(eventCache, source, eventType, id, string.Format(format, args));
TraceEvent(eventCache, source, eventType, id, string.Format(format, args));
}

public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
{
string msg = BuildMessage(eventType, message);

//byte[] bytes = Encoding.GetEncoding(1251).GetBytes(sb.ToString().ToCharArray());
if (string.IsNullOrEmpty(base.LogFileName))
if (string.IsNullOrEmpty(LogFileName))
{
if (TraceConsole.TraceVerbose)
{
Expand All @@ -50,7 +50,7 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace
{
if (TraceLog.TraceVerbose)
{
File.AppendAllText(base.LogFileName, msg, Encoding.GetEncoding(1251));
File.AppendAllText(LogFileName, msg, Encoding.GetEncoding(1251));
}
}
}
Expand Down Expand Up @@ -94,4 +94,4 @@ private static string BuildMessage(TraceEventType eventType, string message)
return sb.AppendLine(message).ToString();
}
}
}
}
34 changes: 18 additions & 16 deletions Mailer/Lib/EMailMessage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2016-2021 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

using Mailer;

using System;
using System.Diagnostics;
using System.IO;
Expand All @@ -11,56 +13,52 @@

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

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

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

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

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

if (body.Length > 0)
{
if (mode.IndexOf(body[0]) > -1)
{
string list = Parameters.LIST;
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);
Body += BodyBuilder(m, item);
}
}
}
else
{
this.Body = body;
Body = body;
}
}

Expand All @@ -71,17 +69,20 @@ public EmailMessage(string recipients, string subject, string body, string[] fil
{
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);

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
Expand Down Expand Up @@ -209,6 +210,7 @@ static string GetBodyContent(string file, Encoding enc, int lines)
sb.AppendLine(content[i]);
}
}

FileInfo fi = new FileInfo(file);
sb.AppendFormat("---- eof {0}, {1}B, {2} ----", file, fi.Length, fi.LastWriteTime);
sb.AppendLine();
Expand Down
48 changes: 26 additions & 22 deletions Mailer/Lib/EMailSender.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2016-2021 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

using Mailer;

using System;
using System.ComponentModel;
using System.Diagnostics;
Expand All @@ -11,34 +13,36 @@

namespace Lib
{
class EmailSender
public class EmailSender
{
public string Host;
public int Port = 25;
public bool Ssl = false;
public int Port;
public bool Ssl;

public string Username;
public string Password;

public int Timeout = 5;
public int Timeout;

public EmailSender(string host, int port, bool ssl, string user, string pass)
public EmailSender()
{
Host = string.IsNullOrEmpty(host) ? Gateway.DefaultGateway() : host;
Port = port;
Ssl = ssl;
Username = user;
Password = Lib.Password.Decode(pass);
Host = Parameters.HOST ?? Gateway.DefaultGateway();
Port = Parameters.PORT;
Ssl = Parameters.SSL;
Username = Parameters.USER;
Password = Lib.Password.Decode(Parameters.PASS);
Timeout = Parameters.TIMEOUT;
}

public void SendWait(EmailMessage email)
{
SmtpClient client = new SmtpClient(Host, Port);

client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(Username, Password);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = Ssl;
SmtpClient client = new SmtpClient(Host, Port)
{
UseDefaultCredentials = false,
Credentials = new NetworkCredential(Username, Password),
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = Ssl
};
// client.Timeout = Timeout * 1000; // Ignored for Async

// The userState can be any object that allows your callback
Expand Down Expand Up @@ -89,12 +93,13 @@ public void SendWait(EmailMessage email)
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
string token = (string)e.UserState;

if (e.Cancelled)
{
Trace.TraceWarning("[{0}] Send canceled.", token);
}

if (e.Error != null)
{
Trace.TraceWarning("[{0}] {1}", token, e.Error.ToString());
Expand All @@ -105,10 +110,10 @@ private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs
}
}

class StatusChecker
protected class StatusChecker
{
private int invokeCount;
private int maxCount;
private readonly int maxCount;

public bool Canceled = false;
public bool TimedOut = false;
Expand All @@ -128,8 +133,7 @@ public void CheckStatus(Object stateInfo)

if (Console.KeyAvailable)
{
ConsoleKeyInfo keyInfo = new ConsoleKeyInfo();
keyInfo = Console.ReadKey(true);
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Escape)
{
// Reset the counter and signal the waiting thread.
Expand Down
4 changes: 2 additions & 2 deletions Mailer/Lib/Gateway.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2017-2021 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

Expand Down Expand Up @@ -38,4 +38,4 @@ public static string DefaultGateway()
return null;
}
}
}
}
6 changes: 3 additions & 3 deletions Mailer/Lib/Password.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2016-2017 Dmitrii Evdokimov. All rights reserved.
// Copyright (c) 2016-2021 Dmitrii Evdokimov. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// Source https://github.com/diev/

Expand All @@ -8,7 +8,7 @@

namespace Lib
{
class Password
public class Password
{
/// <summary>
/// The base encoding for text strings. 0: UTF8, 866: DOS, 1251: Windows, etc.
Expand Down Expand Up @@ -57,4 +57,4 @@ public static string Encode(string toEncode)
return Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
}
}
}
}
Loading

0 comments on commit e3a149d

Please sign in to comment.