MailTransmitter reads SmtpServer from configuration data

This commit is contained in:
Rick Falkvinge
2013-11-20 08:01:58 +00:00
parent b3cddb24bb
commit 7d40813fd4
+26 -3
View File
@@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using Swarmops.Database;
using Swarmops.Logic.Communications;
using Swarmops.Logic.Communications.Transmission;
using Swarmops.Logic.Support;
using Swarmops.Logic.Swarm;
namespace Swarmops.Utility.Communications
@@ -13,6 +15,9 @@ namespace Swarmops.Utility.Communications
{
// To be implemented
private static string _smtpServerCache = string.Empty;
private static DateTime _cacheReloadTime = DateTime.MaxValue;
public void Transmit(PayloadEnvelope envelope, Person person)
{
if (envelope.PayloadClass != "Swarmops.Logic.Communications.Transmission.NotificationPayload")
@@ -35,8 +40,25 @@ namespace Swarmops.Utility.Communications
mail.Body = comm[CommRenderPart.BodyText];
mail.SubjectEncoding = Encoding.UTF8;
mail.BodyEncoding = Encoding.UTF8;
SmtpClient mailClient = new SmtpClient("192.168.80.204"); // Fix this - for development use only
string smtpServer = _smtpServerCache;
DateTime now = DateTime.Now;
if (now > _cacheReloadTime)
{
smtpServer = _smtpServerCache = Persistence.Key ["SmtpServer"];
_cacheReloadTime = now.AddMinutes(5);
}
if (string.IsNullOrEmpty(smtpServer))
{
smtpServer = "192.168.80.204"; // For development use only - invalidate cache instead of this, forcing re-reload
}
SmtpClient mailClient = new SmtpClient(smtpServer);
// TODO: SMTP Server login credentials
try
{
@@ -44,7 +66,8 @@ namespace Swarmops.Utility.Communications
}
catch (Exception e)
{
throw new OutboundCommTransmitException("Cannot send mail to " + person.Mail, e);
_cacheReloadTime = DateTime.MaxValue;
throw new OutboundCommTransmitException("Cannot send mail to " + person.Mail, e);
}
}