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

63 lines
2.8 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;
namespace EnVisage.Code
{
public class MailManager
{
private const string INVITATION_MESSAGE_SUBJECT = "Activate your EnVisage account";
private const string INVITATION_MESSAGE_BODY = "Hello, {{user_name}}. {{new_line}}{{new_line}}Please activate your PlanIT PPM account 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 PlanIT PPM account by clicking the following link: {{activation_link}}. {{new_line}}{{new_line}}Thank you!";
public static void SendInvitationMessage(string email, string userName, string userId)
{
string text = INVITATION_MESSAGE_BODY;
string link;
if(HttpContext.Current.Request.Url.Port == 80)
link = string.Format("http://{1}/Account/Activate/?userId={0}", userId, HttpContext.Current.Request.Url.Host);
else
link = string.Format("http://{1}:{2}/Account/Activate/?userId={0}",
userId,
HttpContext.Current.Request.Url.Host,
HttpContext.Current.Request.Url.Port);
text = text.Replace("{{user_name}}", userName);
text = text.Replace("{{activation_link}}", link);
text = text.Replace("{{new_line}}", Environment.NewLine);
SendMessage(INVITATION_MESSAGE_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)
link = string.Format("http://{1}/Account/RestorePassword/?token={0}", token, HttpContext.Current.Request.Url.Host);
else
link = string.Format("http://{1}:{2}/Account/RestorePassword/?token={0}", token, HttpContext.Current.Request.Url.Host,
HttpContext.Current.Request.Url.Port);
text = text.Replace("{{user_name}}", userName);
text = text.Replace("{{activation_link}}", link);
text = text.Replace("{{new_line}}", Environment.NewLine);
SendMessage(RESTORE_PASSWORD_MESSAGE_SUBJECT, email, text);
}
private static void SendMessage(string subject, string emailTo, string text)
{
var recipient = Settings.Default.IsEmailTestMode ? "envisage.tulasoft@gmail.com" : emailTo;
var message = new MailMessage("envisage.tulasoft@gmail.com", recipient, subject, text);
var client = new SmtpClient();
client.Send(message);
}
}
}