支持HTML格式和优先级设置的邮件发送类

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace Blood.Com.ClassLib
{         
    /// <summary>
    /// TcpClient派生类,用来进行SMTP服务器的连接工作
    /// </summary>
    public class SMTPClient : TcpClient
    {
        public bool isConnected()
        {
            return Active;
        }

        public void SendCommandToServer(string Command)
        {
            NetworkStream ns = this.GetStream() ;
            byte[]  WriteBuffer ;
            WriteBuffer = new byte[1024] ;
            WriteBuffer = Encoding.Default.GetBytes(Command) ;
            ns.Write(WriteBuffer,0,WriteBuffer.Length);
            return  ;
        }         
        public string GetServerResponse()
        {
            int StreamSize ;
            string ReturnValue = "" ;
            byte[]  ReadBuffer ;
            NetworkStream ns = this.GetStream() ;
            ReadBuffer = new byte[1024] ;
            StreamSize = ns.Read(ReadBuffer,0,ReadBuffer.Length);
            if (StreamSize==0)
            {
                return ReturnValue ;
            }
            else
            {
                ReturnValue = Encoding.Default.GetString(ReadBuffer);
                return  ReturnValue;
            }         
        }

        public bool DoesStringContainSMTPCode(string s,string SMTPCode)
        {
            return(s.IndexOf(SMTPCode,0,10)==-1)?false:true;
        }

    } //结束类

    /// <summary>
    /// 发送邮件类
    /// </summary>
    public class SMTPMail
    {
        /// <summary>
        /// 错误反馈信息
        /// </summary>
        private string strErrMessage = null;

        /// <summary>
        /// SMTP服务器反馈的信息
        /// </summary>
        private string strResponse;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SMTPMail()
        {
            strErrMessage = "";
            strResponse = "";
        }

        /// <summary>
        /// 取得错误反馈信息
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                return strErrMessage ;
            }
        }

        /// <summary>
        /// 取得SMTP服务器反馈的信息
        /// </summary>
        public string ServerResponse
        {
            get
            {
                return strResponse;
            }
        }

        /// <summary>
        /// 邮件发送优先级
        /// </summary>
        public enum Prioritys
        {
            /// <summary>
            /// 最高级别
            /// </summary>
            HIGH = 1,
            /// <summary>
            /// 默认级别
            /// </summary>
            NORMAL = 3,
            /// <summary>
            /// 最低级别
            /// </summary>
            LOW = 5
        }

        public void SendMail(string SmtpHost,int Port,string From,string DisplayFromName,string To,string DisplayToName,Prioritys Priority,bool Html,string Base,string Subject,string Message)
        {
            try
            {
                string strResponseNumber;
                SMTPClient smtpcMail = new SMTPClient();
                smtpcMail.Connect(SmtpHost,Port);
                bool bolConnect = smtpcMail.isConnected();
                
                //判断是否进行了连接
                if (!bolConnect)
                {
                    strErrMessage = "Smtp服务器连接失败...";
                    return;
                }
                
                //读取反馈信息
                strResponseNumber = smtpcMail.GetServerResponse();
                if (smtpcMail.DoesStringContainSMTPCode(strResponseNumber,"220"))
                {
                    this.strResponse += strResponseNumber;
                }
                else
                {
                    this.strErrMessage = "连接失败" + strResponseNumber;
                    return ;
                }
                
                string[] strSendBuffer = new string[6];
                string[] strResponseCode = {"220","250","251","354","221"};   // success codes from SMTP server
                
                string strData = "";
                strData = string.Concat("HELO ",SmtpHost);
                strData = string.Concat(strData,"\r\n");
                strSendBuffer[0] = strData  ;
                
                strData = "";
                strData = string.Concat("MAIL FROM: ","<" + From + ">");
                strData = string.Concat(strData,"\r\n");
                strSendBuffer[1] = strData;
                
                strData = "";
                strData = string.Concat("RCPT TO: ","<" + To + ">");
                strData = string.Concat(strData,"\r\n");
                strSendBuffer[2] = strData;
                
                strData = "" ;
                strData = string.Concat("DATA","\r\n");
                strSendBuffer[3] = strData  ;
                
                strData = "" ;
                strData = string.Concat("From: ",DisplayFromName + "<" + From + ">");
                strData = string.Concat(strData,"\r\n" );
                strData = string.Concat(strData,"To: " );
                strData = string.Concat(strData,DisplayToName + "<" + To + ">");
                strData = string.Concat(strData,"\r\n" );
                strData = string.Concat(strData,"Subject: " );
                strData = string.Concat(strData,Subject);
                strData = string.Concat(strData,"\r\n");
                strData = string.Concat(strData,"MIME-Version: 1.0" );
                strData = string.Concat(strData,"\r\n");
                strData = string.Concat(strData,"X-Priority: " + Priority);
                strData = string.Concat(strData,"\r\n");
                strData = string.Concat(strData,"X-MSMail-Priority: " + Priority);
                strData = string.Concat(strData,"\r\n");
                if(Html == true)
                {
                    strData = string.Concat(strData,"Content-Type: text/html;" );
                }
                else
                {
                    strData = string.Concat(strData,"Content-Type: text/plain;" );
                }
                strData = string.Concat(strData,"\r\n");
                strData = string.Concat(strData,"charset=\"iso-8859-1\"" );
                strData = string.Concat(strData,"\r\n");
                if(Html == true)
                {
                    strData = string.Concat(strData,"Content-Transfer-Encoding: text/html;" );
                }
                else
                {
                    strData = string.Concat(strData,"Content-Transfer-Encoding: text/plain;" );
                }
                strData = string.Concat(strData,"\r\n");
                strData = string.Concat(strData,"Content-Base: \"" + Base + "\"" );
                strData = string.Concat(strData,"\r\n" + "\r\n");
                
                strData = string.Concat(strData,Message);
                strData = string.Concat(strData,"\r\n.\r\n");
                strSendBuffer[4] = strData;
                
                strData = "" ;
                strData = string.Concat(strData,"QUIT\r\n");
                strSendBuffer[5] = strData;
                
                int i = 0 ;
                
                while(i < strSendBuffer.Length)
                {
                    smtpcMail.SendCommandToServer(strSendBuffer[i]);
                    strResponseNumber = smtpcMail.GetServerResponse();
                    
                    for(int j=0;j<strResponseCode.Length;j++)
                    {
                        if (smtpcMail.DoesStringContainSMTPCode(strResponseNumber,strResponseCode[j]))
                        {
                            this.strResponse += strResponseNumber;
                            this.strResponse += "<br>";
                            break;
                        }
                        else
                        {
                            if(j==strResponseCode.Length-1)
                            {
                                this.strErrMessage += strResponseNumber;
                                this.strErrMessage += strSendBuffer[i];
                                return;
                            }
                        }
                    }
                    
                    i++ ;
                } // 结束循环
            }
            catch(SocketException err)
            {
                this.strErrMessage += err.Message + " " + err.StackTrace;
            }
            catch(Exception e)
            {
                this.strErrMessage += e.Message + " " + e.StackTrace;
            }
        } //结束邮件发送方法
         
    } // 结束类

} // 结束Namespace

