ResetUser.cs
5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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;
}
}
}