SSL support for sending mail

This commit is contained in:
2016-03-26 20:12:00 +01:00
parent d3079cce59
commit c25c8365ae
6 changed files with 118 additions and 19 deletions
+1 -1
View File
@@ -25,7 +25,7 @@
<LangVersion>4</LangVersion>
<Externalconsole>true</Externalconsole>
<DebugSymbols>true</DebugSymbols>
<Commandlineparameters>--config ~/code/ntpa/example/ntpa.graph.conf --temp /tmp/</Commandlineparameters>
<Commandlineparameters>--config ~/code/ntpa/example/ntpa.web.prod.conf --temp /tmp/</Commandlineparameters>
<AssemblyName>ntpa</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Server|AnyCPU' ">
@@ -25,12 +25,13 @@
// THE SOFTWARE.
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Mail;
using System.Text;
using Ntp.Analyzer.Config;
using Ntp.Analyzer.Config.Root;
using Ntp.Analyzer.Log;
using Ntp.Process;
using Ntp.Analyzer.Config.Root;
namespace Ntp.Analyzer.Process.Description
{
@@ -56,6 +57,13 @@ namespace Ntp.Analyzer.Process.Description
jobs = new List<Ntp.Process.Job> ();
client = new SmtpClient (config.SmptServer, config.SmtpPort);
if (config.SmptUser != null) {
client.Credentials = new NetworkCredential(
config.SmptUser, config.SmptPass ?? string.Empty);
}
client.EnableSsl = config.EnableSsl;
}
public override bool SingleThread {
@@ -42,7 +42,10 @@ namespace Ntp.Analyzer.Config.Root
string subject,
string sender,
string server,
int port)
int port,
string user,
string pass,
bool ssl)
: base(configName, indentLevel, frequency, initialRun, fixedRun)
{
this.mail = mail;
@@ -50,6 +53,9 @@ namespace Ntp.Analyzer.Config.Root
this.sender = sender;
this.server = server;
this.port = port;
this.user = user;
this.pass = pass;
this.ssl = ssl;
}
public const string Identifier = "Notify";
@@ -61,14 +67,21 @@ namespace Ntp.Analyzer.Config.Root
"Mail",
"Subject",
"Sender",
"Smtp",
"Port"
"SmtpHost",
"SmtpPort",
"SmtpUser",
"SmtpPass",
"EnableSsl"
};
private readonly string mail;
private readonly int port;
private readonly string sender;
private readonly string server;
private readonly string subject;
private readonly string user;
private readonly string pass;
private readonly bool ssl;
public string Mail {
get { return mail; }
@@ -90,6 +103,18 @@ namespace Ntp.Analyzer.Config.Root
get { return port; }
}
public string SmptUser {
get { return user; }
}
public string SmptPass {
get { return pass; }
}
public bool EnableSsl {
get { return ssl; }
}
protected override IEnumerable<Object> Items {
get {
return new object[] {
@@ -101,7 +126,10 @@ namespace Ntp.Analyzer.Config.Root
subject,
sender,
server,
port
port,
user,
pass,
ssl ? 1 : 0
};
}
}
@@ -127,8 +155,11 @@ namespace Ntp.Analyzer.Config.Root
GetOptionString (options, "Mail", Params [0], true),
GetOptionString (options, "Subject", Params [0], true),
GetOptionString (options, "Sender", Params [0], true),
GetOptionString (options, "Smtp", Params [0], true),
GetOptionInteger (options, "Port", Params [0], false, 25)
GetOptionString (options, "SmtpHost", Params [0], true),
GetOptionInteger (options, "SmtpPort", Params [0], false, 25),
GetOptionString (options, "SmtpUser", Params [0], false),
GetOptionString (options, "SmtpPass", Params [0], false),
GetOptionBoolean (options, "EnableSsl", Params [0], false)
);
}
}
+1 -1
View File
@@ -36,7 +36,7 @@ Server {
HostID 1
# Domain name of hosting NTP server
HostAddress 192.168.222.114
HostAddress ntp1.domain.org
# The utility used to query ntpd. Use ntpdc for old hosts.
HostType ntpq
+46 -3
View File
@@ -29,8 +29,8 @@ Listener {
Log {
# Levels: error, warn, notice, info, debug and trace
Severity trace
File /var/log/ntpa.stat.log
Severity info
File File /var/log/ntpa/ntpa.stat.log
}
# Synchronize timestamps for readings across different NTP servers/peers
@@ -82,4 +82,47 @@ Server {
# PeerStats {
# Frequency stat1
# }
#}
#}
Notify ntpstat-summery {
# Indicate if a test mail should be sent immediately after start up
InitialRun 1
# Send a mail every 1440 minutes or 24 hours
Frequency 1440
# Send the mail to this address
Mail root@example.com
# Subject of mail
Subject NTP Analyzer stats daily run - stat
# Sender of mail
Sender ntpa-xxx@gmail.com
# Sending smtp host
SmtpHost smtp.gmail.com
# Port on smtp host. Default is 25.
SmtpPort 587
# Username for SMTP server. Should be used with SSL turned on.
SmtpUser xxx@gmail.com
# Password for SMTP server. Should be used with SSL turned on.
SmtpPass xxx
# Set to 1 when using RFC 3207 SMTP. Default is disabled.
EnableSsl 1
## Does SSL work for SMTP, like GMail ?
## Yes it does. First you must import the root certificates using the mozroots tool:
# mozroots --import --ask-remove
## Note that if you are using a web application (i.e. not the current user) you must add the --machine option like this:
# mozroots --import --ask-remove --machine
## Next you need to import the intermediate certificates. You can do this by using the certmgr tool to connect to the SSL server. E.g.
# certmgr -ssl smtps://smtp.gmail.com:465
## Use the -m option to import the certificates into the machine store if required.
## Finally you need to make sure to use the SSL-enabled port in your application. This is generally 465 or 587 instead of port 25.
## Source: http://www.mono-project.com/docs/faq/security/
}
+24 -7
View File
@@ -18,16 +18,36 @@ Database {
# Password for the user
Pass xxxx
# Set to 1 to automatically create all tables and data.
Create 1
}
# OPTIONAL
Log {
# Levels: error, warn, notice, info, debug and trace
Severity info
File /var/log/ntpa/ntpa.log
File /var/log/ntpa/ntpa.web.log
}
Notify ntpstat-summery {
# Indicate if a test mail should be sent immediately after start up
InitialRun 1
# Send a mail every 1440 minutes or 24 hours
Frequency 1440
# Send the mail to this address
Mail webmaster@example.com
# Subject of mail
Subject NTP Analyzer stats daily run - web
# Sender of mail
Sender ntpa@example.com
# Sending smtp host
SmtpHost 127.0.0.1
# Port on smtp host. Default is 25.
#SmtpPort 25
}
# Multiple server configurations are available
@@ -41,9 +61,6 @@ Server {
# The utility used to query ntpd. Use ntpdc for old hosts.
HostType ntpq
# Domain name of hosting NTP server
HostAddress ntp1.domain.org
# NTP configuration file from host
ConfigFile /usr/local/etc/ntpa/ntp1.conf