欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net邮箱发布邮箱信息的实例

程序员文章站 2024-03-03 16:44:46
复制代码 代码如下:#region 发送邮箱方法        /// ...

复制代码 代码如下:

#region 发送邮箱方法
        /// <summary>
        /// 发送邮箱方法
        /// </summary>
        /// <param name="useremail">发送到的邮箱地址</param>

    /// <param name="username">姓名</param>
        /// <returns>发送成功 返回 true 否则返回 false</returns>
        public bool getemail(string useremail, string username)
        {
            datetime dt = datetime.now;
            string gettime = dt.tolongdatestring().tostring();
            string _mailaddress = "邮箱地址";
            string _mailnickname = "xxx";
            string _mailpassword = "邮箱密码";
            string _mailsmtphost = "smtp.163.com";
            string _mailsmtpport = "25";
            string _to = useremail;
            string _title = "xxxxxx";
            string _body = "亲爱的“" + username + "”用户:<br><br>您好!";
            string strxmlfile = httpcontext.current.server.mappath("/config/mail.config");
            xmlcontrol xmltool = new xmlcontrol(strxmlfile);
            xmltool.update("root/address", _mailaddress);
            xmltool.update("root/nickname", _mailnickname);
            xmltool.update("root/password", _mailpassword);
            xmltool.update("root/smtphost", _mailsmtphost);
            xmltool.update("root/smtpport", _mailsmtpport);
            xmltool.update("root/toaddress", useremail);
            xmltool.update("root/userinfo", username);
            xmltool.save();
            xmltool.dispose();
            if (gmailhelp.gmailsendmail(username, _to, _body, _title, _mailaddress, _mailnickname, _mailpassword, _mailsmtphost, int.parse(_mailsmtpport)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region   获取主机名称
        /// <summary>
        /// 获取主机名称    返回如  www.myweb.com  or   www.myweb.com:8080  注意没有 http://
        /// </summary>
        /// <returns></returns>
        public static string gethttphost()
        {
            int port = httpcontext.current.request.url.port;
            if (port == 80)
            {
                return httpcontext.current.request.url.host;
            }
            else
            {
                return httpcontext.current.request.url.host + ":" + port;
            }

        }
        #endregion

/// <summary>
        /// 调用 gmailsendmail("收件姓名","收件人","邮件内容","邮件标题","发件人","发件人姓名","密码","smtp主机","端口")isbodyhtml 表示所使用的邮件是html格式的, 还是text  文本格式的 enablessl 是否启用 ssl 连接, gmail 是需要的, 163 就不需要了...
        /// </summary>
        /// <param name="mailto"></param>
        /// <param name="strbody"></param>
        /// <param name="strsubjec"></param>
        /// <param name="mailfrom"></param>
        /// <param name="mailfromname"></param>
        /// <param name="mypwd"></param>
        /// <param name="smtphost"></param>
        /// <param name="smtpport"></param>
        /// <returns></returns>
        public static bool gmailsendmail(string userinfo, string mailto, string strbody, string strsubjec, string mailfrom, string mailfromname, string mypwd, string smtphost, int smtpport)
        {

            bool flag = true;
            string[] _mail = mailto.split(',');
            system.net.mail.mailmessage onemail = new system.net.mail.mailmessage(mailfrom, mailto, strsubjec, strbody);
            onemail.bodyencoding = system.text.encoding.utf8;
            onemail.isbodyhtml = true;
            //onemail.from = new system.net.mail.mailaddress(mailfrom);
            onemail.from = new mailaddress(mailfrom, "xxx", system.text.encoding.utf8);
            onemail.to.add(new system.net.mail.mailaddress(mailto));
            onemail.subject = strsubjec;
            onemail.body = strbody;
            system.net.mail.smtpclient clint = new system.net.mail.smtpclient(smtphost, smtpport);//发送邮件的服务器
            clint.credentials = new system.net.networkcredential(mailfrom, mypwd);
            clint.enablessl = true;//gmail 必须的
            clint.timeout = 10000;//必须的
            try
            {
                clint.send(onemail);//发送
                savesuclog(userinfo, mailto, mailfrom, mailfromname, smtphost);//保存正确日志
                flag = true;
            }
            catch (exception ex)
            {
                saveerrlog(userinfo, mailto, mailfrom, mailfromname, smtphost, ex.message);//保存错误日志
                flag = false;
            }
            return flag;
        }
        /// <summary>
        /// 保存正确日志
        /// </summary>
        /// <param name="mailfrom"></param>
        /// <param name="mailfromname"></param>
        /// <param name="mailsmtphost"></param>
        private static void savesuclog(string userinfo, string mailto, string mailfrom, string mailfromname, string mailsmtphost)
        {
            system.io.streamwriter sw = new system.io.streamwriter(httpcontext.current.server.mappath("/log/mailsuccess_" + datetime.now.tostring("yyyymmdd") + ".txt"), true, system.text.encoding.utf8);
            sw.writeline(system.datetime.now.tostring());
            sw.writeline("\t收信姓名:" + userinfo);
            sw.writeline("\t收 信 人:" + mailto);
            sw.writeline("\tsmtp服务器:" + mailsmtphost);
            sw.writeline("\t发 信 人:" + mailfromname + "<" + mailfrom + ">");
            sw.writeline("---------------------------------------------------------------------------------------------------");
            sw.close();
            sw.dispose();
        }
        /// <summary>
        /// 保存错误日志
        /// </summary>
        /// <param name="mailfrom"></param>
        /// <param name="mailfromname"></param>
        /// <param name="mailsmtphost"></param>
        /// <param name="errmsg"></param>
        private static void saveerrlog(string userinfo, string mailto, string mailfrom, string mailfromname, string mailsmtphost, string errmsg)
        {
            system.io.streamwriter sw = new system.io.streamwriter(httpcontext.current.server.mappath("/log/mailerror_" + datetime.now.tostring("yyyymmdd") + ".txt"), true, system.text.encoding.utf8);
            sw.writeline(system.datetime.now.tostring());
            sw.writeline("\t收信姓名:" + userinfo);
            sw.writeline("\t收 信 人:" + mailto);
            sw.writeline("\tsmtp服务器:" + mailsmtphost);
            sw.writeline("\t发 信 人:" + mailfromname + "<" + mailfrom + ">");
            sw.writeline("\t错误信息:\r\n" + errmsg);
            sw.writeline("---------------------------------------------------------------------------------------------------");
            sw.close();
            sw.dispose();
        }