190 lines
4.1 KiB
C#
190 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Mail;
|
|
using System.Text.RegularExpressions;
|
|
namespace SPSolutions.Net.Mail
|
|
{
|
|
public class MailDefinition
|
|
{
|
|
private string _bodyContainerName;
|
|
private string _cc;
|
|
private string _from;
|
|
private bool _isBodyHtml;
|
|
private MailPriority _priority;
|
|
private string _subject;
|
|
private string _body;
|
|
public string BodyContainerName
|
|
{
|
|
get
|
|
{
|
|
return this._bodyContainerName;
|
|
}
|
|
set
|
|
{
|
|
this._bodyContainerName = value;
|
|
}
|
|
}
|
|
public string Body
|
|
{
|
|
get
|
|
{
|
|
return this._body;
|
|
}
|
|
set
|
|
{
|
|
this._body = value;
|
|
}
|
|
}
|
|
public string CC
|
|
{
|
|
get
|
|
{
|
|
return this._cc;
|
|
}
|
|
set
|
|
{
|
|
this._cc = value;
|
|
}
|
|
}
|
|
public string From
|
|
{
|
|
get
|
|
{
|
|
return this._from;
|
|
}
|
|
set
|
|
{
|
|
this._from = value;
|
|
}
|
|
}
|
|
public bool IsBodyHtml
|
|
{
|
|
get
|
|
{
|
|
return this._isBodyHtml;
|
|
}
|
|
set
|
|
{
|
|
this._isBodyHtml = value;
|
|
}
|
|
}
|
|
public MailPriority Priority
|
|
{
|
|
get
|
|
{
|
|
return this._priority;
|
|
}
|
|
set
|
|
{
|
|
this._priority = value;
|
|
}
|
|
}
|
|
public string Subject
|
|
{
|
|
get
|
|
{
|
|
return this._subject;
|
|
}
|
|
set
|
|
{
|
|
this._subject = value;
|
|
}
|
|
}
|
|
public MailMessage CreateMailMessage(string recipients, IDictionary<string, string> replacements)
|
|
{
|
|
if (string.IsNullOrEmpty(this._body))
|
|
{
|
|
return this.CreateMailMessage(MailBodyManager.Provider, recipients, replacements);
|
|
}
|
|
return MailDefinition.CreateMailMessage(this.From, recipients, this.CC, this.Subject, this.Body, this.Priority, this.IsBodyHtml, replacements);
|
|
}
|
|
public MailMessage CreateMailMessage(string providerName, string recipients, IDictionary<string, string> replacements)
|
|
{
|
|
return this.CreateMailMessage(MailBodyManager.Providers[providerName], recipients, replacements);
|
|
}
|
|
public MailMessage CreateMailMessage(MailBodyProvider provider, string recipients, IDictionary<string, string> replacements)
|
|
{
|
|
return this.CreateMailMessage(recipients, replacements, provider.RetrieveMailBody(this.BodyContainerName));
|
|
}
|
|
public MailMessage CreateMailMessage(string recipients, IDictionary<string, string> replacements, string body)
|
|
{
|
|
string from = this.From;
|
|
MailMessage mailMessage = null;
|
|
try
|
|
{
|
|
mailMessage = new MailMessage(from, recipients);
|
|
if (!string.IsNullOrEmpty(this.CC))
|
|
{
|
|
mailMessage.CC.Add(this.CC);
|
|
}
|
|
if (!string.IsNullOrEmpty(this.Subject))
|
|
{
|
|
mailMessage.Subject = this.Subject;
|
|
}
|
|
mailMessage.Priority = this.Priority;
|
|
if (replacements != null && !string.IsNullOrEmpty(body))
|
|
{
|
|
foreach (string current in replacements.Keys)
|
|
{
|
|
string text = replacements[current];
|
|
if (current == null || text == null)
|
|
{
|
|
throw new ArgumentException("MailDefinition_InvalidReplacements");
|
|
}
|
|
body = Regex.Replace(body, current, text, RegexOptions.IgnoreCase);
|
|
}
|
|
}
|
|
mailMessage.Body = body;
|
|
mailMessage.IsBodyHtml = this.IsBodyHtml;
|
|
}
|
|
catch
|
|
{
|
|
if (mailMessage != null)
|
|
{
|
|
mailMessage.Dispose();
|
|
}
|
|
throw;
|
|
}
|
|
return mailMessage;
|
|
}
|
|
public static MailMessage CreateMailMessage(string from, string recipients, string cc, string subject, string body, MailPriority priority, bool isBodyHtml, IDictionary<string, string> replacements)
|
|
{
|
|
MailMessage mailMessage = null;
|
|
try
|
|
{
|
|
mailMessage = new MailMessage(from, recipients);
|
|
mailMessage.Priority = priority;
|
|
mailMessage.IsBodyHtml = isBodyHtml;
|
|
if (!string.IsNullOrEmpty(cc))
|
|
{
|
|
mailMessage.CC.Add(cc);
|
|
}
|
|
if (replacements != null)
|
|
{
|
|
foreach (string current in replacements.Keys)
|
|
{
|
|
string text = replacements[current];
|
|
if (current == null || text == null)
|
|
{
|
|
throw new ArgumentException("MailDefinition_InvalidReplacements");
|
|
}
|
|
subject = Regex.Replace(subject, current, text, RegexOptions.IgnoreCase);
|
|
body = Regex.Replace(body, current, text, RegexOptions.IgnoreCase);
|
|
}
|
|
}
|
|
mailMessage.Subject = subject;
|
|
mailMessage.Body = body;
|
|
}
|
|
catch
|
|
{
|
|
if (mailMessage != null)
|
|
{
|
|
mailMessage.Dispose();
|
|
}
|
|
throw;
|
|
}
|
|
return mailMessage;
|
|
}
|
|
}
|
|
}
|