时间: 2024-11-03 03:06:08

支持HTML格式和优先级设置的邮件发送类的相关文章

PHP实现支持SSL连接的SMTP邮件发送类

 这篇文章主要介绍了PHP实现支持SSL连接的SMTP邮件发送类,实例分析了php实现smtp邮件发送类的原理与技巧,以及支持SSL连接的方法,需要的朋友可以参考下     本文实例讲述了PHP实现支持SSL连接的SMTP邮件发送类.分享给大家供大家参考.具体如下: 该实例代码测试过了gmail和QQ邮箱的SMTP.具体代码如下: 代码如下: <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件(单个或多个附件),支持到服务器的

PHP实现支持SSL连接的SMTP邮件发送类_php技巧

本文实例讲述了PHP实现支持SSL连接的SMTP邮件发送类.分享给大家供大家参考.具体如下: 该实例代码测试过了gmail和QQ邮箱的SMTP.具体代码如下: 复制代码 代码如下: <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件(单个或多个附件),支持到服务器的ssl连接 * 需要的php扩展:sockets.Fileinfo和openssl. * 编码格式是UTF-8,传输编码格式是base64 * @example *

PHP邮件发送类

Swift Mailer 和前面介绍的PHPMailer一样,也是一个PHP邮件发送类.它不依赖于 PHP 自带的mail() 函数,因为该函数在发送多个邮件时占用的系统资源很高.Swift 直接与 SMTP 服务器通讯,具有非常高的发送速度和效率. phpmailer邮件找回密码 1.根据邮件找回链接进入重置密码操作页面 .仅一周内.一次有效 2.重置密码 (也可以将密码发送直接登录) 生成邮件找回邮件 用linux下安装的sendmail写了一个发送email的程序 Java代码   <?p

用c#写的smtp邮件发送类

smtp //**********************Created by Chen**************************using System;using System.IO;using System.Text;using System.Net;using System.Net.Sockets;using System.Collections;using System.Collections.Specialized;using KSN.Exceptions;using KS

ThinkPHP邮件发送类

最近在做一个项目,需要有邮件发送的功能,服务器邮件发送的话,服务器上必须有能链接邮件服务器,才能实现以下的步骤,现在就给大家分享一下,专门做了一个邮件的发送类 /** * 系统邮件发送函数 * @param string $to 接收邮件者邮箱 * @param string $name 接收邮件者名称 * @param string $subject 邮件主题 * @param string $body 邮件内容 * @param string $attachment 附件列表 * @retu

PHP在线邮件发送类,通过SMTP来发送

<?  /*  * 邮件发送类 * 作    者:多菜鸟 * 联系邮箱:kingerq AT msn DOT com * 创建时间:2005-08-19 * 注明:此类为改装过来的,忘记出处了 */class smail { //您的SMTP 服务器供应商,可以是域名或IP地址 var $smtp = "smtp.163.com";  //SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系. var

PHP邮件发送类PHPMailer用法实例详解

 本文实例讲述了PHP邮件发送类PHPMailer用法,并详细讲述了其具体的操作步骤.分享给大家供大家参考.具体步骤如下: 1.在服务器安装 sendmail 1 sudo apt-get install sendmail 2.启动 sendmail 1 sudo /etc/init.d/sendmail start 3.修改 php.ini 1 2 3 4 [mail function]  SMTP = localhost  smtp_port = 25  sendmail_from = me

PHP实现发送邮件的方法(基于简单邮件发送类)_php技巧

本文实例讲述了PHP实现发送邮件的方法.分享给大家供大家参考,具体如下: 邮件发送类 <?php /*邮件发送类 *功能:使用smtp服务器发送邮件 */ class smtp { /* 全局变量 */ var $smtp_port; var $time_out; var $host_name; var $log_file; var $relay_host; var $debug; var $auth; var $user; var $pass; var $sock; /* 构造函数 */ fu

PHP邮件发送类PHPMailer用法实例详解_php技巧

本文实例讲述了PHP邮件发送类PHPMailer用法,并详细讲述了其具体的操作步骤.分享给大家供大家参考.具体步骤如下: 1.在服务器安装 sendmail sudo apt-get install sendmail 2.启动 sendmail sudo /etc/init.d/sendmail start 3.修改 php.ini [mail function] SMTP = localhost smtp_port = 25 sendmail_from = me@example.com 4.F