ResetUser.cs
9.81 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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'> </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'> </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'> </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'> </td></tr><tr><td colspan='2'> </td></tr><tr><td colspan='2'> </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;'> </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;'> </td></tr><tr><td style=' font-size:12px; font-family:Gotham, Helvetica, Arial, sans-serif; color:#000000;'> </td></tr><tr><td style=' font-size:12px; font-family:Gotham, Helvetica, Arial, sans-serif; color:#000000;'><em>© 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;
}
}
}