Allow SMTP settings to be set in web.config

This commit is contained in:
Sebastien Ros
2014-03-07 16:20:13 -08:00
parent 584981efe3
commit 22f2fe1c15
6 changed files with 64 additions and 19 deletions

View File

@@ -1,4 +1,8 @@
using Orchard.ContentManagement;
using System.ComponentModel;
using System.Configuration;
using System.Net.Configuration;
using System.Net.Mail;
using Orchard.ContentManagement;
using System;
using Orchard.ContentManagement.Utilities;
@@ -46,9 +50,23 @@ namespace Orchard.Email.Models {
}
public bool IsValid() {
return !String.IsNullOrWhiteSpace(Host)
&& Port > 0
&& !String.IsNullOrWhiteSpace(Address);
var section = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
if (section != null && !String.IsNullOrWhiteSpace(section.Network.Host)) {
return true;
}
// establish if the settings are defined in the web.config file
var smtpClient = new SmtpClient();
if (!String.IsNullOrWhiteSpace(Address)) {
return false;
}
if (!String.IsNullOrWhiteSpace(Host) && Port == 0) {
return false;
}
return String.IsNullOrWhiteSpace(smtpClient.Host);
}
}
}

View File

@@ -54,6 +54,9 @@
<HintPath>..\..\..\..\lib\newtonsoft.json\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration">
<Private>True</Private>
</Reference>
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.Web.DynamicData" />

View File

@@ -10,7 +10,7 @@ using Orchard.Messaging.Services;
using Orchard.Messaging.Models;
namespace Orchard.Email.Services {
[Obsolete]
[Obsolete("Use ISmtpChannel instead")]
public class EmailMessagingChannel : IMessagingChannel {
private readonly IOrchardServices _orchardServices;
@@ -54,7 +54,7 @@ namespace Orchard.Email.Services {
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
context.MailMessage.From = new MailAddress(smtpSettings.Address);
context.MailMessage.IsBodyHtml = context.MailMessage.Body != null && context.MailMessage.Body.Contains("<") && context.MailMessage.Body.Contains(">");
context.MailMessage.IsBodyHtml = !String.IsNullOrWhiteSpace(context.MailMessage.Body) && context.MailMessage.Body.Contains("<") && context.MailMessage.Body.Contains(">");
try {
smtpClient.Send(context.MailMessage);

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Net.Mail;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.Email.Models;
@@ -21,10 +22,12 @@ namespace Orchard.Email.Services {
var workContext = _orchardServices.WorkContext;
var smtpSettings = workContext.CurrentSite.As<SmtpSettingsPart>();
var smtpClient = new SmtpClient();
if (smtpSettings == null || !smtpSettings.IsValid()) {
var urlHelper = new UrlHelper(workContext.HttpContext.Request.RequestContext);
var url = urlHelper.Action("Email", "Admin", new {Area = "Settings"});
yield return new NotifyEntry {Message = T("The <a href=\"{0}\">SMTP settings</a> needs to be configured.", url), Type = NotifyType.Warning};
yield return new NotifyEntry {Message = T("The <a href=\"{0}\">SMTP settings</a> need to be configured.", url), Type = NotifyType.Warning};
}
}
}

View File

@@ -1,10 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Logging;
@@ -61,12 +61,16 @@ namespace Orchard.Email.Services {
}));
var mailMessage = new MailMessage {
From = new MailAddress(_smtpSettings.Address),
Subject = emailMessage.Subject,
Body = _shapeDisplay.Display(template),
IsBodyHtml = true
};
var section = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
mailMessage.From = !String.IsNullOrWhiteSpace(_smtpSettings.Address)
? new MailAddress(_smtpSettings.Address)
: new MailAddress(section.From);
try {
foreach (var recipient in emailMessage.Recipients.Split(new [] {',', ';'}, StringSplitOptions.RemoveEmptyEntries)) {
mailMessage.To.Add(new MailAddress(recipient));
@@ -80,8 +84,13 @@ namespace Orchard.Email.Services {
}
private SmtpClient CreateSmtpClient() {
// if no properties are set in the dashboard, use the web.config value
if (String.IsNullOrWhiteSpace(_smtpSettings.Host)) {
return new SmtpClient();
}
var smtpClient = new SmtpClient {
UseDefaultCredentials = !_smtpSettings.RequireCredentials
UseDefaultCredentials = !_smtpSettings.RequireCredentials,
};
if (!smtpClient.UseDefaultCredentials && !String.IsNullOrWhiteSpace(_smtpSettings.UserName)) {

View File

@@ -1,27 +1,37 @@
@model Orchard.Email.Models.SmtpSettingsPart
@using System.Net.Mail;
@using System.Configuration
@using System.Net.Mail
@model Orchard.Email.Models.SmtpSettingsPart
@{
var smtpClient = new SmtpClient();
dynamic section = ConfigurationManager.GetSection("system.net/mailSettings/smtp");
}
<fieldset>
<legend>@T("Email")</legend>
<div>
<label for="@Html.FieldIdFor(m => m.Address)">@T("Sender email address")</label>
@Html.EditorFor(m => m.Address)
@Html.TextBoxFor(m => m.Address, new { @class = "text medium", placeholder = (string) section.From })
@Html.ValidationMessage("Address", "*")
<span class="hint">@T("The default email address to use as a sender.")</span>
</div>
<div>
<label for="@Html.FieldIdFor(m => m.Host)">@T("Host name")</label>
@Html.EditorFor(m => m.Host)
@Html.ValidationMessage("Host", "*")
@Html.TextBoxFor(m => m.Host, new { placeholder = smtpClient.Host, @class = "text medium" })
@Html.ValidationMessage("Host", "*")
<span class="hint">@T("The SMTP server domain, e.g. <i>smtp.mailprovider.com</i>.")</span>
</div>
<div>
<label for="@Html.FieldIdFor(m => m.Port)">@T("Port number")</label>
@Html.EditorFor(m => m.Port)
@Html.TextBoxFor(m => m.Port, new { placeholder = smtpClient.Port, @class = "text small" })
@Html.ValidationMessage("Port", "*")
<span class="hint">@T("The SMTP server port, usually 25.")</span>
</div>
<div>
@Html.EditorFor(m => m.EnableSsl)
<label for="@Html.FieldIdFor(m => m.EnableSsl)" class="forcheckbox">@T("Enable SSL communications")</label>
@Html.ValidationMessage("EnableSsl", "*")
<span class="hint">@T("Check if the SMTP server requires SSL communications.")</span>
</div>
<div>
@Html.EditorFor(m => m.RequireCredentials)
@@ -31,13 +41,15 @@
<div data-controllerid="@Html.FieldIdFor(m => m.RequireCredentials)">
<div>
<label for="@Html.FieldIdFor(m => m.UserName)">@T("User name")</label>
@Html.EditorFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName, new { @class = "text" })
@Html.ValidationMessage("UserName", "*")
<span class="hint">@T("The username for authentication.")</span>
</div>
<div>
<label for="@Html.FieldIdFor(m => m.Password)">@T("Password")</label>
@Html.PasswordFor(m => m.Password)
@Html.TextBoxFor(m => m.Password, new { type = "password", @class = "text medium" })
@Html.ValidationMessage("Password", "*")
<span class="hint">@T("The password for authentication.")</span>
</div>
</div>
</fieldset>