Самым простым способом, очевидно, было бы просто сбросить их в настройках приложения, но это было бы не очень аккуратно:
<appSettings>
<add key="EmailSupport" value="support@somedomain.com" />
<add key="EmailSales" value="sales@somedomain.com" />
</appSettings>
Тогда в вашем коде вы просто делаете что-то вроде:
if (!string.IsNullOrEmpty(Request["recipient"])) {
string recipientEmail =
WebConfigurationManager.AppSettings["Email" + Request["recipient"]];
// Send your email to recipientEmail
}
Если вы хотите быть немного аккуратнее, вы можете создать пользовательский раздел конфигурации следующим образом (я боюсь, C #, но в документах есть VB ): 1011 *
namespace EmailSystem {
public class EmailRecipientsSection : ConfigurationSection {
[ConfigurationProperty("emailSender", IsRequired = true, IsKey = false)]
public string EmailSender {
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("emailRecipients", IsDefaultCollection = true)]
public EmailRecipientCollection EmailRecipients {
get {
var emailRecipientCollection =
(EmailRecipientCollection) base["emailRecipients"];
return emailRecipientCollection;
}
}
}
public class EmailRecipientCollection : ConfigurationElementCollection {
public EmailRecipientElement this[int index] {
get { return (EmailRecipientElement) BaseGet(index); }
set {
if (BaseGet(index) != null) {
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public new EmailRecipientElement this[string name] {
get { return (EmailRecipientElement) BaseGet(name); }
}
protected override ConfigurationElement CreateNewElement() {
return new EmailRecipientElement();
}
protected override object GetElementKey(ConfigurationElement element) {
return ((EmailRecipientElement) element).Name;
}
}
public class EmailRecipientElement : ConfigurationElement {
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name {
get { return (string) this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("emailAddress", IsRequired = true)]
public string EmailAddress {
get { return (string) this["emailAddress"]; }
set { this["emailAddress"] = value; }
}
}
}
Тогда в вашем web.config есть что-то вроде этого:
<configSections>
[...]
<section name="EmailSystem" type="EmailSystem, AssmeblyName" />
</configSections>
<EmailSystem emailSender="fromAddress@somedomain.com">
<emailRecipients>
<clear />
<add name="Support" emailAddress="support@somedomain.com" />
<add name="Sales" emailAddress="sales@somedomain.com" />
</emailRecipients>
</EmailSystem>
Тогда вы можете позвонить в это:
emailRecipient = Request["recipient"];
var emailSystem = ConfigurationManager.GetSection("EmailSystem")
as EmailRecipientsSection;
string recipientEmail = emailSystem.EmailRecipients[emailRecipient].emailAddress;
// send email to recipientEmail.