日志记录接口:(ILogService.cs)
public interface ILogService
{
void Write(string message);
}
日志记录接口(实现):(FileLogService.cs)
public class FileLogService : ILogService
{
private static object lockObj = new object();
#region ILogService 成员
public void Write(string message)
{
lock (lockObj)
{
try
{
string logFile = HttpContext.Current.Server.MapPath(string.Format("~/{0:yyyy-MM-dd}.log", DateTime.Now));
FileInfo fi = new FileInfo(logFile);
StreamWriter sw = null;
try
{
if (fi.Exists)
sw = fi.AppendText();
else
sw = fi.CreateText();
sw.WriteLine(">>> " + DateTime.Now + " <<<");
sw.WriteLine(message);
sw.WriteLine();
sw.Flush();
}
finally
{
if (sw != null)
sw.Close();
}
}
catch
{
// do nothing.
}
}
}
#endregion
}
发送邮件的接口:
public class MailServiceImpl : IMailService
{
private string SmtpServer;
private string MailFormAddress;
private string MailUserName;
private string MailPassWord;
public MailServiceImpl()
{
//读取Email和接收邮件地址
SmtpServer = ConfigurationManager.AppSettings["SMTPServer"].ToString();
MailFormAddress = ConfigurationManager.AppSettings["MailFromAddress"].ToString();
MailUserName = ConfigurationManager.AppSettings["MailUserName"].ToString();
MailPassWord = ConfigurationManager.AppSettings["MailPassWord"].ToString();
}
#region IMailService 成员
public bool SendResetPasswordLinkMail(string receiveEmail, string username, string resetlink)
{
try
{
SmtpClient client = null;
string strFrom = MailFormAddress;
string strTo = receiveEmail;
string strBody = string.Format("Dear user: <br/>your accounts password reset request has been accepted.please click the following URL link to get your new password.<br/><a href='{0}'>{1}</a><br/> this link will expire in 24 hour.", resetlink, resetlink);
string strSubject = "reset password link";
//发送邮件
client = new SmtpClient(SmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassWord);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailmessage = new MailMessage(strFrom, strTo, strSubject, strBody);
mailmessage.BodyEncoding = System.Text.Encoding.UTF8;
mailmessage.IsBodyHtml = true;
client.Send(mailmessage);
return true;
}
catch (Exception ex)
{
LogException(ex);
return false;
}
}
private void LogException(Exception ex)
{
string msg = string.Format("Error Message: {0}\r\n{1}", ex.Message, ex.StackTrace);
ServiceLocator.LogService.Write(msg);
}
/// <summary>
/// 修改密码后发邮件
/// </summary>
/// <param name="receiveEmail"></param>
/// <param name="newPassword"></param>
/// <returns></returns>
public bool SendSetPasswordSucessflyMail(string receiveEmail, string username, string newPassword)
{
try
{
SmtpClient client = null;
string strFrom = MailFormAddress;
string strTo = receiveEmail;
string strBody = "your new password is:" + newPassword;
string strSubject = "reset password!";
//发送邮件
client = new SmtpClient(SmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassWord);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailmessage = new MailMessage(strFrom, strTo, strSubject, strBody);
mailmessage.BodyEncoding = System.Text.Encoding.UTF8;
mailmessage.IsBodyHtml = true;
client.Send(mailmessage);
return true;
}
catch (Exception ex)
{
LogException(ex);
return false;
}
}
/// <summary>
/// 反馈发邮件
/// </summary>
/// <param name="receiveEmail"></param>
/// <param name="content"></param>
/// <returns></returns>
public bool SendFeedbackMail(string sendEmail, string subject, string content)
{
try
{
SmtpClient client = null;
string strFrom = MailFormAddress;
string strTo = "Endusercare@lenovo.com";
string strBody = string.Format("Email: {0}<br/><br/>Feedback: {1}", sendEmail, content);
string strSubject = "GIS Portal Feedback: " + subject;
//发送邮件
client = new SmtpClient(SmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassWord);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailmessage = new MailMessage(strFrom, strTo, strSubject, strBody);
mailmessage.BodyEncoding = System.Text.Encoding.UTF8;
mailmessage.IsBodyHtml = true;
client.Send(mailmessage);
return true;
}
catch (Exception ex)
{
LogException(ex);
return false;
}
}
#endregion
posted on 2007-11-19 16:09
飘叶 阅读(117)
评论(1) 编辑 收藏 所属分类:
IT生活