EnVisageOnline/Main/Source/EnVisage/Code/BLL/MailManager.cs

183 lines
8.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Threading.Tasks;
using EnVisage.Properties;
using NLog;
using System.Text;
using System.Data.Entity.Validation;
namespace EnVisage.Code
{
public class MailManager
{
private const string INVITATION_MESSAGE_SUBJECT = "Activate your Prevu account";
private const string INVITATION_SSO_MESSAGE_SUBJECT = "Your Prevu account has been created";
private const string INVITATION_MESSAGE_BODY = "Hello, {{user_name}}. {{new_line}}{{new_line}}Please activate your Prevu account by clicking the following link: {{activation_link}} {{new_line}}{{new_line}}Thank you!";
private const string INVITATION_SSO_MESSAGE_BODY = "Hello, {{user_name}}. {{new_line}}{{new_line}}your Prevu account has been created and is ready for you to access by clicking the following link: {{activation_link}} {{new_line}}{{new_line}}Thank you!";
private const string RESTORE_PASSWORD_MESSAGE_SUBJECT = "Restore password";
private const string RESTORE_PASSWORD_MESSAGE_BODY = "Hello, {{user_name}}. {{new_line}}{{new_line}}You can restore your Prevu account by clicking the following link: {{activation_link}}{{new_line}}{{new_line}}The link can only be used for {{TTL}} hours. {{new_line}}{{new_line}}Thank you!";
protected static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static void SendInvitationMessage(string email, string userName, string userId, bool isSSO)
{
string text = INVITATION_MESSAGE_BODY;
string subject = INVITATION_MESSAGE_SUBJECT;
if (isSSO)
{
text = INVITATION_SSO_MESSAGE_BODY;
subject = INVITATION_SSO_MESSAGE_SUBJECT;
}
string link;
if (HttpContext.Current.Request.Url.Port == 80 || HttpContext.Current.Request.Url.Port == 443)
link = string.Format("{2}://{1}/Account/Activate/?userId={0}", userId, HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.Url.Scheme);
else
link = string.Format("{3}://{1}:{2}/Account/Activate/?userId={0}",
userId,
HttpContext.Current.Request.Url.Host,
HttpContext.Current.Request.Url.Port, HttpContext.Current.Request.Url.Scheme);
if (isSSO)
{
if (HttpContext.Current.Request.Url.Port == 80 || HttpContext.Current.Request.Url.Port == 443)
link = string.Format("{1}://{0}/", HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.Url.Scheme);
else
link = string.Format("{2}://{0}:{1}/",
HttpContext.Current.Request.Url.Host,
HttpContext.Current.Request.Url.Port, HttpContext.Current.Request.Url.Scheme);
}
text = text.Replace("{{user_name}}", userName);
text = text.Replace("{{activation_link}}", link);
text = text.Replace("{{new_line}}", Environment.NewLine);
SendMessage(subject, email, text);
}
public static void SendRestorePasswordMessage(string email, Guid token, string userName)
{
string text = RESTORE_PASSWORD_MESSAGE_BODY;
string link;
if (HttpContext.Current.Request.Url.Port == 80 || HttpContext.Current.Request.Url.Port == 443)
link = string.Format("{2}://{1}/Account/RestorePassword/?token={0}", token, HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.Url.Scheme);
else
link = string.Format("{3}://{1}:{2}/Account/RestorePassword/?token={0}", token, HttpContext.Current.Request.Url.Host,
HttpContext.Current.Request.Url.Port, HttpContext.Current.Request.Url.Scheme);
var ttl = Settings.Default.ForgotPasswordLinkTTLDays * 24; // time to live in hours instead of days
text = text.Replace("{{user_name}}", userName);
text = text.Replace("{{activation_link}}", link);
text = text.Replace("{{new_line}}", Environment.NewLine);
text = text.Replace("{{TTL}}", ttl.ToString());
SendMessage(RESTORE_PASSWORD_MESSAGE_SUBJECT, email, text);
}
public static void SendMessage(string subject, string emailTo, string text)
{
var recipient = Settings.Default.IsEmailTestMode ? Settings.Default.TestEmailAddress : emailTo;
var message = new MailMessage("support@prevuplan.com", recipient, subject, text);
using (var client = new SmtpClient())
{
client.Send(message);
client.Dispose();
}
}
public static void SendMessageHtml(string subject, string emailTo, string text, string fromAddress)
{
try
{
System.Threading.Thread t = new System.Threading.Thread(() => SendMessageHtmlThread(subject, emailTo, text, fromAddress));
t.Start();
}
catch (Exception dds) { LogException(dds); }
}
public static void SendMessageHtmlThread(string subject, string emailTo, string text, string fromAddress )
{
MailMessage message = null;
var recipient = emailTo;
var MessageID = Guid.NewGuid();
try {
recipient = Settings.Default.IsEmailTestMode ? Settings.Default.TestEmailAddress : emailTo;
}
catch (Exception)
{
}
if (Settings.Default.IsEmailTestMode)
text += "<br/><br/> this is a test email! the original recipient is:" + emailTo;
try
{
message = new MailMessage(fromAddress, recipient, subject, text);
message.IsBodyHtml = true;
using (var client = new SmtpClient())
{
client.Send(message);
client.Dispose();
}
}
catch (Exception dds)
{
LogException(dds);
}
}
public static void LogException(Exception ex)
{
var sb = new StringBuilder();
sb.AppendLine(string.Format("{0}: {1}", ex.GetType(), ex.Message));
sb.AppendLine(ex.StackTrace);
var innerCount = 0;
var innerEx = ex;
while (innerEx.InnerException != null && innerCount++ < Constants.MAX_INNER_EXCEPTION_LOG_LEVEL)
{
if (innerEx.Message != innerEx.InnerException.Message)
sb.AppendLine("Inner Exception Message: " + innerEx.InnerException.Message);
innerEx = innerEx.InnerException;
}
var dbEntityValidationException = ex as DbEntityValidationException;
if (dbEntityValidationException != null)
{
foreach (var validationErrors in dbEntityValidationException.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
sb.AppendFormat("Property: {0} Error: {1}", validationError.PropertyName,
validationError.ErrorMessage);
}
}
sb.AppendLine(dbEntityValidationException.StackTrace);
}
if (System.Web.HttpContext.Current != null)
{
sb.AppendLine();
sb.AppendLine(string.Format("URL: {0}", System.Web.HttpContext.Current.Request.Url));
sb.AppendLine(string.Format("Referrer: {0}", System.Web.HttpContext.Current.Request.UrlReferrer));
sb.AppendLine(string.Format("QueryString: {0}", System.Web.HttpContext.Current.Request.QueryString));
sb.AppendLine(string.Format("UserHostAddress: {0}", System.Web.HttpContext.Current.Request.UserHostAddress));
sb.AppendLine(string.Format("UserAgent: {0}", System.Web.HttpContext.Current.Request.UserAgent));
if (System.Web.HttpContext.Current.Request.Form.Count > 0)
{
sb.AppendLine();
sb.AppendLine("Form:");
foreach (string key in System.Web.HttpContext.Current.Request.Form.Keys)
{
sb.AppendLine(string.Format("{0}: {1}", key, System.Web.HttpContext.Current.Request.Form[key]));
}
}
}
// log error using NLog
Logger.Fatal(sb.ToString());
}
}
}