ResetUser.cs 9.81 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;

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>();
                LinkedResource LinkImage;
                string ParseText = "";
                int TextPos = 0;
                int MinPos = 0;
                string ImgSrc = "";
                int Counter = 0;
                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;

                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;
                
                ImgSrc = "//52.2.38.120/content/images/common/logo-large.png";
                ImgSrc = "//192.168.86.34:82/content/images/common/logo-large.png";
                string _logoPath = HttpContext.Current.Server.MapPath("//52.2.38.120/content/images/common/logo-large.png");

                string _templatePath = string.Empty;

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

                string _resetPassLink = "//192.168.86.34:82?e:"+ HttpUtility.UrlEncode(_userMail);

                
                
                string mailBody = ResetUser.ReadMailBodyTextFromFile(_templatePath, _userId, _password, _userMail, _fullName, _resetPassLink);
                //sBody.Append("<style type='text/css'>strong {font-size: 18px;color: #ffff00;}</style><table width='500' border='0' align='center' cellpadding='0' cellspacing='0'><tbody><tr><td align='center' valign='middle' bgcolor='#393939' style='padding:30px 0 20px 0;'><a href='#'><img src='{logoPath}' alt='AIA' title='AIA' /></a></td></tr><tr><td align='center' valign='top' bgcolor='#808d43' style='padding:20px;'><table width='100%' border='0' align='center' cellpadding='0' cellspacing='0'><tbody><tr><td colspan='2' style=' font-size:32px; font-weight:bold; color:#fff; font-family:Gotham, Helvetica, Arial, sans-serif'>Forgot UserID</td></tr><tr><td colspan='2'>&nbsp;</td><tr><td colspan='2' style='font-size:20px; font-weight:bold; color:#fff; font-family:Gotham, Helvetica, Arial, sans-serif'>Hello {userName}</td><tr><td colspan='2'>&nbsp;</td></tr><tr><td colspan='2' style=' font-size:16px; font-weight:bold; color:#fff; font-family:Gotham, Helvetica, Arial, sans-serif'>You have requested your 'Login ID' for following account: {emailId}</td></tr><tr><td colspan='2'>&nbsp;</td></tr><tr><td colspan='2' style=' font-size:18px; font-family:Gotham, Helvetica, Arial, sans-serif; color:#fff;'>Your login ID is: <strong>{loginId}</strong></td></tr><tr><td colspan='2'>&nbsp;</td></tr><tr><td colspan='2'>&nbsp;</td></tr><tr><td colspan='2'>&nbsp;</td></tr></tbody></table></td></tr><tr><td bgcolor='#f9f2e7' style='padding:20px;'><table width='100%' border='0' align='center' cellpadding='0' cellspacing='0'><tbody><tr><td style=' font-size:12px; font-family:Gotham, Helvetica, Arial, sans-serif; color:#000000;'>A.D.A.M. – the company that  pioneered online health content – is dedicated to creating and offering the  most effective and innovative educational solutions possible for teaching  medical science and improving health literacy.</td></tr><tr><td style=' font-size:12px; font-family:Gotham, Helvetica, Arial, sans-serif; color:#000000;'>&nbsp;</td></tr><tr><td style=' font-size:12px; font-family:Gotham, Helvetica, Arial, sans-serif; color:#000000;'>Give us a <b>call toll-free at 1-888-278-9614</b> or <em>send us an email</em> if you have any questions or if you need help. It will be our pleasure to help  you.</td></tr><tr><td style=' font-size:12px; font-family:Gotham, Helvetica, Arial, sans-serif; color:#000000;'>&nbsp;</td></tr><tr><td style=' font-size:12px; font-family:Gotham, Helvetica, Arial, sans-serif; color:#000000;'>&nbsp;</td></tr><tr><td style=' font-size:12px; font-family:Gotham, Helvetica, Arial, sans-serif; color:#000000;'><em>&copy; 2017  Ebix, Inc. All Rights Reserved. </em></td></tr></tbody></table></td></tr></tbody></table>");

                lstToAddress.Add(_userMail);

                sEmailText = mailBody;

                // for embedding images in email
                if (sEmailText.Contains("<img"))
                {
                    ParseText = sEmailText;
                    TextPos = ParseText.IndexOf("<img", TextPos);
                    while (TextPos != -1)
                    {
                        TextPos = ParseText.IndexOf("src", TextPos) + 5;
                        MinPos = ParseText.IndexOf('"', TextPos);
                        //ImgSrc = ParseText.Substring(TextPos, MinPos - TextPos);
                        string ImagePath = HttpContext.Current.Server.MapPath(ImgSrc);
                        LinkImage = new LinkedResource(ImagePath);
                        Counter++;
                        LinkImage.ContentId = "Img" + Counter;
                        sEmailText = sEmailText.Replace("\"" + ImgSrc + "\"", "cid:" + LinkImage.ContentId);
                        lstLinkedResource.Add(LinkImage);
                        TextPos = ParseText.IndexOf("<img", TextPos);
                    }
                }

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

                //LinkedResource logoImage = new LinkedResource(ImgSrc);
                ////logoImage.ContentType = MediaTypeNames.Image.Gif;
                //logoImage.ContentId = "logo-large";
                //lstLinkedResource.Add(logoImage);

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

                


                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();
                }

                //System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource(logoPath, "image/jpng");
                //imageResource.ContentId = "HDIImage";

                //sBody = sBody.Replace("{logoPath}", imageResource.ContentStream.ToString());
                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;
        }

        protected static AlternateView GetEmbeddedImage(String filePath)
        {
            LinkedResource inline = new LinkedResource(filePath);
            inline.ContentId = Guid.NewGuid().ToString();
            string htmlBody = @"<img src='cid:" + inline.ContentId + @"'/>";
            AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
            alternateView.LinkedResources.Add(inline);
            return alternateView;
        }
    }
}