Update email configuration

This commit is contained in:
2016-04-09 11:15:54 -04:00
parent 151f17943f
commit a5072850ea
2 changed files with 45 additions and 21 deletions
+41 -14
View File
@@ -2,9 +2,11 @@
using System.Configuration; using System.Configuration;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Mail; using System.Net.Mail;
using LeafWeb.Core.DAL; using LeafWeb.Core.DAL;
using LeafWeb.Core.Entities; using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;
using NLog; using NLog;
namespace LeafWeb.Web.Services namespace LeafWeb.Web.Services
@@ -23,7 +25,15 @@ namespace LeafWeb.Web.Services
{ {
_dataService = dataService; _dataService = dataService;
_smtpClient = new SmtpClient(); _smtpClient = new SmtpClient(
ConfigurationManager.AppSettings["SmtpHost"],
Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]));
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SmtpUserName"]))
_smtpClient.Credentials = new NetworkCredential(
ConfigurationManager.AppSettings["SmtpUserName"],
ConfigurationManager.AppSettings["SmtpPassword"]
);
_emaialFromAddress = ConfigurationManager.AppSettings["EmailFromAddress"]; _emaialFromAddress = ConfigurationManager.AppSettings["EmailFromAddress"];
} }
@@ -31,26 +41,32 @@ namespace LeafWeb.Web.Services
public EmailNotificationService() : this(new DataService()) public EmailNotificationService() : this(new DataService())
{ } { }
public void SendLeafWebError(int leafInputId, string errorMessage)
{
var leafInput = _dataService.GetLeafInput(leafInputId);
var body = $"Your leaf analysis job, {leafInput.Identifier}, encountered the following errors." + Environment.NewLine
+ "You will need to correct your input and resubmit." + Environment.NewLine + Environment.NewLine
+ errorMessage;
var message = new MailMessage(_emaialFromAddress, leafInput.Email, "LeafWeb processing error", body);
SendMessage(message);
}
public void SendLeafWebComplete(int leafInputId) public void SendLeafWebComplete(int leafInputId)
{ {
var leafInput = _dataService.GetLeafInput(leafInputId); var leafInput = _dataService.GetLeafInput(leafInputId);
var body = $"Your leaf analysis job, {leafInput.Identifier}, has completed." + Environment.NewLine;
var message = new MailMessage(_emaialFromAddress, leafInput.Email, "LeafWeb results", body);
var outputErrorMessage = leafInput.OutputErrorMessage;
if (outputErrorMessage != null)
SendLeafWebError(leafInput, outputErrorMessage.Contents.GetString());
else
SendLeafWebSuccess(leafInput);
}
private void SendLeafWebSuccess(LeafInput leafInput)
{
var fileStreams = var fileStreams =
(from outputFile in (from outputFile in
leafInput.OutputFiles leafInput.OutputFiles
select Tuple.Create(outputFile, new MemoryStream(outputFile.Contents))).ToList(); select Tuple.Create(outputFile, new MemoryStream(outputFile.Contents))).ToList();
var body = $"Your leaf analysis job, {leafInput.Identifier}, has completed." + Environment.NewLine;
if (leafInput.OutputWarningMessage != null)
body += "The following warning message was generated."
+ Environment.NewLine + Environment.NewLine
+ leafInput.OutputWarningMessage.Contents.GetString()
+ Environment.NewLine;
body += "Please see the attached results.";
var message = new MailMessage(_emaialFromAddress, leafInput.Email, "LeafWeb results", body);
try try
{ {
@@ -72,6 +88,17 @@ namespace LeafWeb.Web.Services
} }
} }
private void SendLeafWebError(LeafInput leafInput, string errorMessage)
{
var body = $"Your leaf analysis job, {leafInput.Identifier}, encountered the following errors." + Environment.NewLine
+ Environment.NewLine + Environment.NewLine
+ errorMessage
+ Environment.NewLine + Environment.NewLine
+ "You will need to correct your input and resubmit.";
var message = new MailMessage(_emaialFromAddress, leafInput.Email, "LeafWeb processing error", body);
SendMessage(message);
}
private void SendMessage(MailMessage mailMessage) private void SendMessage(MailMessage mailMessage)
{ {
try try
+4 -7
View File
@@ -24,14 +24,11 @@
<add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="TimeZoneId" value="US Eastern Standard Time" /> <add key="TimeZoneId" value="US Eastern Standard Time" />
<add key="EmailFromAddress" value="LeafWeb &lt;noreply@leafweb.org&gt;" /> <add key="EmailFromAddress" value="LeafWeb &lt;noreply@leafweb.org&gt;" />
<add key="SmtpHost" value="localhost"/>
<add key="SmtpPort" value="25"/>
<add key="SmtpUserName" value=""/>
<add key="SmtpPassword" value=""/>
</appSettings> </appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="LeafWeb &lt;noreply@leafweb.org&gt;">
<network host="localhost" />
</smtp>
</mailSettings>
</system.net>
<system.web> <system.web>
<compilation debug="true" targetFramework="4.5" /> <compilation debug="true" targetFramework="4.5" />
<!-- max 1GB --> <!-- max 1GB -->