EmailUtility.cs
10.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Collections;
using System.Xml;
using System.Text;
using System.IO;
using System.Net.Mail;
namespace AIAHTML5.API.Utility
{
public class EmailUtility
{
public string sFromAddress { get; set; }
public List<string> sToAddresses { get; set; }
public List<string> sBccAddresses { get; set; }
public string sHostName { get; set; }
public string sSubject { get; set; }
public int iPort { get; set; }
public bool bEnableSsl { get; set; }
public string sUserName { get; set; }
public string sPassword { get; set; }
public bool bIsBodyHtml { get; set; }
public List<Attachment> sAttachments { get; set; }
public string sBodyText { get; set; }
public AlternateView sAlternateView { get; set; }
public void SendMail(MailMessage mm)
{
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTPAddress"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSSL"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(mm.From.ToString(), ConfigurationManager.AppSettings["SenderPassword"]);
smtp.Credentials = NetworkCred;
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);
/*string ccAddress = ConfigurationManager.AppSettings["NotifyCopyAddress"];
string[] emailArr = ccAddress.Split(',');
if (mm.From.ToString().Equals("pfizerhelp@ebix.com"))
{
ccAddress = emailArr[0];
if (!ccAddress.Equals(""))
{
mm.Bcc.Add(ccAddress);
}
}
else
{
if (!ccAddress.Equals(""))
{
mm.Bcc.Add(ccAddress);
}
}*/
smtp.Send(mm);
}
public void SendSmtpEmail()
{
try
{
MailMessage sMail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(sHostName);
string recipientEmailAddress = string.Empty;
if (sToAddresses != null)
{
foreach (var sItem in sToAddresses)
{
sMail.To.Add(sItem);
}
}
if (sBccAddresses != null)
{
foreach (var sItem in sBccAddresses)
{
sMail.Bcc.Add(sItem);
}
}
sMail.IsBodyHtml = bIsBodyHtml;
if (sAlternateView != null)
{
sMail.AlternateViews.Add(sAlternateView);
}
else
{
sMail.Body = sBodyText;
}
sMail.Subject = sSubject;
if (sAttachments != null)
{
foreach (var sItem in sAttachments)
{
sMail.Attachments.Add(sItem);
}
}
SmtpServer.Port = iPort;
SmtpServer.Credentials = new System.Net.NetworkCredential(sUserName, sPassword);
SmtpServer.EnableSsl = bEnableSsl;
// SmtpServer.Send(sMail);
using (MailMessage mm = new MailMessage(sFromAddress, sMail.To.ToString()))
//using (MailMessage mm = new MailMessage())
{
mm.Subject = sSubject;
mm.IsBodyHtml = bIsBodyHtml;
// mm.To.Add(sMail.To.ToString());
//mm.From = new MailAddress(sFromAddress, "AdamOnDemand");
if (sAlternateView != null)
{
mm.AlternateViews.Add(sAlternateView);
}
else
{
mm.Body = sBodyText;
}
// mm.Body = sAlternateView;
// string bccAddress = (!string.IsNullOrEmpty(sBccAddresses) ? lstBccAddress : ConfigurationManager.AppSettings["AccountsEmailAddress"]);
/* if (sBccAddresses != null)
{
foreach (var sItem in sBccAddresses)
{
mm.Bcc.Add(sItem);
}
}*/
//else
//{
// string bccAddress = ConfigurationManager.AppSettings["AccountsEmailAddress"];
// mm.Bcc.Add(bccAddress);
//}
mm.IsBodyHtml = true;
SendMail(mm);
}
}
catch (Exception ex)
{
throw ex;
}
}
//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 AdminNames = "";
// string sEmailText = "";
// string ParseText = "";
// int TextPos = 0;
// int MinPos = 0;
// string ImgSrc = "";
// int Counter = 0;
// string _userId = string.Empty;
// string _password = string.Empty;
// string _userMail = 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 (isPassword)
// sEmailText = "Your password is: <b>" + _password;
// else
// sEmailText = "Your LoginID is: <b>" + _userId;
// lstToAddress.Add(_userMail);
// sEmailUtility.sSubject = "Recovery mail [" + _userMail + "]";
// ////// 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);
// //// }
// ////}
// /*foreach (var sItem in lstEmailParams)
// {
// sEmailText = sEmailText.Replace(sItem.Item1, sItem.Item2);
// }*/
// /*if (!string.IsNullOrEmpty(recipientEmailAddress))
// {
// string[] sStrArr = recipientEmailAddress.Split(',');
// for (int i = 0; i < sStrArr.Length - 1; i++)
// {
// string sEmail = sStrArr[i];
// lstToAddress.Add(sStrArr[i]);
// }
// }*/
// /*if (!string.IsNullOrEmpty(bccEmailAddress))
// {
// string[] sStrArr = bccEmailAddress.Split(',');
// for (int i = 0; i < sStrArr.Length - 1; i++)
// {
// string sEmail = sStrArr[i];
// lstBccAddress.Add(sEmail);
// //AdminNames += context.Users.Where(El => El.EmailAddress == sEmail).First().FirstName + ", ";
// }
// // AdminNames = AdminNames.Substring(0, AdminNames.Length - 2);
// sEmailText = sEmailText.Replace("#ADMINNAMES#", AdminNames);
// }*/
// 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 = "Recovery mail [User Information]";
// sEmailUtility.iPort = 25;
// sEmailUtility.sToAddresses = lstToAddress;//Convert.ToString(UserDetails[7]); ;
// sEmailUtility.sBccAddresses = lstBccAddress;
// sEmailUtility.SendSmtpEmail();
// return true;
// }
// catch (Exception ex)
// {
// return false;
// }
//}
}
}