988 lines
40 KiB
C#
988 lines
40 KiB
C#
|
using System;
|
||
|
using System.Text;
|
||
|
using System.Net.Mail;
|
||
|
using System.Net;
|
||
|
using System.Security.Cryptography.X509Certificates;
|
||
|
using System.Net.Security;
|
||
|
using System.Data;
|
||
|
using HRM.BO.Configuration;
|
||
|
using Microsoft.Extensions.Options;
|
||
|
using System.Threading;
|
||
|
|
||
|
namespace HRM.BO
|
||
|
{
|
||
|
public class MailSender
|
||
|
{
|
||
|
DataSet oEmpDetails = null;
|
||
|
DataRow dRow = null;
|
||
|
string checkSendMail = string.Empty;
|
||
|
MailSender sender = null;
|
||
|
string sbody = "";
|
||
|
string mailTitle = string.Empty;
|
||
|
//private MailMessage mailMessage = null;
|
||
|
private SmtpClient client = null;
|
||
|
private object _mailbody;
|
||
|
Employee oLineManager = null;
|
||
|
private readonly IOptions<EmailSettings> _emailSettings;
|
||
|
|
||
|
public object Mailbody
|
||
|
{
|
||
|
get { return _mailbody; }
|
||
|
set { _mailbody = value; }
|
||
|
}
|
||
|
|
||
|
#region Private Variable
|
||
|
private string _from;
|
||
|
private string _to;
|
||
|
private string _cc;
|
||
|
private string _subject;
|
||
|
private string _body;
|
||
|
private string _attachments;
|
||
|
private string _link;
|
||
|
#endregion
|
||
|
|
||
|
#region Event
|
||
|
public event EventHandler client_SendCompleted;
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Constractor
|
||
|
public MailSender()
|
||
|
{
|
||
|
_to = "";
|
||
|
_cc = "";
|
||
|
_body = "";
|
||
|
_subject = "";
|
||
|
_attachments = "";
|
||
|
_from = "";
|
||
|
_link = "";
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
#region Properties
|
||
|
|
||
|
public string CC
|
||
|
{
|
||
|
get { return _cc; }
|
||
|
set { _cc = value; }
|
||
|
}
|
||
|
public string From
|
||
|
{
|
||
|
get { return _from; }
|
||
|
set { _from = value; }
|
||
|
}
|
||
|
public string To
|
||
|
{
|
||
|
get { return _to; }
|
||
|
set { _to = value; }
|
||
|
}
|
||
|
|
||
|
public string Subject
|
||
|
{
|
||
|
get { return _subject; }
|
||
|
set { _subject = value; }
|
||
|
}
|
||
|
|
||
|
public string Body
|
||
|
{
|
||
|
get { return _body; }
|
||
|
set { _body = value; }
|
||
|
}
|
||
|
|
||
|
|
||
|
public string Link
|
||
|
{
|
||
|
get { return _link; }
|
||
|
set { _link = value; }
|
||
|
}
|
||
|
|
||
|
|
||
|
public string Attachments
|
||
|
{
|
||
|
get { return _attachments; }
|
||
|
set { _attachments = value; }
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region Methods
|
||
|
//public EnumSendMail SendMail()
|
||
|
//{
|
||
|
// if (_to == "")
|
||
|
// {
|
||
|
// return EnumSendMail.ToMissing;
|
||
|
// }
|
||
|
|
||
|
// _body += AddLink();
|
||
|
|
||
|
// try
|
||
|
// {
|
||
|
// string[] toWhom = _to.Split(',');
|
||
|
|
||
|
// if (toWhom.Length > 1)
|
||
|
// {
|
||
|
// _body = _body.Replace("#RECEIVER#", "Colleagues");
|
||
|
|
||
|
// if (this.Subject.ToString() == "Mid Year Review Start" || this.Subject.ToString() == "Mid Year Review Freezed")
|
||
|
// {
|
||
|
// //if (_toEmployee != null)
|
||
|
// //{
|
||
|
// // _body = _body.Replace("#LogID#", _toEmployee.EmployeeNo);
|
||
|
// // _body = _body.Replace("#EmpID#", _toEmployee.ID.ToString());
|
||
|
// //}
|
||
|
// }
|
||
|
|
||
|
// //_mailMessage = new MailMessage(_from, toWhom[0], _subject, _body);
|
||
|
// for (int count = 1; count < toWhom.Length; count++)
|
||
|
// {
|
||
|
// //_mailMessage.To.Add(toWhom[count]);
|
||
|
// }
|
||
|
|
||
|
// }
|
||
|
// else
|
||
|
// {
|
||
|
// _body = _body.Replace("#RECEIVER#", "Colleagues");
|
||
|
|
||
|
// if (this.Subject.ToString() == "Mid Year Review Start" || this.Subject.ToString() == "Mid Year Review Freezed")
|
||
|
// {
|
||
|
// //if (_toEmployee != null)
|
||
|
// //{
|
||
|
// // _body = _body.Replace("#LogID#", _toEmployee.EmployeeNo);
|
||
|
// // _body = _body.Replace("#EmpID#", _toEmployee.ID.ToString());
|
||
|
// // //_body = _body.Remove(
|
||
|
// //}
|
||
|
// }
|
||
|
|
||
|
// //_mailMessage = new MailMessage(_from, _to, _subject, _body);
|
||
|
// }
|
||
|
|
||
|
// if (_cc.Length > 0)
|
||
|
// {
|
||
|
// //_mailMessage.CC.Add(_cc);
|
||
|
// }
|
||
|
// //if (_fileAttachmentPath.Length > 0)
|
||
|
// // _mailMessage.Attachments.Add(new Attachment(_fileAttachmentPath));
|
||
|
|
||
|
// //_client = new SmtpClient(_server);
|
||
|
// string s1 = Ease.Core.Utility.ConfigUtility.GetAppSettings("From");
|
||
|
// string s2 = Ease.Core.Utility.ConfigUtility.GetAppSettings("Password");
|
||
|
|
||
|
// //if (_server.Length > 0)
|
||
|
// //{
|
||
|
// // if (s1 != "" && s2 != "")
|
||
|
// // _client.Credentials = new System.Net.NetworkCredential(s1, s2);
|
||
|
// // //_client.Send(mailMessage);
|
||
|
// //}
|
||
|
// //else
|
||
|
// //{
|
||
|
// // return EnumSendMail.ServerNotFound;
|
||
|
// //}
|
||
|
// //if (_server.Length > 0)
|
||
|
// //{
|
||
|
// // _client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
|
||
|
|
||
|
// // _client.Send(_mailMessage);
|
||
|
// //}
|
||
|
// //else
|
||
|
// //{
|
||
|
// // return EnumSendMail.ServerNotFound;
|
||
|
// //}
|
||
|
// }
|
||
|
// catch (Exception ex)
|
||
|
// {
|
||
|
// // _errMsg = ex.Message + ex.InnerException;
|
||
|
// return EnumSendMail.SendError;
|
||
|
// }
|
||
|
// return EnumSendMail.SuccessFullySend;
|
||
|
//}
|
||
|
|
||
|
//public EnumSendMail SendMail(System.Collections.Generic.List<System.Net.Mail.MailMessage> messages)
|
||
|
//{
|
||
|
// try
|
||
|
// {
|
||
|
// //_messages = messages;
|
||
|
// //if (_messages == null || _messages.Count <= 0) return EnumSendMail.BodyMissing;
|
||
|
|
||
|
// //if (_server.Length == 0)
|
||
|
// //{
|
||
|
// // return EnumSendMail.ServerNotFound;
|
||
|
// //}
|
||
|
|
||
|
// if (this.Subject.Trim().Length == 0)
|
||
|
// {
|
||
|
// return EnumSendMail.SubjectMissing;
|
||
|
// }
|
||
|
|
||
|
// if (this.Body.Trim().Length == 0)
|
||
|
// {
|
||
|
// return EnumSendMail.BodyMissing;
|
||
|
// }
|
||
|
|
||
|
// //_client = new SmtpClient(_server);
|
||
|
// string s1 = Ease.Core.Utility.ConfigUtility.GetAppSettings("From");
|
||
|
// string s2 = Ease.Core.Utility.ConfigUtility.GetAppSettings("Password");
|
||
|
|
||
|
// //if (_server.Length > 0)
|
||
|
// //{
|
||
|
// // if (s1 != "" && s2 != "")
|
||
|
// // _client.Credentials = new System.Net.NetworkCredential(s1, s2);
|
||
|
// // //_client.Send(mailMessage);
|
||
|
// //}
|
||
|
// //else
|
||
|
// //{
|
||
|
// // return EnumSendMail.ServerNotFound;
|
||
|
// //}
|
||
|
|
||
|
// // _client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
|
||
|
|
||
|
// SendAsync();
|
||
|
// }
|
||
|
// catch (Exception ex)
|
||
|
// {
|
||
|
// //_errMsg = ex.Message + ex.InnerException;
|
||
|
// return EnumSendMail.SendError;
|
||
|
// }
|
||
|
// return EnumSendMail.SuccessFullySend;
|
||
|
//}
|
||
|
|
||
|
//public EnumSendMail SendMail(Employee toEmp)
|
||
|
//{
|
||
|
// //_toEmployee = new Employee();
|
||
|
// //_toEmployee = toEmp;
|
||
|
// _to = toEmp.EmailAddress;
|
||
|
|
||
|
// return SendMail();
|
||
|
//}
|
||
|
|
||
|
//public EnumSendMail SendMail(List<Employee> toEmps)
|
||
|
//{
|
||
|
// foreach (Employee emp in toEmps)
|
||
|
// {
|
||
|
// _to = _to + emp.EmailAddress + ";";
|
||
|
// }
|
||
|
// if (_to.Length > 0)
|
||
|
// {
|
||
|
// _to = _to.Substring(0, _to.Length - 1);
|
||
|
// }
|
||
|
|
||
|
// return SendMail();
|
||
|
//}
|
||
|
|
||
|
|
||
|
//public void SendMailForNovartis()
|
||
|
//{
|
||
|
// //Mail Sending using SMTP
|
||
|
// try
|
||
|
// {
|
||
|
// ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
// MailMessage mail = new MailMessage();
|
||
|
// string To = _to;
|
||
|
// mail.To.Add(To);
|
||
|
// //string From = Ease.Core.Utility.ConfigUtility.GetAppSettings("FromAddress");
|
||
|
// //string userName = Ease.Core.Utility.ConfigUtility.GetAppSettings("UserID");
|
||
|
// //string password = Ease.Core.Utility.ConfigUtility.GetAppSettings("Password");
|
||
|
// //string sServer = Ease.Core.Utility.ConfigUtility.GetAppSettings("EmailServer");
|
||
|
// //string sPort = Ease.Core.Utility.ConfigUtility.GetAppSettings("PortNumber");
|
||
|
// int portNumber = Convert.ToInt32(sPort);
|
||
|
// //Added by Ashek 25 Nov 2013
|
||
|
// password = Ease.Core.Utility.Global.CipherFunctions.Decrypt("Cel.Admin", password);
|
||
|
// mail.Subject = _subject;
|
||
|
// mail.Body = _body;
|
||
|
// mail.From = new MailAddress(From);
|
||
|
// mail.IsBodyHtml = true;
|
||
|
// SmtpClient oClient = new SmtpClient(sServer, portNumber);
|
||
|
// oClient.EnableSsl = false;
|
||
|
// oClient.UseDefaultCredentials = false;
|
||
|
// if (userName != "")
|
||
|
// oClient.Credentials = new System.Net.NetworkCredential(userName, password);
|
||
|
// ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
// oClient.Send(mail);
|
||
|
// }
|
||
|
// catch (Exception ex)
|
||
|
// {
|
||
|
// throw new ServiceException(ex.ToString());
|
||
|
// }
|
||
|
//}
|
||
|
|
||
|
|
||
|
public void ThreadSendMail(EmailSettings emailSettings)
|
||
|
{
|
||
|
Thread myNewThread = new Thread(() => SendMail(emailSettings));
|
||
|
myNewThread.Start();
|
||
|
|
||
|
// new WFMovementTranService().UpdateSysNotification(next.ID, true);
|
||
|
|
||
|
}
|
||
|
|
||
|
public void SendMail(EmailSettings emailSettings)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
string sServer = emailSettings.EmailServer;
|
||
|
string userID = emailSettings.UserID;
|
||
|
string checkSendMail = emailSettings.SendMethod;
|
||
|
string password = emailSettings.Password;
|
||
|
string PortNumber = emailSettings.PortNumber;
|
||
|
string CheckSSL = emailSettings.EnableSSL;
|
||
|
|
||
|
password = Ease.Core.Utility.Global.CipherFunctions.Decrypt("Cel.Admin", password);
|
||
|
//password = "";
|
||
|
|
||
|
_body = _body.Trim(' ').TrimEnd('.', ',').TrimEnd('.', ',') + AddLink(_body);
|
||
|
_from = _from == "" ? emailSettings.FromAddress : _from;
|
||
|
|
||
|
if (checkSendMail.ToUpper() == "SMTP")
|
||
|
{
|
||
|
MailMessage mailMessage = null;
|
||
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
|
||
|
client = new SmtpClient(sServer, PortNumber != string.Empty ? Convert.ToInt32(PortNumber) : 25);
|
||
|
client.EnableSsl = CheckSSL.ToUpper() == "YES" ? true : false;
|
||
|
|
||
|
if (sServer.Length > 0)
|
||
|
{
|
||
|
client.UseDefaultCredentials = true;
|
||
|
if (userID != "" && password != "")
|
||
|
{
|
||
|
client.UseDefaultCredentials = false;
|
||
|
client.Credentials = new System.Net.NetworkCredential(userID, password);
|
||
|
}
|
||
|
|
||
|
mailMessage = new MailMessage(_from, _to, _subject, _body);
|
||
|
mailMessage.IsBodyHtml = true;
|
||
|
|
||
|
if (_cc.Length > 0) {
|
||
|
|
||
|
if(_cc.Contains(';'))
|
||
|
{
|
||
|
string[] ccEmails = _cc.Split(';');
|
||
|
|
||
|
foreach (string ccEmail in ccEmails)
|
||
|
{
|
||
|
mailMessage.CC.Add(ccEmail);
|
||
|
}
|
||
|
}
|
||
|
else mailMessage.CC.Add(_cc);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
if (_attachments.Length > 0) { mailMessage.Attachments.Add(new System.Net.Mail.Attachment(_attachments)); }
|
||
|
|
||
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
client.Send(mailMessage);
|
||
|
mailMessage.Dispose();
|
||
|
client.Dispose();
|
||
|
}
|
||
|
}
|
||
|
#region EWSUrl
|
||
|
/*else if (Enum.IsDefined(typeof(ExchangeVersion), checkSendMail))
|
||
|
{
|
||
|
|
||
|
ExchangeVersion enmExchangeVersion = (ExchangeVersion)Enum.Parse(typeof(ExchangeVersion), checkSendMail);
|
||
|
|
||
|
ExchangeService oService = new ExchangeService(enmExchangeVersion);
|
||
|
EmailMessage oMessage;
|
||
|
|
||
|
//oService.AutodiscoverUrl(_from, delegate { return true; });
|
||
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
|
||
|
oService.UseDefaultCredentials = false;
|
||
|
if (userID != "" && password != "")
|
||
|
{
|
||
|
oService.UseDefaultCredentials = false;
|
||
|
oService.Credentials = new NetworkCredential(userID, password, sServer);
|
||
|
}
|
||
|
|
||
|
if (EWSUrl != string.Empty)
|
||
|
{
|
||
|
oService.Url = new Uri(EWSUrl);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
oService.AutodiscoverUrl(_from);
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
oService.AutodiscoverUrl(_from, (calbackURL) => calbackURL.Trim().ToLower().StartsWith("https://"));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*oMessage = new EmailMessage(oService);
|
||
|
oMessage.Subject = _subject;
|
||
|
oMessage.Body = _body;
|
||
|
oMessage.ToRecipients.Add(_to);
|
||
|
oMessage.From = new EmailAddress(_from);
|
||
|
if (!string.IsNullOrEmpty(_attachments))
|
||
|
oMessage.Attachments.AddFileAttachment(Attachments);
|
||
|
oMessage.Send();#1#
|
||
|
|
||
|
}*/
|
||
|
#endregion
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
sb.Append(ex.Message);
|
||
|
Exception innerException = ex.InnerException;
|
||
|
while (innerException != null)
|
||
|
{
|
||
|
sb.Append("--> inner Exception: ");
|
||
|
sb.Append(innerException.Message);
|
||
|
innerException = innerException.InnerException;
|
||
|
}
|
||
|
|
||
|
throw new MailSenderException(sb.ToString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void SendMail(EmailSettings emailSettings,Attachment att)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
string sServer = emailSettings.EmailServer;
|
||
|
string userID = emailSettings.UserID;
|
||
|
string checkSendMail = emailSettings.SendMethod; ;
|
||
|
string password = emailSettings.Password;
|
||
|
string PortNumber = emailSettings.PortNumber;
|
||
|
string CheckSSL = emailSettings.EnableSSL;
|
||
|
|
||
|
password = Ease.Core.Utility.Global.CipherFunctions.Decrypt("Cel.Admin", password);
|
||
|
//password = "";
|
||
|
|
||
|
_body = _body.Trim(' ').TrimEnd('.', ',').TrimEnd('.', ',') + AddLink(_body);
|
||
|
_from = _from == "" ? emailSettings.FromAddress : _from;
|
||
|
|
||
|
if (checkSendMail.ToUpper() == "SMTP")
|
||
|
{
|
||
|
MailMessage mailMessage = null;
|
||
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
|
||
|
client = new SmtpClient(sServer, PortNumber != string.Empty ? Convert.ToInt32(PortNumber) : 25);
|
||
|
client.EnableSsl = CheckSSL.ToUpper() == "YES" ? true : false;
|
||
|
|
||
|
if (sServer.Length > 0)
|
||
|
{
|
||
|
client.UseDefaultCredentials = true;
|
||
|
if (userID != "" && password != "")
|
||
|
{
|
||
|
client.UseDefaultCredentials = false;
|
||
|
client.Credentials = new System.Net.NetworkCredential(userID, password);
|
||
|
}
|
||
|
|
||
|
mailMessage = new MailMessage(_from, _to, _subject, _body);
|
||
|
mailMessage.IsBodyHtml = true;
|
||
|
mailMessage.Attachments.Add(att);
|
||
|
if (_cc.Length > 0) { mailMessage.CC.Add(_cc); }
|
||
|
if (_attachments.Length > 0) { mailMessage.Attachments.Add(new System.Net.Mail.Attachment(_attachments)); }
|
||
|
|
||
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
client.Send(mailMessage);
|
||
|
mailMessage.Dispose();
|
||
|
client.Dispose();
|
||
|
}
|
||
|
}
|
||
|
#region EWSUrl
|
||
|
/*else if (Enum.IsDefined(typeof(ExchangeVersion), checkSendMail))
|
||
|
{
|
||
|
|
||
|
ExchangeVersion enmExchangeVersion = (ExchangeVersion)Enum.Parse(typeof(ExchangeVersion), checkSendMail);
|
||
|
|
||
|
ExchangeService oService = new ExchangeService(enmExchangeVersion);
|
||
|
EmailMessage oMessage;
|
||
|
|
||
|
//oService.AutodiscoverUrl(_from, delegate { return true; });
|
||
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
|
||
|
oService.UseDefaultCredentials = false;
|
||
|
if (userID != "" && password != "")
|
||
|
{
|
||
|
oService.UseDefaultCredentials = false;
|
||
|
oService.Credentials = new NetworkCredential(userID, password, sServer);
|
||
|
}
|
||
|
|
||
|
if (EWSUrl != string.Empty)
|
||
|
{
|
||
|
oService.Url = new Uri(EWSUrl);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
oService.AutodiscoverUrl(_from);
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
oService.AutodiscoverUrl(_from, (calbackURL) => calbackURL.Trim().ToLower().StartsWith("https://"));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*oMessage = new EmailMessage(oService);
|
||
|
oMessage.Subject = _subject;
|
||
|
oMessage.Body = _body;
|
||
|
oMessage.ToRecipients.Add(_to);
|
||
|
oMessage.From = new EmailAddress(_from);
|
||
|
if (!string.IsNullOrEmpty(_attachments))
|
||
|
oMessage.Attachments.AddFileAttachment(Attachments);
|
||
|
oMessage.Send();#1#
|
||
|
|
||
|
}*/
|
||
|
#endregion
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
sb.Append(ex.Message);
|
||
|
Exception innerException = ex.InnerException;
|
||
|
while (innerException != null)
|
||
|
{
|
||
|
sb.Append("--> inner Exception: ");
|
||
|
sb.Append(innerException.Message);
|
||
|
innerException = innerException.InnerException;
|
||
|
}
|
||
|
|
||
|
throw new MailSenderException(sb.ToString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
//public EnumSendMail SendPaySlipMail()
|
||
|
//{
|
||
|
// EnumSendMail sendStatus = EnumSendMail.SuccessFullySend;
|
||
|
// try
|
||
|
// {
|
||
|
// string sServer = Ease.Core.Utility.ConfigUtility.GetAppSettings("EmailServer");
|
||
|
// string userID = Ease.Core.Utility.ConfigUtility.GetAppSettings("UserID");
|
||
|
// string checkSendMail = Ease.Core.Utility.ConfigUtility.GetAppSettings("SendMethod");
|
||
|
// string password = Ease.Core.Utility.ConfigUtility.GetAppSettings("Password");
|
||
|
// string PortNumber = Ease.Core.Utility.ConfigUtility.GetAppSettings("PortNumber");
|
||
|
// string CheckSSL = Ease.Core.Utility.ConfigUtility.GetAppSettings("EnableSSL");
|
||
|
// string EWSUrl = Ease.Core.Utility.ConfigUtility.GetAppSettings("EWSEndpoint");
|
||
|
|
||
|
// password = Ease.Core.Utility.Global.CipherFunctions.Decrypt("Cel.Admin", password);
|
||
|
|
||
|
// _body += AddLink();
|
||
|
// _from = _from == "" ? Ease.Core.Utility.ConfigUtility.GetAppSettings("FromAddress") : _from;
|
||
|
// if (string.IsNullOrEmpty(_from))
|
||
|
// {
|
||
|
// sendStatus = EnumSendMail.FromMissing;
|
||
|
// goto y;
|
||
|
// }
|
||
|
// if (string.IsNullOrEmpty(_to))
|
||
|
// {
|
||
|
// sendStatus = EnumSendMail.ToMissing;
|
||
|
// goto y;
|
||
|
// }
|
||
|
// if (checkSendMail.ToUpper() == "SMTP")
|
||
|
// {
|
||
|
// MailMessage mailMessage = null;
|
||
|
// ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
|
||
|
// client = new SmtpClient(sServer, PortNumber != string.Empty ? Convert.ToInt32(PortNumber) : 25);
|
||
|
// client.EnableSsl = CheckSSL.ToUpper() == "YES" ? true : false;
|
||
|
|
||
|
// if (sServer.Length > 0)
|
||
|
// {
|
||
|
// client.UseDefaultCredentials = true;
|
||
|
// if (userID != "" && password != "")
|
||
|
// {
|
||
|
// client.UseDefaultCredentials = false;
|
||
|
// client.Credentials = new System.Net.NetworkCredential(userID, password);
|
||
|
// }
|
||
|
|
||
|
// mailMessage = new MailMessage(_from, _to, _subject, _body);
|
||
|
// mailMessage.IsBodyHtml = true;
|
||
|
|
||
|
// if (_cc.Length > 0) { mailMessage.CC.Add(_cc); }
|
||
|
// if (_attachments.Length > 0) { mailMessage.Attachments.Add(new System.Net.Mail.Attachment(_attachments)); }
|
||
|
|
||
|
// ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
// client.Send(mailMessage);
|
||
|
// }
|
||
|
// }
|
||
|
// else if (Enum.IsDefined(typeof(ExchangeVersion), checkSendMail))
|
||
|
// {
|
||
|
|
||
|
// ExchangeVersion enmExchangeVersion = (ExchangeVersion)Enum.Parse(typeof(ExchangeVersion), checkSendMail);
|
||
|
|
||
|
// ExchangeService oService = new ExchangeService(enmExchangeVersion);
|
||
|
// EmailMessage oMessage;
|
||
|
|
||
|
// //oService.AutodiscoverUrl(_from, delegate { return true; });
|
||
|
// ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
|
||
|
// oService.UseDefaultCredentials = true;
|
||
|
// if (userID != "" && password != "")
|
||
|
// {
|
||
|
// oService.UseDefaultCredentials = false;
|
||
|
// oService.Credentials = new NetworkCredential(userID, password, sServer);
|
||
|
// }
|
||
|
|
||
|
// if (EWSUrl != string.Empty)
|
||
|
// {
|
||
|
// oService.Url = new Uri(EWSUrl);
|
||
|
// }
|
||
|
// else
|
||
|
// {
|
||
|
// try
|
||
|
// {
|
||
|
// oService.AutodiscoverUrl(_from);
|
||
|
// }
|
||
|
// catch (Exception)
|
||
|
// {
|
||
|
// oService.AutodiscoverUrl(_from, (calbackURL) => calbackURL.Trim().ToLower().StartsWith("https://"));
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
// oMessage = new EmailMessage(oService);
|
||
|
// oMessage.Subject = _subject;
|
||
|
// oMessage.Body = _body;
|
||
|
// oMessage.ToRecipients.Add(_to);
|
||
|
// oMessage.From = new EmailAddress(_from);
|
||
|
// if (!string.IsNullOrEmpty(_attachments))
|
||
|
// oMessage.Attachments.AddFileAttachment(Attachments);
|
||
|
// oMessage.Send();
|
||
|
|
||
|
|
||
|
// }
|
||
|
|
||
|
// }
|
||
|
// catch (Exception ex)
|
||
|
// {
|
||
|
// //StringBuilder sb = new StringBuilder();
|
||
|
// //sb.Append(ex.Message);
|
||
|
// //Exception innerException = ex.InnerException;
|
||
|
// //while (innerException != null)
|
||
|
// //{
|
||
|
// // sb.Append("--> inner Exception: ");
|
||
|
// // sb.Append(innerException.Message);
|
||
|
// // innerException = innerException.InnerException;
|
||
|
// //}
|
||
|
// //throw new MailSenderException(sb.ToString());
|
||
|
// sendStatus = EnumSendMail.SendError;
|
||
|
// goto y;
|
||
|
|
||
|
// }
|
||
|
|
||
|
//y:
|
||
|
// return sendStatus;
|
||
|
|
||
|
//}
|
||
|
|
||
|
//public void SendMail()
|
||
|
//{
|
||
|
|
||
|
// string s1="";
|
||
|
|
||
|
// try
|
||
|
// {
|
||
|
// //lock (this)
|
||
|
// //{
|
||
|
// if (_from == "")
|
||
|
// {
|
||
|
// s1 = Ease.Core.Utility.ConfigUtility.GetAppSettings("FromAddress");
|
||
|
// _from = s1;
|
||
|
// }
|
||
|
// else s1 = _from;
|
||
|
// //string s2 = Ease.Core.Utility.ConfigUtility.GetAppSettings("Password");
|
||
|
// string userID = Ease.Core.Utility.ConfigUtility.GetAppSettings("UserID");
|
||
|
// string password = Ease.Core.Utility.ConfigUtility.GetAppSettings("Password");
|
||
|
|
||
|
// string sServer = Ease.Core.Utility.ConfigUtility.GetAppSettings("EmailServer");
|
||
|
|
||
|
// _body += AddLink();
|
||
|
|
||
|
// mailMessage = new MailMessage(_from, _to, _subject, _body);
|
||
|
// mailMessage.IsBodyHtml = true;
|
||
|
|
||
|
// if (_cc.Length > 0)
|
||
|
// {
|
||
|
// mailMessage.CC.Add(_cc);
|
||
|
// }
|
||
|
// if (_attachments.Length > 0)
|
||
|
// mailMessage.Attachments.Add(new System.Net.Mail.Attachment(_attachments));
|
||
|
|
||
|
// string checkSendMail = Ease.Core.Utility.ConfigUtility.GetAppSettings("SendMethod");
|
||
|
|
||
|
// if (checkSendMail.ToUpper() == "SMTP")
|
||
|
// {
|
||
|
// ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
// mailMessage.From = new MailAddress(_from);
|
||
|
// mailMessage.To.Add(_to);
|
||
|
// mailMessage.Subject = _subject;
|
||
|
// mailMessage.Body = _body;
|
||
|
// mailMessage.IsBodyHtml = true;
|
||
|
// string PortNumber = Ease.Core.Utility.ConfigUtility.GetAppSettings("PortNumber");
|
||
|
// if (PortNumber != string.Empty)
|
||
|
// {
|
||
|
// client = new SmtpClient(sServer, Convert.ToInt32(PortNumber));
|
||
|
// }
|
||
|
|
||
|
// else
|
||
|
// {
|
||
|
// client = new SmtpClient(sServer, 25);
|
||
|
// }
|
||
|
// //Close Server Certification Validation
|
||
|
// //ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
|
||
|
|
||
|
// //client = new SmtpClient(sServer);
|
||
|
|
||
|
// string CheckSSL = Ease.Core.Utility.ConfigUtility.GetAppSettings("EnableSSL");
|
||
|
// if (CheckSSL.ToUpper() == "YES")
|
||
|
// client.EnableSsl = true;
|
||
|
// else
|
||
|
// client.EnableSsl = false;
|
||
|
// client.UseDefaultCredentials = false;
|
||
|
|
||
|
// if (sServer.Length > 0)
|
||
|
// {
|
||
|
// if (userID != "" && password != "")
|
||
|
// client.Credentials = new System.Net.NetworkCredential(s1, password);
|
||
|
// ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||
|
// client.Send(mailMessage);
|
||
|
// }
|
||
|
|
||
|
|
||
|
// }
|
||
|
// else
|
||
|
// {
|
||
|
// ExchangeService oService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
|
||
|
// EmailMessage oMessage;
|
||
|
// try
|
||
|
// {
|
||
|
// oService.AutodiscoverUrl(_from, delegate { return true; });
|
||
|
// oService.Credentials = new NetworkCredential(sServer, "clientvision", Environment.UserDomainName);
|
||
|
// oMessage = new EmailMessage(oService);
|
||
|
// oMessage.Subject = _subject;
|
||
|
// oMessage.Body = _body;
|
||
|
// oMessage.ToRecipients.Add(_to);
|
||
|
// oMessage.From = new EmailAddress(_from);
|
||
|
// oMessage.SendAndSaveCopy();
|
||
|
|
||
|
// }
|
||
|
// catch (System.Exception ex)
|
||
|
// {
|
||
|
// throw new System.Exception(ex.Message);
|
||
|
// }
|
||
|
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
// }
|
||
|
// catch (Exception ex)
|
||
|
// {
|
||
|
// throw new ServiceException("Cannot Send Mail :" + ex.Message);
|
||
|
// }
|
||
|
|
||
|
//}
|
||
|
public void AddCC(string cc)
|
||
|
{
|
||
|
if (IsValidEmail(cc))
|
||
|
{
|
||
|
if (_cc == "")
|
||
|
_cc += cc;
|
||
|
else
|
||
|
_cc += ";" + cc;
|
||
|
}
|
||
|
}
|
||
|
public void AddTo(string to)
|
||
|
{
|
||
|
if (IsValidEmail(to))
|
||
|
{
|
||
|
if (_to == "")
|
||
|
_to += to;
|
||
|
else
|
||
|
_to += to + ";";
|
||
|
}
|
||
|
}
|
||
|
public void AddAttachment(string attachment)
|
||
|
{
|
||
|
|
||
|
if (_attachments == "")
|
||
|
_attachments += attachment;
|
||
|
else
|
||
|
_attachments += attachment + ";";
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
public string AddLink()
|
||
|
{
|
||
|
if (_link.Length > 0)
|
||
|
{
|
||
|
//return ( Environment.NewLine + Environment.NewLine + Environment.NewLine +
|
||
|
// "<br>Please, click on the following link to take action." +
|
||
|
// Environment.NewLine + Environment.NewLine +
|
||
|
// "<a href=\"" + _link.ToString() + "\">Link</a> " +
|
||
|
// //"<" + _link.ToString() + ">" +
|
||
|
// Environment.NewLine + Environment.NewLine + Environment.NewLine +
|
||
|
// "<br><br><br><br>This is a computer generated mail. Please don't reply.<br>");
|
||
|
return (". Please click on this " + _link.ToString() + " " + " to take action in Luminous self-service (if require)." + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return "<br/><br/>This is a system generated mail, Please do not reply.";
|
||
|
}
|
||
|
}
|
||
|
public string AddLink(string sbody)
|
||
|
{
|
||
|
if (_link.Length > 0)
|
||
|
{
|
||
|
//return ( Environment.NewLine + Environment.NewLine + Environment.NewLine +
|
||
|
// "<br>Please, click on the following link to take action." +
|
||
|
// Environment.NewLine + Environment.NewLine +
|
||
|
// "<a href=\"" + _link.ToString() + "\">Link</a> " +
|
||
|
// //"<" + _link.ToString() + ">" +
|
||
|
// Environment.NewLine + Environment.NewLine + Environment.NewLine +
|
||
|
// "<br><br><br><br>This is a computer generated mail. Please don't reply.<br>");
|
||
|
if (sbody.Contains("recognition") && sbody.Contains("approved"))
|
||
|
{
|
||
|
return ("To see the details, please click on this " + _link.ToString() + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
else if (sbody.Contains("recognition"))
|
||
|
{
|
||
|
return ("To approve the nomination, please click on this " + _link.ToString() + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
else if (sbody.Contains("reverted"))
|
||
|
{
|
||
|
return (". Please click on this " + _link.ToString() + " " + " to access." + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
else if (sbody.Contains("Declined"))
|
||
|
{
|
||
|
return (". Please click on this " + _link.ToString() + " " + " to access." + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
else if (sbody.Contains("Recruitment"))
|
||
|
{
|
||
|
if ((sbody.Contains("Department") && sbody.Contains("Head")) || sbody.Contains("approved by HR"))
|
||
|
{
|
||
|
return (". To accept the request, please click on this " + _link.ToString() + " to access. <br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
return (". Please click on this " + _link.ToString() + " to access. <br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
else if (sbody.Contains("approved"))
|
||
|
{
|
||
|
return (". Please click on this " + _link.ToString() + " " + " to access." + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
else if (sbody.Contains("accepted"))
|
||
|
{
|
||
|
return (". Please click on this link for access: " + _link.ToString() + " " + " ." + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
else if (sbody.Contains("Attendance"))
|
||
|
{
|
||
|
return ( _link.ToString() + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
|
||
|
|
||
|
else
|
||
|
{
|
||
|
//return (". To approve the leave, please click on this " + _link.ToString()+"." + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
return (". To accept the reliever request during the mentioned period, please click on this " + _link.ToString() + "." + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
|
||
|
// return (". Please click on this " + _link.ToString() + " " + " to take action in Luminous self-service (if require)." + "<br/><br/>This is a system generated mail, Please do not reply.");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return "<br/><br/>This is a system generated mail, Please do not reply.";
|
||
|
}
|
||
|
}
|
||
|
public static bool IsValidEmail(string inputEmail)
|
||
|
{
|
||
|
int n1 = inputEmail.IndexOf("@");
|
||
|
int n2 = inputEmail.IndexOf(".");
|
||
|
int n4 = inputEmail.LastIndexOf(".");
|
||
|
int n3 = inputEmail.IndexOf(" ");
|
||
|
if (n3 > 0)
|
||
|
return false;
|
||
|
if (n2 < n4)
|
||
|
n2 = n4;
|
||
|
if (n1 < n2)
|
||
|
return true;
|
||
|
else
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||
|
{
|
||
|
// replace with proper validation
|
||
|
if (sslPolicyErrors == SslPolicyErrors.None)
|
||
|
return true;
|
||
|
else
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
//circular
|
||
|
//public void SendMailForAttendance(Employee oEmp)
|
||
|
//{
|
||
|
// try
|
||
|
// {
|
||
|
// checkSendMail = Ease.Core.Utility.ConfigUtility.GetAppSettings("SendMethod");
|
||
|
// MailSender sender = new MailSender();
|
||
|
// List<Employee> _employees = Employee.Get();
|
||
|
// oEmpDetails = Employee.GetEmpDetailsView();
|
||
|
// dRow = oEmpDetails.Tables[0].AsEnumerable().Where(x => Convert.ToString(x["EmployeeNo"]) == oEmp.EmployeeNo.Trim()).FirstOrDefault();
|
||
|
// if (dRow != null)
|
||
|
// {
|
||
|
// oLineManager = _employees.Where(o => o.EmployeeNo == dRow["LineManagerNo"].ToString().Trim()).FirstOrDefault();
|
||
|
// if (oLineManager != null)
|
||
|
// {
|
||
|
// sbody = " Dear " + oLineManager.Name + ", " + "<br/><br/>" + oEmp.Name + " has submitted a Attendance Regularization request. To accept the request, please click on this " + "" + "<br/><br/>";
|
||
|
// mailTitle = "Incomplete Regularization ";
|
||
|
// SetProperty(mailTitle, sbody, oLineManager.EmailAddress);
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// catch (Exception ex)
|
||
|
// {
|
||
|
// throw new ServiceException(ex.ToString());
|
||
|
// }
|
||
|
//}
|
||
|
|
||
|
public void SetProperty(string mailSubject, string mailDescription, string emailAddress)
|
||
|
{
|
||
|
if (emailAddress != string.Empty)
|
||
|
{
|
||
|
sender = new MailSender();
|
||
|
sender.AddTo(emailAddress);
|
||
|
sender.Subject = mailSubject;
|
||
|
sender.Body = mailDescription;
|
||
|
sender.Link = "http://115.127.93.53:8088/default.aspx"; // Ease.Core.Utility.ConfigUtility.GetAppSettings("WebAddress");
|
||
|
//if (checkSendMail != "")
|
||
|
// sender.SendMail();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void SendLHEmail(string emailTo, string emailFrom, string subject, string emailBody)
|
||
|
{
|
||
|
//System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
|
||
|
//mail.From = emailFrom;
|
||
|
//mail.Subject = subject;
|
||
|
//mail.BodyFormat = System.Web.Mail.MailFormat.Html;
|
||
|
//mail.Body = emailBody;
|
||
|
//mail.To = emailTo;
|
||
|
//System.Web.Mail.SmtpMail.SmtpServer = Ease.Core.Utility.ConfigUtility.GetAppSettings("EmailServer"); ;
|
||
|
//System.Web.Mail.SmtpMail.Send(mail);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class MailSenderException : Exception
|
||
|
{
|
||
|
|
||
|
public MailSenderException(string message)
|
||
|
: base("Failed to Send Mail due to:" + message)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
public MailSenderException(string message, Exception innerException)
|
||
|
: base("Failed to Send Mail due to:" + message, innerException)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|