115 lines
3.7 KiB
C#
115 lines
3.7 KiB
C#
using ASPNET.StarterKit.BusinessLogicLayer;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Mail;
|
|
using System.Reflection;
|
|
using System.Reflection.Emit;
|
|
using System.Runtime.Caching;
|
|
using System.Text;
|
|
using System.Web;
|
|
using System.Web.UI;
|
|
using System.Web.UI.WebControls;
|
|
/// <summary>
|
|
/// Summary description for SmtpMailManager
|
|
/// </summary>
|
|
public static class SmtpMailManager
|
|
{
|
|
|
|
|
|
public static bool sendEmail(AppUser u,UserControl template)
|
|
|
|
{
|
|
ObjectCache cache = MemoryCache.Default;
|
|
|
|
string fromAddress = (string) cache["RegFromEmailAddress"];
|
|
string ActivationUrl = Utils.GetCleanUrl();
|
|
if (HttpContext.Current.Request.Url.Port == 80 || HttpContext.Current.Request.Url.Port == 443)
|
|
ActivationUrl = string.Format(ActivationUrl + "/TimeTracker/Activate.aspx?Reg={0}&Email={1}",
|
|
Utils.toBase64(u.UserId.ToString()), Utils.toBase64(u.UserName));
|
|
else
|
|
ActivationUrl = string.Format(ActivationUrl + "/TimeTracker/Activate.aspx?Reg={0}&Email={1}",
|
|
Utils.toBase64(u.UserId.ToString()), Utils.toBase64(u.UserName));
|
|
|
|
|
|
try
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringWriter sw = new StringWriter(sb);
|
|
Html32TextWriter htw = new Html32TextWriter(sw);
|
|
template.RenderControl(htw);
|
|
string siteTitle = cache["SiteTitle"] as string;
|
|
string productname = cache["ProductName"] as string;
|
|
string emailSubject = cache["RegEmailSubject"] as string;
|
|
string body = sb.ToString();
|
|
body = body.Replace("%Title%", productname).Replace("%link%", ActivationUrl);
|
|
SmtpMailManager.sendMail(u.UserName, fromAddress, body, emailSubject);
|
|
}
|
|
catch (Exception emailEx)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public static void sendMail(string to, string from, string body,string subject)
|
|
{
|
|
MailMessage email = new MailMessage();
|
|
try
|
|
{
|
|
|
|
email.To.Add(to);
|
|
email.Subject = subject;
|
|
email.From = new MailAddress(from);
|
|
email.IsBodyHtml = true;
|
|
email.Body = body;
|
|
SmtpClient emailSvr = new SmtpClient();
|
|
emailSvr.Send(email);
|
|
}
|
|
catch (Exception dds)
|
|
{
|
|
SaveMailMessage(email, to);
|
|
throw dds;
|
|
}
|
|
}
|
|
public static void SaveMailMessage(this MailMessage msg,string emailaddress)
|
|
{
|
|
ObjectCache cache = MemoryCache.Default;
|
|
|
|
string captureEmailPath = (string) cache["EmailSendFailurePath"];
|
|
string root = HttpRuntime.AppDomainAppPath;
|
|
string path= Path.Combine(root, captureEmailPath);
|
|
//path = Path.Combine(path, emailaddress + ".eml");
|
|
if (!string.IsNullOrEmpty(path))
|
|
{
|
|
|
|
ToEMLStream(msg, path);
|
|
}
|
|
}
|
|
public static void ToEMLStream(this MailMessage msg,string tempFolder)
|
|
{
|
|
using (var client = new SmtpClient())
|
|
{
|
|
ObjectCache cache = MemoryCache.Default;
|
|
|
|
string appname = (string) cache["ApplicationName"];
|
|
|
|
tempFolder = Path.Combine(tempFolder, appname);
|
|
|
|
if (!Directory.Exists(tempFolder))
|
|
{
|
|
Directory.CreateDirectory(tempFolder);
|
|
}
|
|
|
|
client.UseDefaultCredentials = true;
|
|
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
|
|
client.PickupDirectoryLocation = tempFolder;
|
|
client.Send(msg);
|
|
|
|
}
|
|
}
|
|
}
|