Files
Orchard/src/Orchard.Web/Modules/Orchard.Email/Models/SmtpSettingsPart.cs
2014-03-11 15:02:08 -07:00

72 lines
2.2 KiB
C#

using System.ComponentModel;
using System.Configuration;
using System.Net.Configuration;
using System.Net.Mail;
using Orchard.ContentManagement;
using System;
using Orchard.ContentManagement.Utilities;
namespace Orchard.Email.Models {
public class SmtpSettingsPart : ContentPart {
private readonly ComputedField<string> _password = new ComputedField<string>();
public ComputedField<string> PasswordField {
get { return _password; }
}
public string Address {
get { return this.Retrieve(x => x.Address); }
set { this.Store(x => x.Address, value); }
}
public string Host {
get { return this.Retrieve(x => x.Host); }
set { this.Store(x => x.Host, value); }
}
public int Port {
get { return this.Retrieve(x => x.Port, 25); }
set { this.Store(x => x.Port, value); }
}
public bool EnableSsl {
get { return this.Retrieve(x => x.EnableSsl); }
set { this.Store(x => x.EnableSsl, value); }
}
public bool RequireCredentials {
get { return this.Retrieve(x => x.RequireCredentials); }
set { this.Store(x => x.RequireCredentials, value); }
}
public string UserName {
get { return this.Retrieve(x => x.UserName); }
set { this.Store(x => x.UserName, value); }
}
public string Password {
get { return _password.Value; }
set { _password.Value = value; }
}
public bool IsValid() {
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 true;
}
}
}