📄 emailutility.cs
字号:
//*********************************************************************
//
// SendFormattedEmail Method
//
// Sends a single email with formatting applied to it. For
// example, <Username> is replaced with a username.
//
//*********************************************************************
public static void SendFormattedEmail
(
string from,
MessageInfo messageInfo,
ProfileInfo profileInfo,
string smtpServer
) {
// format the text
messageInfo.Title = FormatUserText
(
messageInfo.Title,
MailFormat.Text,
profileInfo.Username,
profileInfo.Password,
profileInfo.FirstName,
profileInfo.LastName
);
messageInfo.Body = FormatUserText
(
messageInfo.Body,
MailFormat.Text,
profileInfo.Username,
profileInfo.Password,
profileInfo.FirstName,
profileInfo.LastName
);
messageInfo.Body = FormatContentText(messageInfo.Body, MailFormat.Text);
// Send the email
SendEmail
(
from,
profileInfo.Email,
messageInfo.Title,
messageInfo.Body,
MailFormat.Text,
smtpServer
);
}
//*********************************************************************
//
// FormatEmailText Method
//
// Formats email text without sending the email.
//
//*********************************************************************
public static string FormatEmailText
(
string text,
MailFormat bodyFormat,
ProfileInfo profileInfo
) {
// Format Content Text
text = FormatContentText(text, bodyFormat);
// Get and apply user specific formatting
text = FormatUserText
(
text,
bodyFormat,
profileInfo.Username,
profileInfo.Password,
profileInfo.FirstName,
profileInfo.LastName
);
return text;
}
//*********************************************************************
//
// FormatUserText Method
//
// Formats text with user specific information.
//
//*********************************************************************
public static string FormatUserText
(
string text,
MailFormat bodyFormat,
string username,
string password,
string firstName,
string lastName
) {
string matchString;
// Get base url
string baseUrl = CommunityGlobals.ResolveAbsoluteUrl(CommunityGlobals.AppPath);
// Replace username
matchString = Regex.Escape("<Username>");
text = Regex.Replace(text, matchString, username);
// Replace password
matchString = Regex.Escape("<Password>");
text = Regex.Replace(text, matchString, password);
// Replace first name
matchString = Regex.Escape("<FirstName>");
text = Regex.Replace(text, matchString, firstName);
// Replace last name
matchString = Regex.Escape("<LastName>");
text = Regex.Replace(text, matchString, lastName);
// Replace full name
matchString = Regex.Escape("<FullName>");
text = Regex.Replace(text, matchString, firstName + " " + lastName);
// Replace Edit Profile Link
matchString = Regex.Escape("<EditProfileLink>");
if (bodyFormat == MailFormat.Html)
text = Regex.Replace(text, matchString, String.Format("<a href=\"{0}\">{0}</a>", baseUrl + "/Users_EditProfile.aspx"));
else
text = Regex.Replace(text, matchString, baseUrl + "/Users_EditProfile.aspx");
// Replace Home Link
matchString = Regex.Escape("<HomeLink>");
if (bodyFormat == MailFormat.Html)
text = Regex.Replace(text, matchString, String.Format("<a href=\"{0}\">{0}</a>", baseUrl + "/Default.aspx"));
else
text = Regex.Replace(text, matchString, baseUrl + "/Default.aspx");
return text;
}
//*********************************************************************
//
// FormatContentText Method
//
// Formats email text with content specific information.
//
//*********************************************************************
public static string FormatContentText(string text, MailFormat bodyFormat) {
string matchText;
// Replace new articles
matchText = Regex.Escape("<NewArticles>");
if (Regex.IsMatch(text, matchText))
text = Regex.Replace(text, matchText, FormatContentCollection(ContentPageUtility.GetNewContent("Article"), bodyFormat));
// Replace new events
matchText = Regex.Escape("<NewEvents>");
if (Regex.IsMatch(text, matchText))
text = Regex.Replace(text, matchText, FormatContentCollection(ContentPageUtility.GetNewContent("Event"), bodyFormat));
// Replace new books
matchText = Regex.Escape("<NewBooks>");
if (Regex.IsMatch(text, matchText))
text = Regex.Replace(text, matchText, FormatContentCollection(ContentPageUtility.GetNewContent("Book"), bodyFormat));
// Replace new links
matchText = Regex.Escape("<NewLinks>");
if (Regex.IsMatch(text, matchText))
text = Regex.Replace(text, matchText, FormatContentCollection(ContentPageUtility.GetNewContent("Link"), bodyFormat));
// Replace new downloads
matchText = Regex.Escape("<NewDownloads>");
if (Regex.IsMatch(text, matchText))
text = Regex.Replace(text, matchText, FormatContentCollection(ContentPageUtility.GetNewContent("Download"), bodyFormat));
// Replace new photos
matchText = Regex.Escape("<NewPhotos>");
if (Regex.IsMatch(text, matchText))
text = Regex.Replace(text, matchText, FormatContentCollection(ContentPageUtility.GetNewContent("Photo"), bodyFormat));
// Replace new discuss
matchText = Regex.Escape("<NewDiscuss>");
if (Regex.IsMatch(text, matchText))
text = Regex.Replace(text, matchText, FormatContentCollection(ContentPageUtility.GetNewContent("Post"), bodyFormat));
return text;
}
//*********************************************************************
//
// FormatContentCollection Method
//
// Formats a collection of content items (events, articles, etc)
// and returns the formatted string.
//
//*********************************************************************
public static string FormatContentCollection(ArrayList colContent, MailFormat bodyFormat) {
string contentLink;
StringBuilder builder = new StringBuilder();
foreach (ContentInfo _contentInfo in colContent) {
contentLink = ContentPageUtility.CalculateFullContentPath(_contentInfo.SectionID, _contentInfo.ContentPageID);
if (bodyFormat == MailFormat.Html) {
builder.Append("<li>");
builder.AppendFormat("<a href=\"{0}\">{1}</a>", contentLink, _contentInfo.Title);
builder.Append("<br>");
builder.Append(CommunityGlobals.FormatPlainText(_contentInfo.BriefDescription));
builder.Append("<p>");
} else {
builder.AppendFormat("\n{0}", _contentInfo.Title);
builder.AppendFormat("\n {0}", _contentInfo.BriefDescription);
builder.AppendFormat("\n\n {0}", contentLink);
builder.Append("\n ");
}
}
return builder.ToString();
}
//*********************************************************************
//
// EmailUtility Constructor
//
// Use a private constructor for a class with static methods.
//
//*********************************************************************
private EmailUtility() {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -