Hashtable MailContents = new Hashtable();
MailContents["[[Outcome]]"] = "Approved";
MailContents["[[Approver]]"] = Approver.Name;
MailContents["[[Subject]]"] = "A Request is Approved by " + Approver.Name;
MailContents["[[To]]"] = Convert.ToString(Author.Email);
MailContents["[[User]]"] = Author.Name;
MailContents["[[Comments]]"] = txtCommnets.Text;
MailContents["[[Link]]"] = URL.Remove(0, 7);
SendEmailFromTemplate("key", MailContents, SPContext.Current.Web.Url);
MailContents.Clear();
public static void SendEmailFromTemplate(string KeyName, Hashtable KeyValue, string SiteUrl)
{
try
{
SPItem itemMailTemplate = getMailTemplate(KeyName, SiteUrl);
string strFrom = Convert.ToString(itemMailTemplate["From"]);
string strTo = Convert.ToString(itemMailTemplate["To"]);
string strCc = Convert.ToString(itemMailTemplate["CC"]);
string strSubject = Convert.ToString(itemMailTemplate["Subject"]);
string strBody = Convert.ToString(itemMailTemplate["Body"]);
// Check if From Field is Dynamic
if (strFrom.Contains("[[From]]"))
{
strFrom = strFrom.Replace("[[From]]", Convert.ToString(KeyValue["[[From]]"]));
}
// Check if To Field is Dynamic
if (strTo.Contains("[[To]]"))
{
strTo = strTo.Replace("[[To]]", Convert.ToString(KeyValue["[[To]]"]));
}
// Check if CC Field is Dynamic
if (strCc.Contains("[[CC]]"))
{
strCc = strCc.Replace("[[CC]]", Convert.ToString(KeyValue["[[CC]]"]));
}
foreach (DictionaryEntry Item in KeyValue)
{
strSubject = strSubject.Replace(Item.Key.ToString(), Convert.ToString(Item.Value));
strBody = strBody.Replace(Item.Key.ToString(), Convert.ToString(Item.Value));
}
SendEmail(strFrom, strTo, strCc, string.Empty, strSubject, strBody, null, null);
}
catch (Exception ex)
{
throw ex;
}
}
protected static SPItem getMailTemplate(string key, string siteUrl)
{
SPItem itemMailTemplate = null;
try
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["TempltesList"];
SPQuery query = new SPQuery();
query.Query = "<OrderBy>" +
"<FieldRef Name='Value' />" +
"</OrderBy>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='Title' />" +
"<Value Type='Text'>" + key + "</Value>" +
"</Eq>" +
"</Where>";
SPListItemCollection itemColl = list.GetItems(query);
if (itemColl != null && itemColl.Count > 0)
{
itemMailTemplate = itemColl[0];
}
else
{
throw new Exception("Key doesn't exist in the TempltesList ");
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return itemMailTemplate;
}
public static void SendEmail(string from, string to, string cc, string bcc, string subject, string body, SPFolder attachments, string SMTPServer)
{
try
{
if (string.IsNullOrEmpty(SMTPServer))
SMTPServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
SmtpClient client = new SmtpClient(SMTPServer);
MailMessage mail = new MailMessage();
string[] strArrTo = to.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
string[] strArrCC = cc.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
string[] strArrBCC = bcc.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
client.UseDefaultCredentials = true;
client.EnableSsl = false;
if (string.IsNullOrEmpty(from))
{
from = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;
}
mail.From = new MailAddress(from);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
foreach (string strTo in strArrTo)
{
if (!string.IsNullOrEmpty(strTo))
{
mail.To.Add(new MailAddress(strTo));
}
}
foreach (string strCC in strArrCC)
{
if (!string.IsNullOrEmpty(strCC))
{
mail.CC.Add(new MailAddress(strCC));
}
}
foreach (string strBCC in strArrBCC)
{
if (!string.IsNullOrEmpty(strBCC))
{
mail.Bcc.Add(new MailAddress(strBCC));
}
}
if (attachments != null && attachments.Files.Count > 0)
{
try
{
foreach (SPFile file in attachments.Files)
{
mail.Attachments.Add(new Attachment(file.OpenBinaryStream(), file.Name));
}
}
catch (Exception ex)
{
throw ex; }
}
client.Send(mail);
}
catch (SmtpException ex)
{
throw ex;
}
catch (InvalidOperationException ioe)
{
throw ioe;
}
}
MailContents["[[Outcome]]"] = "Approved";
MailContents["[[Approver]]"] = Approver.Name;
MailContents["[[Subject]]"] = "A Request is Approved by " + Approver.Name;
MailContents["[[To]]"] = Convert.ToString(Author.Email);
MailContents["[[User]]"] = Author.Name;
MailContents["[[Comments]]"] = txtCommnets.Text;
MailContents["[[Link]]"] = URL.Remove(0, 7);
SendEmailFromTemplate("key", MailContents, SPContext.Current.Web.Url);
MailContents.Clear();
public static void SendEmailFromTemplate(string KeyName, Hashtable KeyValue, string SiteUrl)
{
try
{
SPItem itemMailTemplate = getMailTemplate(KeyName, SiteUrl);
string strFrom = Convert.ToString(itemMailTemplate["From"]);
string strTo = Convert.ToString(itemMailTemplate["To"]);
string strCc = Convert.ToString(itemMailTemplate["CC"]);
string strSubject = Convert.ToString(itemMailTemplate["Subject"]);
string strBody = Convert.ToString(itemMailTemplate["Body"]);
// Check if From Field is Dynamic
if (strFrom.Contains("[[From]]"))
{
strFrom = strFrom.Replace("[[From]]", Convert.ToString(KeyValue["[[From]]"]));
}
// Check if To Field is Dynamic
if (strTo.Contains("[[To]]"))
{
strTo = strTo.Replace("[[To]]", Convert.ToString(KeyValue["[[To]]"]));
}
// Check if CC Field is Dynamic
if (strCc.Contains("[[CC]]"))
{
strCc = strCc.Replace("[[CC]]", Convert.ToString(KeyValue["[[CC]]"]));
}
foreach (DictionaryEntry Item in KeyValue)
{
strSubject = strSubject.Replace(Item.Key.ToString(), Convert.ToString(Item.Value));
strBody = strBody.Replace(Item.Key.ToString(), Convert.ToString(Item.Value));
}
SendEmail(strFrom, strTo, strCc, string.Empty, strSubject, strBody, null, null);
}
catch (Exception ex)
{
throw ex;
}
}
protected static SPItem getMailTemplate(string key, string siteUrl)
{
SPItem itemMailTemplate = null;
try
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["TempltesList"];
SPQuery query = new SPQuery();
query.Query = "<OrderBy>" +
"<FieldRef Name='Value' />" +
"</OrderBy>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='Title' />" +
"<Value Type='Text'>" + key + "</Value>" +
"</Eq>" +
"</Where>";
SPListItemCollection itemColl = list.GetItems(query);
if (itemColl != null && itemColl.Count > 0)
{
itemMailTemplate = itemColl[0];
}
else
{
throw new Exception("Key doesn't exist in the TempltesList ");
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return itemMailTemplate;
}
public static void SendEmail(string from, string to, string cc, string bcc, string subject, string body, SPFolder attachments, string SMTPServer)
{
try
{
if (string.IsNullOrEmpty(SMTPServer))
SMTPServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
SmtpClient client = new SmtpClient(SMTPServer);
MailMessage mail = new MailMessage();
string[] strArrTo = to.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
string[] strArrCC = cc.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
string[] strArrBCC = bcc.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
client.UseDefaultCredentials = true;
client.EnableSsl = false;
if (string.IsNullOrEmpty(from))
{
from = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;
}
mail.From = new MailAddress(from);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
foreach (string strTo in strArrTo)
{
if (!string.IsNullOrEmpty(strTo))
{
mail.To.Add(new MailAddress(strTo));
}
}
foreach (string strCC in strArrCC)
{
if (!string.IsNullOrEmpty(strCC))
{
mail.CC.Add(new MailAddress(strCC));
}
}
foreach (string strBCC in strArrBCC)
{
if (!string.IsNullOrEmpty(strBCC))
{
mail.Bcc.Add(new MailAddress(strBCC));
}
}
if (attachments != null && attachments.Files.Count > 0)
{
try
{
foreach (SPFile file in attachments.Files)
{
mail.Attachments.Add(new Attachment(file.OpenBinaryStream(), file.Name));
}
}
catch (Exception ex)
{
throw ex; }
}
client.Send(mail);
}
catch (SmtpException ex)
{
throw ex;
}
catch (InvalidOperationException ioe)
{
throw ioe;
}
}
No comments:
Post a Comment