点击回首页
我的浏览记录 | | 帮助?
当前位置:

源码截图

源码目录树

当前路径:InsApp/Email/Email.cs     using System;
using System.Configuration;
using System.ComponentModel;

using System.Net.Mail;
using System.Net;
using System.Web;

namespace InsApp.Email
{
    /// <summary>
    /// SendEmail 的摘要说明
    /// </summary>
    public class Email
    {

        private static readonly string emailAddress = ConfigurationManager.AppSettings["EmailAddress"];
        private static readonly string emailPassword = ConfigurationManager.AppSettings["EmaiPassword"];
        private static readonly string emailHost = ConfigurationManager.AppSettings["EmaiHost"];
        //website dns
        private static readonly string displayName = GetDns(HttpContext.Current);

        //线程安全的Singleton设计模式
        private static class LazyHolder
        {
            public static readonly Email Instance = new Email();
        }
        private Email() { }

        public static Email GetInstance()
        {
            return LazyHolder.Instance;
        }


        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="msg">邮件内容</param>
        /// <param name="host">发信的主机</param>
        /// <param name="email">发信用户名</param>
        /// <param name="password">密码</param>
        public void Send(string toEmail, string emailContent)
        {
            try
            {
                SmtpClient MailObj = new SmtpClient();
                MailMessage Msg = GetEmailMsg(toEmail, emailContent);
                MailObj.Host = emailHost;     //发信主机  // < - - check with your hosting provider on what to put in here
                MailObj.Credentials = new NetworkCredential(emailAddress, emailPassword);
                MailObj.SendCompleted += new  SendCompletedEventHandler(SendCompletedCallback);
                MailObj.EnableSsl = true;           //设置后,使用gmail
                //string userState = "test message1";
                //MailObj.SendAsync(Msg,userState);
                MailObj.Send(Msg);
                Msg.Dispose();
            }
            catch (Exception ex)
            { throw ex; }

        }

        /// <summary>
        /// 返回MailMessage
        /// </summary>
        /// <param name="toEmail">目的地</param>
        /// <param name="emailContent">内容</param>
        /// <returns></returns>
        private MailMessage GetEmailMsg(string toEmail, string emailContent)
        {
            string fromEmail = emailAddress;
            return GetEmailMsg(toEmail, fromEmail, emailContent);

        }
        /// <summary>
        /// 返回MailMessage
        /// </summary>
        /// <param name="toEmail">目的地</param>
        /// <param name="fromEmail">来源</param>
        /// <param name="emailContent">内容</param>
        /// <returns>MailMessage</returns>


        private MailMessage GetEmailMsg(string toEmail, string fromEmail, string emailContent)
        {

            MailMessage Msg = new MailMessage();
            Msg.To.Add(toEmail);
            Msg.From = new MailAddress(fromEmail, displayName);             //displayName       Title 
            Msg.Subject = "欢迎!" + displayName;                     //找到本网站URL
            Msg.SubjectEncoding = System.Text.Encoding.UTF8;
            Msg.Body = GetBody(toEmail, emailContent);
            Msg.BodyEncoding= System.Text.Encoding.UTF8;

            Msg.IsBodyHtml = false;

            return Msg;
        }


        /// <summary>
        /// 返回要发送邮件的body
        /// </summary>
        /// <param name="toEmail">目的地</param>
        /// <param name="emailContent">用户评论内容</param>
        /// <returns>body字符串</returns>

        private string GetBody(string toEmail, string emailContent)
        {

            string sMsg = string.Empty;

            sMsg += "你受到一封来自 " + displayName;//找到本网站的URL
            sMsg += " -----------------------------------\n";
            sMsg += "电子邮件:" + toEmail + "\n";
            sMsg += "内  容:" + emailContent + "\n";

            return sMsg;

        }

        static bool mailSent = false;
        public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            String token = (string)e.UserState;

            if (e.Cancelled)
            {
                Console.WriteLine("[{0}] Send canceled.", token);
            }
            if (e.Error != null)
            {
                Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
            }
            else
            {
                Console.WriteLine("Message sent.");
            }
            mailSent = true;
        }


        #region 获的网站的域名
        /// <summary>
        /// 获得Dns
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns>返回字符串DNS</returns>
        static string GetDns(HttpContext context)
        {
            string result = String.Empty;
            string url = string.Empty;
            string no_http = string.Empty;

            if (context == null) return result;


            if (null == result || result == String.Empty)
                url = HttpContext.Current.Request.Url.ToString();  //获取当前页的URL
            no_http = url.Substring(url.IndexOf("//") + 2);//截取去掉HTTP://的URL 
            result = "http://" + no_http.Substring(0, no_http.IndexOf("/") + 1);

            return result;

        }
        #endregion

    }
}
关于我们 | 顾问团队 | 发展历程 | 联系我们 | 源码上传
联系电话(Tel):4008-010-151(免长途) 企业QQ:4000410510
地址:北京市海淀区中关村鼎好大厦A座二层 邮编:100080
Room A-801,Dinghao Building,Zhongguancun,Beijing,China,100080
51Aspx.com 版权所有 CopyRight © 2006-2015. 京ICP备09089570号 | 京公网安备11010702000869号
在线客服
分享该页面
关闭侧边栏