ResetUser.cs 5.31 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Driver;
using MongoDB.Bson;
using AIAHTML5.API.Properties;
using AIAHTML5.API.Constants;
using log4net;
using System.Net.Mail;
using AIAHTML5.API.Utility;
using System.Text;
using System.IO;
using System.Net.Mime;
using System.Configuration;

namespace AIAHTML5.API.Models
{
    public class ResetUser
    {
        public static bool SendEmail(dynamic UserDetails, bool isPassword)
        {
            try
            {
                List<LinkedResource> lstLinkedResource = new List<LinkedResource>();
                EmailUtility sEmailUtility = new EmailUtility();
                List<string> lstToAddress = new List<string>();
                List<string> lstBccAddress = new List<string>();

                string sEmailText = string.Empty;
                string _userId = string.Empty;
                string _password = string.Empty;
                string _userMail = string.Empty;
                string _userName = string.Empty;
                string _fName = string.Empty;
                string _lName = string.Empty;
                string _site_url = Convert.ToString(ConfigurationManager.AppSettings["Site_URL"]);

                foreach (KeyValuePair<string, object> kvp in UserDetails)
                {
                    if (kvp.Key == "loginId")
                        _userId = kvp.Value.ToString();

                    if (kvp.Key == "password")
                        _password = kvp.Value.ToString();

                    if (kvp.Key == "emailId")
                        _userMail = kvp.Value.ToString();

                    if (kvp.Key == "firstName")
                        _fName = kvp.Value.ToString();

                    if (kvp.Key == "lastName")
                        _lName = kvp.Value.ToString();
                }

                string _fullName = _fName + " " + _lName;
                
                string _logoPath = HttpContext.Current.Server.MapPath("~/content/images/logo.png");

                string _templatePath = string.Empty;

                if (!isPassword)
                    _templatePath = "~/Templates/forgot-UserId.html";
                else
                    _templatePath = "~/Templates/forgot-Password.html";

                string _resetPassLink = _site_url + "?em:" + HttpUtility.UrlEncode(_userMail);

                string mailBody = ResetUser.ReadMailBodyTextFromFile(_templatePath, _userId, _password, _userMail, _fullName, _resetPassLink);

                lstToAddress.Add(_userMail);

                sEmailText = mailBody;


                // for embedding images in email
                if (sEmailText.Contains("<img"))
                {
                    LinkedResource inline = new LinkedResource(_logoPath, MediaTypeNames.Image.Jpeg);
                    inline.ContentId = "AIA_Logo_Image";
                    inline.TransferEncoding = TransferEncoding.Base64;
                    sEmailText = sEmailText.Replace("{logoPath}", "cid:" + inline.ContentId);
                    lstLinkedResource.Add(inline);
                }

                sEmailUtility.sAlternateView = AlternateView.CreateAlternateViewFromString(sEmailText, null, "text/html");
                foreach (var sItem in lstLinkedResource)
                {
                    sEmailUtility.sAlternateView.LinkedResources.Add(sItem);
                }

                string _mailSubject = string.Empty;
                if(!isPassword)
                    _mailSubject = "UserID recovery mail for: ";
                else
                    _mailSubject = "Password recovery mail for: ";                

                sEmailUtility.sHostName = "10.100.12.13";
                sEmailUtility.sFromAddress = "utkarsh.singh@ebix.com";
                sEmailUtility.bIsBodyHtml = true;
                sEmailUtility.bEnableSsl = false;
                sEmailUtility.sSubject = _mailSubject + _userMail;
                //sEmailUtility.sBodyText = sEmailText;
                sEmailUtility.iPort = 25;
                sEmailUtility.sToAddresses = lstToAddress;
                sEmailUtility.sBccAddresses = lstBccAddress;

                sEmailUtility.SendSmtpEmail();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Exception: " + ex.Message;
                return false;
            }
        }

        protected static string ReadMailBodyTextFromFile(string templatePath, string userId, string password, string userMail, string fullName, string resetPasswordUrl)
        {
            string fileToRead = string.Empty;
            string sBody = string.Empty;
            try
            {               
                fileToRead = HttpContext.Current.Server.MapPath(templatePath);

                using (StreamReader reader = new StreamReader(fileToRead))
                {
                    sBody = reader.ReadToEnd();
                }

                sBody = sBody.Replace("{loginId}", userId);
                sBody = sBody.Replace("{emailId}", userMail);
                sBody = sBody.Replace("{userFullName}", fullName);
                sBody = sBody.Replace("{resetPasswordLink}", resetPasswordUrl);
            }
            catch (Exception e)
            {
                sBody = "Exception: " + e.Message;
            }
            return sBody;
        }
    }
}