• 发送邮件类


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

    namespace Edu.Stu.Common.Mail
    {
        ///<summary>
        /// Module ID:0005
        /// Depiction:发送邮件,支持ESMTP, 多附件
        /// Wreter:Nick
        /// Create Date;2004-12-20
        ///</summary>
        public class MailSender
        {

            #region "Fields"
            private string m_strFromName = "";
            private string[] m_strTo = null;
            private string m_strFrom = "";
            private string m_strPassWord = "";
            private string m_strUserName = "";
            private int m_iPort = 25;
            private string m_strServer = "";
            private int m_iPriority = 3;
            private string m_strEncoding = "8bit";
            private string m_strLanguageEncoding = "GB2312";
            private bool m_bIsHtml = true;
            private string m_strHtmlBody = "";
            private string m_strTxtBody = "";
            private string m_strSubject = "";
            private string m_strToName = "";
            private ArrayList m_alAttachments = new ArrayList();
            #endregion

            #region "Propertes"
            /// <summary>
            /// SMTP服务器域名
            /// </summary>
            public string Server
            {
                get { return m_strServer; }
                set { if (value != m_strServer) m_strServer = value; }
            }

            /// <summary>
            /// SMTP服务器端口 [默认为25]
            /// </summary>
            public int Port
            {
                get { return m_iPort; }
                set { if (value != m_iPort) m_iPort = value; }
            }

            /// <summary>
            /// 用户名 [如果需要身份验证的话]
            /// </summary>
            public string UserName
            {
                get { return m_strUserName; }
                set { if (value != m_strUserName) m_strUserName = value; }
            }

            /// <summary>
            /// 密码 [如果需要身份验证的话]
            /// </summary>
            public string Password
            {
                get { return m_strPassWord; }
                set { if (value != m_strPassWord) m_strPassWord = value; }
            }

            /// <summary>
            /// 发件人地址
            /// </summary>
            public string From
            {
                get { return m_strFrom; }
                set { if (value != m_strFrom) m_strFrom = value; }
            }

            /// <summary>
            /// 收件人地址
            /// </summary>
            public string[] To
            {
                get { return m_strTo; }
                set { if (value != m_strTo) m_strTo = value; }
            }

            /// <summary>
            /// 发件人姓名
            /// </summary>
            public string FromName
            {
                get { return m_strFromName; }
                set { if (value != m_strFromName) m_strFromName = value; }
            }

            /// <summary>
            /// 收件人姓名
            /// </summary>
            public string ToName
            {
                get { return m_strToName; }
                set { if (value != m_strToName) m_strToName = value; }
            }


            /// <summary>
            /// 邮件的主题
            /// </summary>
            public string Subject
            {
                get { return m_strSubject; }
                set { if (value != m_strSubject) m_strSubject = value; }
            }

            /// <summary>
            /// 邮件正文
            /// </summary>
            public string Body
            {
                get { return m_strTxtBody; }
                set { if (value != m_strTxtBody) m_strTxtBody = value; }
            }

            /// <summary>
            /// 超文本格式的邮件正文
            /// </summary>
            public string HtmlBody
            {
                get { return m_strHtmlBody; }
                set { if (value != m_strHtmlBody) m_strHtmlBody = value; }
            }

            /// <summary>
            /// 是否是html格式的邮件
            /// </summary>
            public bool IsHtml
            {
                get { return m_bIsHtml; }
                set { if (value != m_bIsHtml) m_bIsHtml = value; }
            }

            /// <summary>
            /// 语言编码 [默认为GB2312]
            /// </summary>
            public string LanguageEncoding
            {
                get { return m_strLanguageEncoding; }
                set { if (value != m_strLanguageEncoding) m_strLanguageEncoding = value; }
            }

            /// <summary>
            /// 邮件编码 [默认为8bit]
            /// </summary>
            public string MailEncoding
            {
                get { return m_strEncoding; }
                set { if (value != m_strEncoding) m_strEncoding = value; }
            }

            /// <summary>
            /// 邮件优先级 [默认为3]
            /// </summary>
            public int Priority
            {
                get { return m_iPriority; }
                set { if (value != m_iPriority) m_iPriority = value; }
            }

            /// <summary>
            /// 附件 [AttachmentInfo]
            /// </summary>
            public IList Attachments
            {
                get { return m_alAttachments; }
                // set { if (value != m_alAttachments) m_alAttachments = value; }
            }

            #endregion

            #region "Struct AttachmentInfo"
            /// <summary>
            /// 附件信息
            /// </summary>
            public struct AttachmentInfo
            {
                /// <summary>
                /// 附件的文件名 [如果输入路径,则自动转换为文件名]
                /// </summary>
                public string FileName
                {
                    get { return fileName; }
                    set { fileName = Path.GetFileName(value); }
                } private string fileName;

                /// <summary>
                /// 附件的内容 [由经Base64编码的字节组成]
                /// </summary>
                public string Bytes
                {
                    get { return bytes; }
                    set { if (value != bytes) bytes = value; }
                } private string bytes;

                /// <summary>
                /// 从流中读取附件内容并构造
                /// </summary>
                /// <param name="p_strFileName">附件的文件名</param>
                /// <param name="p_stream">流</param>
                public AttachmentInfo(string p_strFileName, Stream p_stream)
                {
                    fileName = Path.GetFileName(p_strFileName);
                    byte[] by = new byte[p_stream.Length];
                    p_stream.Read(by, 0, (int)p_stream.Length); // 读取文件内容
                    //格式转换
                    bytes = Convert.ToBase64String(by); // 转化为base64编码
                }

                /// <summary>
                /// 按照给定的字节构造附件
                /// </summary>
                /// <param name="p_strFileName">附件的文件名</param>
                /// <param name="ibytes">附件的内容 [字节]</param>
                public AttachmentInfo(string p_strFileName, byte[] ibytes)
                {
                    fileName = Path.GetFileName(p_strFileName);
                    bytes = Convert.ToBase64String(ibytes); // 转化为base64编码
                }

                /// <summary>
                /// 从文件载入并构造
                /// </summary>
                /// <param name="p_strPath"></param>
                public AttachmentInfo(string p_strPath)
                {
                    fileName = Path.GetFileName(p_strPath);
                    FileStream file = new FileStream(p_strPath, FileMode.Open);
                    byte[] by = new byte[file.Length];
                    file.Read(by, 0, (int)file.Length); // 读取文件内容
                    //格式转换
                    bytes = Convert.ToBase64String(by); // 转化为base64编码
                    file.Close();
                }
            }
            #endregion

            #region "Functions"
            /// <summary>
            /// 发送邮件
            /// </summary>
            private void SendMail()
            {
                // 创建TcpClient对象, 并建立连接
                TcpClient tcpNewClient = null;
                try
                {
                    tcpNewClient = new TcpClient(m_strServer, m_iPort);
                }
                catch (Exception)
                {
                    throw new Exception("无法连接服务器");
                }

                ReadString(tcpNewClient.GetStream());//获取连接信息

                // 开始进行服务器认证
                // 如果状态码是250则表示操作成功
                if (!Command(tcpNewClient.GetStream(), "EHLO Localhost", "250"))
                {
                    tcpNewClient.Close();
                    throw new Exception("登陆阶段失败");
                }

                if (m_strUserName != "")
                {
                    // 需要身份验证
                    if (!Command(tcpNewClient.GetStream(), "AUTH LOGIN", "334"))
                    {
                        tcpNewClient.Close();
                        throw new Exception("身份验证阶段失败");
                    }

                    // 此处将username转换为Base64码
                    string nameB64 = ToBase64(m_strUserName);
                    if (!Command(tcpNewClient.GetStream(), nameB64, "334"))
                    {
                        tcpNewClient.Close();
                        throw new Exception("身份验证阶段失败");
                    }

                    // 此处将m_strPassWord转换为Base64码
                    string passB64 = ToBase64(m_strPassWord);
                    if (!Command(tcpNewClient.GetStream(), passB64, "235"))
                    {
                        tcpNewClient.Close();
                        throw new Exception("身份验证阶段失败");
                    }
                }


                // 准备发送
                WriteString(tcpNewClient.GetStream(), "mail From: " + m_strFrom);
                for (int i = 0; i < m_strTo.Length; i++)
                {
                    WriteString(tcpNewClient.GetStream(), "rcpt m_strTo: " + m_strTo[i]);
                }
                WriteString(tcpNewClient.GetStream(), "data");

                // 发送邮件头
                WriteString(tcpNewClient.GetStream(), "Date: " + DateTime.Now); // 时间
                WriteString(tcpNewClient.GetStream(), "From: " + m_strFromName + "<" + m_strFrom + ">"); // 发件人
                WriteString(tcpNewClient.GetStream(), "Subject: " + m_strSubject); // 主题
                WriteString(tcpNewClient.GetStream(), "To:" + m_strToName + "<" + m_strTo + ">"); // 收件人

                //邮件格式
                WriteString(tcpNewClient.GetStream(), "Content-Type: multipart/mixed; boundary=\"unique-boundary-1\"");
                WriteString(tcpNewClient.GetStream(), "Reply-To:" + m_strFrom); // 回复地址
                WriteString(tcpNewClient.GetStream(), "X-Priority:" + m_iPriority); // 优先级
                WriteString(tcpNewClient.GetStream(), "MIME-Version:1.0"); // MIME版本

                // 数据ID,随意
                // WriteString (tcpNewClient.GetStream(), "Message-Id: " + DateTime.Now.ToFileTime() + "@security.com");
                WriteString(tcpNewClient.GetStream(), "Content-Transfer-Encoding:" + m_strEncoding); // 内容编码
                WriteString(tcpNewClient.GetStream(), "X-Mailer:JcPersonal.Utility.MailSender"); // 邮件发送者
                WriteString(tcpNewClient.GetStream(), "");

                WriteString(tcpNewClient.GetStream(), ToBase64("This is a multi-part message in MIME format."));
                WriteString(tcpNewClient.GetStream(), "");

                // 从此处开始进行分隔输入
                WriteString(tcpNewClient.GetStream(), "--unique-boundary-1");

                // 在此处定义第二个分隔符
                WriteString(tcpNewClient.GetStream(), "Content-Type: multipart/alternative;Boundary=\"unique-boundary-2\"");
                WriteString(tcpNewClient.GetStream(), "");

                if (!m_bIsHtml)
                {
                    // 文本信息
                    WriteString(tcpNewClient.GetStream(), "--unique-boundary-2");
                    WriteString(tcpNewClient.GetStream(), "Content-Type: text/plain;charset=" + m_strLanguageEncoding);
                    WriteString(tcpNewClient.GetStream(), "Content-Transfer-Encoding:" + m_strEncoding);
                    WriteString(tcpNewClient.GetStream(), "");
                    WriteString(tcpNewClient.GetStream(), m_strTxtBody);
                    WriteString(tcpNewClient.GetStream(), "");//一个部分写完之后就写如空信息,分段
                    WriteString(tcpNewClient.GetStream(), "--unique-boundary-2--");//分隔符的结束符号,尾巴后面多了--
                    WriteString(tcpNewClient.GetStream(), "");
                }
                else
                {
                    //html信息
                    WriteString(tcpNewClient.GetStream(), "Content-Type: multipart/mixed");
                    WriteString(tcpNewClient.GetStream(), "--unique-boundary-2");
                    WriteString(tcpNewClient.GetStream(), "Content-Type: text/html;charset=" + m_strLanguageEncoding);
                    WriteString(tcpNewClient.GetStream(), "Content-Transfer-Encoding:" + m_strEncoding);
                    WriteString(tcpNewClient.GetStream(), "");
                    WriteString(tcpNewClient.GetStream(), m_strHtmlBody);
                    WriteString(tcpNewClient.GetStream(), "");
                    WriteString(tcpNewClient.GetStream(), "--unique-boundary-2--");//分隔符的结束符号,尾巴后面多了--
                    WriteString(tcpNewClient.GetStream(), "");
                }

                // 发送附件
                // 对文件列表做循环
                for (int i = 0; i < m_alAttachments.Count; i++)
                {
                    WriteString(tcpNewClient.GetStream(), "--unique-boundary-1"); // 邮件内容分隔符
                    WriteString(tcpNewClient.GetStream(), "Content-Type: application/octet-stream;name=\"" + ((AttachmentInfo)m_alAttachments[i]).FileName + "\""); // 文件格式
                    WriteString(tcpNewClient.GetStream(), "Content-Transfer-Encoding: base64"); // 内容的编码
                    WriteString(tcpNewClient.GetStream(), "Content-Disposition:attachment;filename=\"" + ((AttachmentInfo)m_alAttachments[i]).FileName + "\""); // 文件名
                    WriteString(tcpNewClient.GetStream(), "");
                    WriteString(tcpNewClient.GetStream(), ((AttachmentInfo)m_alAttachments[i]).Bytes); // 写入文件的内容
                    WriteString(tcpNewClient.GetStream(), "");
                }

                Command(tcpNewClient.GetStream(), ".", "250"); // 最后写完了,输入"."

                // 关闭连接
                tcpNewClient.Close();
            }
            /// <summary>
            /// 向流中写入字符
            /// </summary>
            /// <param name="p_netStream">来自TcpClient的流</param>
            /// <param name="p_str">写入的字符</param>
            protected void WriteString(NetworkStream p_netStream, string p_str)
            {
                p_str = p_str + "\r\n"; // 加入换行符

                // 将命令行转化为byte[]
                byte[] bWrite = Encoding.GetEncoding(m_strLanguageEncoding).GetBytes(p_str.ToCharArray());

                // 由于每次写入的数据大小是有限制的,那么我们将每次写入的数据长度定在75个字节,一旦命令长度超过了75,就分步写入。
                int iStart = 0;
                int iLength = bWrite.Length;
                int iPage = 0;
                int iSize = 75;
                int iCount = iSize;
                try
                {
                    if (iLength > 75)
                    {
                        // 数据分页
                        if ((iLength / iSize) * iSize < iLength)
                            iPage = iLength / iSize + 1;
                        else
                            iPage = iLength / iSize;
                        for (int i = 0; i < iPage; i++)
                        {
                            iStart = i * iSize;
                            if (i == iPage - 1)
                                iCount = iLength - (i * iSize);
                            p_netStream.Write(bWrite, iStart, iCount);// 将数据写入到服务器上
                        }
                    }
                    else
                        p_netStream.Write(bWrite, 0, bWrite.Length);
                }
                catch (Exception)
                {
                    // 忽略错误
                }
            }

            /// <summary>
            /// 从流中读取字符
            /// </summary>
            /// <param name="p_netStream">来自TcpClient的流</param>
            /// <returns>读取的字符</returns>
            protected string ReadString(NetworkStream p_netStream)
            {
                string strSp = null;
                byte[] by = new byte[1024];
                int iSize = p_netStream.Read(by, 0, by.Length);// 读取数据流
                if (iSize > 0)
                {
                    strSp = Encoding.Default.GetString(by);// 转化为String
                }
                return strSp;
            }

            /// <summary>
            /// 发出命令并判断返回信息是否正确
            /// </summary>
            /// <param name="p_netStream">来自TcpClient的流</param>
            /// <param name="p_strCommand">命令</param>
            /// <param name="p_strState">正确的状态码</param>
            /// <returns>是否正确</returns>
            protected bool Command(NetworkStream p_netStream, string p_strCommand, string p_strState)
            {
                string strSp = null;
                bool bSuccess = false;
                try
                {
                    // 写入命令
                    WriteString(p_netStream, p_strCommand);
                    strSp = ReadString(p_netStream);// 接受返回信息
                    if (strSp.IndexOf(p_strState) != -1)// 判断状态码是否正确
                        bSuccess = true;
                }
                catch (Exception)
                {
                    // 忽略错误
                }
                return bSuccess;
            }

            /// <summary>
            /// 字符串编码为Base64
            /// </summary>
            /// <param name="p_str">字符串</param>
            /// <returns>Base64编码的字符串</returns>
            protected string ToBase64(string p_str)
            {
                try
                {
                    byte[] by = Encoding.Default.GetBytes(p_str.ToCharArray());
                    p_str = Convert.ToBase64String(by);
                }
                catch (Exception)
                {
                    // 忽略错误
                }
                return p_str;
            }
            /// <summary>
            /// 表示一封待发送的邮件
            /// </summary>
            public MailSender()
            {
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }
            #endregion

            #region "Tools"
            /// <summary>
            /// 回复格式化html的格式到htmlt格式
            /// </summary>
            /// <param name="p_strOldStr">要格式化的字符串</param>
            /// <param name="p_strSeparator_n"></param>
            /// <param name="p_strSeparator_r"></param>
            /// <returns></returns>
            public string reformatSpecailHtml_toHtml(string p_strOldStr, string p_strSeparator_n, string p_strSeparator_r)
            {
                //string result;
                while (p_strOldStr.IndexOf("&lt;") > -1 || p_strOldStr.IndexOf("&gt") > -1 || p_strOldStr.IndexOf("@$@_n") > -1 || p_strOldStr.IndexOf("@$@_r") > -1)
                {
                    if (p_strOldStr.IndexOf("&lt;") > -1)
                    {
                        p_strOldStr = p_strOldStr.Substring(0, p_strOldStr.IndexOf("&lt;")) + "<" + p_strOldStr.Substring(p_strOldStr.IndexOf("&lt;") + 4);
                    }
                    else if (p_strOldStr.IndexOf("&gt") > -1)
                    {
                        p_strOldStr = p_strOldStr.Substring(0, p_strOldStr.IndexOf("&gt")) + ">" + p_strOldStr.Substring(p_strOldStr.IndexOf("&gt") + 4);
                    }
                    else if (p_strOldStr.IndexOf(p_strSeparator_n) > -1)
                    {
                        p_strOldStr = p_strOldStr.Substring(0, p_strOldStr.IndexOf(p_strSeparator_n)) + "<br>" + p_strOldStr.Substring(p_strOldStr.IndexOf(p_strSeparator_n) + p_strSeparator_n.Length);
                    }
                    else if (p_strOldStr.IndexOf(p_strSeparator_r) > -1)
                    {
                        p_strOldStr = p_strOldStr.Substring(0, p_strOldStr.IndexOf(p_strSeparator_r)) + "<br>" + p_strOldStr.Substring(p_strOldStr.IndexOf(p_strSeparator_r) + p_strSeparator_r.Length);
                    }
                    else
                    {
                        p_strOldStr = p_strOldStr;
                    }
                }
                return p_strOldStr;
            }
            /// <summary>
            /// 格式化html的格式(目前不用)
            /// </summary>
            /// <param name="p_strOldStr">要格式化的字符串</param>
            /// <returns></returns>
            private string formatSpecailHtml(string p_strOldStr)
            {
                string result;
                if (p_strOldStr.IndexOf("<") > -1)
                {
                    result = p_strOldStr.Substring(0, p_strOldStr.IndexOf("<")) + "&lt;" + p_strOldStr.Substring(p_strOldStr.IndexOf("<") + 1);
                    formatSpecailHtml(result);
                }
                else if (p_strOldStr.IndexOf(">") > -1)
                {
                    result = p_strOldStr.Substring(0, p_strOldStr.IndexOf(">")) + "&gt;" + p_strOldStr.Substring(p_strOldStr.IndexOf(">") + 1);
                    formatSpecailHtml(result);
                }
                else
                {
                    result = p_strOldStr;
                }
                return result;
            }
            /// <summary>
            /// 回复格式化html的格式到txt格式
            /// </summary>
            /// <param name="p_strOldStr">要格式化的字符串</param>
            /// <param name="p_strSeparator_n"></param>
            /// <param name="p_strSeparator_r"></param>
            /// <returns></returns>
            public string reformatSpecailHtml_toTxt(string p_strOldStr, string p_strSeparator_n, string p_strSeparator_r)
            {
                //string result;
                while (p_strOldStr.IndexOf("&lt;") > -1 || p_strOldStr.IndexOf("&gt") > -1 || p_strOldStr.IndexOf(p_strSeparator_n) > -1 || p_strOldStr.IndexOf(p_strSeparator_r) > -1)
                {
                    if (p_strOldStr.IndexOf("&lt;") > -1)
                    {
                        p_strOldStr = p_strOldStr.Substring(0, p_strOldStr.IndexOf("&lt;")) + "<" + p_strOldStr.Substring(p_strOldStr.IndexOf("&lt;") + 4);
                    }
                    else if (p_strOldStr.IndexOf("&gt") > -1)
                    {
                        p_strOldStr = p_strOldStr.Substring(0, p_strOldStr.IndexOf("&gt")) + ">" + p_strOldStr.Substring(p_strOldStr.IndexOf("&gt") + 4);
                    }
                    else if (p_strOldStr.IndexOf(p_strSeparator_n) > -1)
                    {
                        p_strOldStr = p_strOldStr.Substring(0, p_strOldStr.IndexOf(p_strSeparator_n)) + "\n" + p_strOldStr.Substring(p_strOldStr.IndexOf(p_strSeparator_n) + p_strSeparator_n.Length);
                    }
                    else if (p_strOldStr.IndexOf(p_strSeparator_r) > -1)
                    {
                        p_strOldStr = p_strOldStr.Substring(0, p_strOldStr.IndexOf(p_strSeparator_r)) + "\r" + p_strOldStr.Substring(p_strOldStr.IndexOf(p_strSeparator_r) + p_strSeparator_r.Length);
                    }
                    else
                    {
                        p_strOldStr = p_strOldStr;
                    }
                }
                return p_strOldStr;
            }
            #endregion

            #region "Use"
            /// <summary>
            /// 发送邮件
            /// </summary>
            /// <param name="p_strMailFrom">发件人地址</param>
            /// <param name="p_strMailTo">收件人地址</param>
            /// <param name="p_strMailFormat">邮件主体格式</param>
            /// <param name="p_strMailSubject">邮件主题</param>
            /// <param name="p_strMailBody">邮件主体</param>
            /// <param name="p_strMailAttachmentPaths">附件路径</param>
            /// <param name="mailSmtp">smtp服务地址</param>
            /// <param name="smtpUser"></param>
            /// <param name="p_strSmtpPassword"></param>
            /// <param name="p_strSeparator_n"></param>
            /// <param name="p_strSeparator_r"></param>
            public void send(string p_strMailFrom, string[] p_strMailTo, string p_strMailFormat, string p_strMailSubject, string p_strMailBody, string[] p_strMailAttachmentPaths, string mailSmtp, string smtpUser, string p_strSmtpPassword, string p_strSeparator_n, string p_strSeparator_r)
            {
                //MailSender myEmail = null;
                try
                {
                    //myEmail = new MailSender();
                    this.From = p_strMailFrom;
                    this.To = p_strMailTo;
                    this.Subject = p_strMailSubject;
                    this.Body = reformatSpecailHtml_toTxt(p_strMailBody, p_strSeparator_n, p_strSeparator_r);
                    this.HtmlBody = reformatSpecailHtml_toHtml(p_strMailBody, p_strSeparator_n, p_strSeparator_r);
                    if (p_strMailFormat.ToUpper() == "HTML")
                    {
                        this.IsHtml = true;
                    }
                    else
                    {
                        this.IsHtml = false;
                    }

                    for (int i = 0; i < p_strMailAttachmentPaths.Length; i++)
                    {
                        if (p_strMailAttachmentPaths[i] != null && p_strMailAttachmentPaths[i].Length > 0)
                        {
                            object tempObj = new MailSender.AttachmentInfo(p_strMailAttachmentPaths[i]);
                            this.Attachments.Add(tempObj);
                        }
                    }

                    this.Server = mailSmtp;
                    this.UserName = smtpUser;
                    this.Password = p_strSmtpPassword;
                    //this.SendMail();
                    SendMail();
                }
                catch (Exception ex)
                {
                    throw new Exception("使用MailSender失败", ex);
                }
            }
            /// <summary>
            /// 发送邮件(使用默认分隔符号)
            /// </summary>
            /// <param name="p_strMailFrom">发件人地址</param>
            /// <param name="p_strMailTo">收件人地址</param>
            /// <param name="p_strMailFormat">邮件主体格式</param>
            /// <param name="p_strMailSubject">邮件主题</param>
            /// <param name="p_strMailBody">邮件主体</param>
            /// <param name="p_strMailAttachmentPaths">附件路径</param>
            /// <param name="mailSmtp">smtp服务地址</param>
            /// <param name="smtpUser"></param>
            /// <param name="p_strSmtpPassword"></param>
            /// Writer:Nick
            /// Create Date:2004-12-20
            public void send(string p_strMailFrom, string[] p_strMailTo, string p_strMailFormat, string p_strMailSubject, string p_strMailBody, string[] p_strMailAttachmentPaths, string mailSmtp, string smtpUser, string p_strSmtpPassword)
            {
                string strSeparator_n = "@$@_n";
                string strSeparator_r = "@$@_r";
                send(p_strMailFrom, p_strMailTo, p_strMailFormat, p_strMailSubject, p_strMailBody, p_strMailAttachmentPaths, mailSmtp, smtpUser, p_strSmtpPassword, strSeparator_n, strSeparator_r);
            }
            /// <summary>
            /// 发送邮件(无附件)
            /// </summary>
            /// <param name="p_strMailFrom">发件人地址</param>
            /// <param name="p_strMailTo">收件人地址</param>
            /// <param name="p_strMailFormat">邮件主体格式</param>
            /// <param name="p_strMailSubject">邮件主题</param>
            /// <param name="p_strMailBody">邮件主体</param>
            /// <param name="mailSmtp">smtp服务地址</param>
            /// <param name="smtpUser"></param>
            /// <param name="p_strSmtpPassword"></param>
            /// Writer:Nick
            /// Create Date:2004-12-20
            public void send(string p_strMailFrom, string[] p_strMailTo, string p_strMailFormat, string p_strMailSubject, string p_strMailBody, string mailSmtp, string smtpUser, string p_strSmtpPassword)
            {
                string[] strMailAttachmentPaths = new string[3];
                send(p_strMailFrom, p_strMailTo, p_strMailFormat, p_strMailSubject, p_strMailBody, strMailAttachmentPaths, mailSmtp, smtpUser, p_strSmtpPassword);
            }
            /// <summary>
            /// 发送邮件(无附件、txt格式)
            /// </summary>
            /// <param name="p_strMailFrom">发件人地址</param>
            /// <param name="p_strMailTo">收件人地址</param>
            /// <param name="p_strMailSubject">邮件主题</param>
            /// <param name="p_strMailBody">邮件主体</param>
            /// <param name="mailSmtp">smtp服务地址</param>
            /// <param name="smtpUser"></param>
            /// <param name="p_strSmtpPassword"></param>
            /// Writer:Nick
            /// Create Date:2004-12-20
            public void send(string p_strMailFrom, string[] p_strMailTo, string p_strMailSubject, string p_strMailBody, string mailSmtp, string smtpUser, string p_strSmtpPassword)
            {
                string strMailFormat = "txt";
                send(p_strMailFrom, p_strMailTo, strMailFormat, p_strMailSubject, p_strMailBody, mailSmtp, smtpUser, p_strSmtpPassword);
            }
            /// <summary>
            /// 发送邮件
            /// </summary>
            /// Writer:Nick
            /// Create Date:2004-12-20
            public void send()
            {
                SendMail();
            }
            /// <summary>
            /// 销毁(预留)
            /// </summary>
            /// Writer:Nick
            /// Create Date:2004-12-20
            public void Dispose()
            {
            }
            #endregion
        }
    }

    --------------------

    调用

       MailSender ms = new MailSender();
               

                ms.From = "lilibobo@yeah.net";
                string[] strs= new string[1];
                strs[0]="jiesuganqing@163.com";

                ms.To = strs;

                ms.Subject = "Subject";

                ms.Body = "body text";

                ms.UserName = "*********";

                ms.Password = "********";

                ms.Server = "smtp.yeah.net";


                //ms.Attachments.Add(new MailSender.AttachmentInfo(@"D:\test.txt"));


                Console.WriteLine("mail sending...");

                try
                {

                    ms.send();

                    Console.WriteLine("mail sended.");

                }

                catch (Exception e)
                {

                    Console.WriteLine(e);

                }

       MailSender ms = new MailSender();
               

                ms.From = "********@yeah.net";
                string[] strs= new string[1];
                strs[0]="*******@163.com";

                ms.To = strs;

                ms.Subject = "Subject";

                ms.Body = "body text";

                ms.UserName = "*******"; //用户名, 怎么能告诉你呢

                ms.Password = "********"; // 密码 ,怎么能告诉你呢

                ms.Server = "smtp.yeah.net";


                //ms.Attachments.Add(new MailSender.AttachmentInfo(@"D:\test.txt"));


                Console.WriteLine("mail sending...");

                try
                {

                    ms.send();

                    Console.WriteLine("mail sended.");

                }

                catch (Exception e)
                {

                    Console.WriteLine(e);

                }

     

  • 相关阅读:
    vue:自定义指令
    vue去掉严格开发,即去掉vue-cli安装时的eslint
    vue路由6:导航钩子
    vue路由5:命名视图
    vue路由3:子路由
    vue组件通信之任意级组件之间的通信
    vue中组件通信之子父通信
    令狐冲也是个情场高手啊
    Java —— 时区(夏令时)问题
    PL/SQL 如何导出INSERT语句
  • 原文地址:https://www.cnblogs.com/xianzuoqiaoqi/p/1453006.html
Copyright © 2020-2023  润新知