“邮件无法发送到 SMTP 服务器。传输错误代码为 0x80040217. The transport error code was 0x80040217. The server response was not available

出现这个问题,一定是用了 System.Web.Mail 吧,丢弃吧,这东西已经过时了,使用 System.Net.Mail 吧。

 

public class NetSendMail
    {
        /**/
        /// <summary>
        /// 发送邮件类 的摘要说明
        /// </summary>

        #region 数据成员
        //收件人地址
        private string m_To = "";
        //发件人地址
        private string m_From = "";
        //邮件标题
        private string m_Subject = "";
        //邮件正文
        private string m_Body = "";
        //发送服务器名或地址
        private string m_Host = "";
        //发件人用户名
        private string m_UserName = "";
        //发件人密码
        private string m_Password = "";
        //邮件附件
        private string m_File = "";
        #endregion

        #region 构造函数
        /**/
        /// <summary>
        /// 构造函数重载
        /// </summary>
        /// <param name="to">收件人地址</param>
        /// <param name="from">发件人地址</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="body">邮件正文</param>
        /// <param name="host">发送邮件服务器名或地址</param>
        /// <param name="userName">发件人用户名</param>
        /// <param name="password">发件人密码</param>
        public NetSendMail(string to, string from, string subject, string body, string host, string userName, string password, string fileName)
        {
            m_To = to;
            m_From = from;
            m_Subject = subject;
            m_Body = body;
            m_Host = host;
            m_UserName = userName;
            m_Password = password;
            m_File = fileName;
        }
        #endregion

        #region 数据属性
        //收件人地址
        public string TO
        {
            get
            {
                return m_To;
            }
            set
            {
                m_To = value;
            }
        }

        //发件人地址
        public string From
        {
            get
            {
                return m_From;
            }
            set
            {
                m_From = value;
            }
        }

        //邮件标题
        public string Subject
        {
            get
            {
                return m_Subject;
            }
            set
            {
                m_Subject = value;
            }
        }

        //邮件正文
        public string Body
        {
            get
            {
                return m_Body;
            }
            set
            {
                m_Body = value;
            }
        }

        //服务器地址(名)
        public string Host
        {
            get
            {
                return m_Host;
            }
            set
            {
                m_Host = value;
            }
        }

        //发件人的用户名
        public string UserName
        {
            get
            {
                return m_UserName;
            }
            set
            {
                m_UserName = value;
            }
        }

        //发件人的密码
        public string Password
        {
            get
            {
                return m_Password;
            }
            set
            {
                m_Password = value;
            }
        }

        //邮件附件
        public string File
        {
            get
            {
                return m_File;
            }
            set
            {
                m_File = value;
            }
        }
        #endregion

        /**/
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <returns>发送是否成功</returns>
        public bool Send()
        {
            try
            {
                //获取所有的收件人地址
                char[] ch = { ',' };
                string[] address = m_To.Split(ch);
                MailAddressCollection allAddress = new MailAddressCollection();
                for (int i = 0; i < address.Length; i++)
                {
                    //收件人地址
                    MailAddress toAddress = new MailAddress(address[i]);
                    allAddress.Add(toAddress);

                    //发件人地址
                    MailAddress fromAddress = new MailAddress(m_From);

                    //定义邮件消息
                    MailMessage msg = new MailMessage(fromAddress, toAddress);
                    //邮件标题
                    msg.Subject = m_Subject;
                    //邮件正文
                    msg.Body = m_Body;

                    //设置是否以html方式发送
                    msg.IsBodyHtml = true;

                    //获取所有邮件附件
                    char[] cr = { ';' };
                    string[] file = m_File.Split(cr);
                    for (int n = 0; n < file.Length; n++)
                    {
                        if (file[n] != "")
                        {
                            //附件对象
                            Attachment data = new Attachment(file[n], MediaTypeNames.Application.Octet);
                            //附件资料
                            ContentDisposition disposition = data.ContentDisposition;
                            disposition.CreationDate = System.IO.File.GetCreationTime(file[n]);
                            disposition.ModificationDate = System.IO.File.GetLastWriteTime(file[n]);
                            disposition.ReadDate = System.IO.File.GetLastAccessTime(file[n]);
                            //加入邮件附件
                            msg.Attachments.Add(data);
                        }
                    }

                    //使用简单邮件传输协议来传送邮件
                    SmtpClient sendMail = new SmtpClient();
                    //发送邮件的服务器名或地址
                    sendMail.Host = m_Host;
                    //验证发件人的身份
                    sendMail.Credentials = new NetworkCredential(m_UserName, m_Password);
                    //处理待发送邮件的方法
                    sendMail.DeliveryMethod = SmtpDeliveryMethod.Network;

                    

                    //发送邮件
                    sendMail.Send(msg);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

    }

posted @ 2011-05-18 16:25  caicainiao  阅读(3482)  评论(0编辑  收藏  举报