飘叶
飘零的叶子,也要享受生活
posts - 476,  comments - 958,  trackbacks - 0
日志记录接口:(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/>&nbsp;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生活

FeedBack:
# re: 邮件发送错误记录日志
2007-11-28 14:26 | Jerome
看不懂,貌似C#  回复  更多评论
  

<2007年11月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

常用链接

留言簿(9)

随笔分类(473)

随笔档案(474)

文章分类(17)

文章档案(16)

收藏夹(5)

好友Blog

名人博客

求职

网上杂志

医生博客

有关IT信息

在线工具

在线听歌

最新随笔

搜索

  •  

积分与排名

  • 积分 - 169630
  • 排名 - 19

最新评论

阅读排行榜

评论排行榜