using System.Collections.Generic;namespace LBC.Mail
{
    /// <summary>
    /// 提高用于构造电子邮件的属性和方法
    /// </summary>
    public class MaillMessage
    {
        private string to;
        private string from;
        private string password;
        private string host;
        private string body;
        private string title;
        private bool isHtml;
        private MailPriority priority;
        private System.Text.Encoding bodyEncoding;
        private IList<Attachment> attachments = new List<Attachment>();        /// <summary>
        /// 获取或设置用于存储附加到此电子邮件的数据的附件集合
        /// </summary>
        public IList<Attachment> Attachments
        {
            get
            {
                return attachments;
            }
            set
            {
                attachments = value;
            }
        }        /// <summary>
        /// 获取或甚至邮件正文编码格式
        /// </summary>
        public System.Text.Encoding BodyEncoding
        {
            get
            {
                return bodyEncoding;
            }
            set
            {
                bodyEncoding = value;
            }
        }        /// <summary>
        /// 获取或设置收件人邮箱地址
        /// </summary>
        public string To
        {
            set
            {
                to = value;
            }
            get
            {
                return to;
            }
        }        /// <summary>
        /// 获取或设置发件人邮箱地址
        /// </summary>
        public string From
        {
            set
            {
                from = value;
            }
            get
            {
                return from;
            }
        }        /// <summary>
        /// 获取或设置发件人邮箱地址的秘密
        /// </summary>
        public string Password
        {
            set
            {
                password = value;
            }
            get
            {
                return password;
            }
        }        /// <summary>
        /// 获取或设置此邮件的正文是否为html格式
        /// </summary>
        public bool IsHtml
        {
            set
            {
                isHtml = value;
            }
            get
            {
                return isHtml;
            }
        }        /// <summary>
        /// 获取或设置用于SMTP邮件发送的主机地址
        /// </summary>
        public string Host
        {
            set
            {
                host = value;
            }
            get
            {
                return host;
            }
        }        /// <summary>
        /// 获取或设置邮件正文
        /// </summary>
        public string Body
        {
            set
            {
                body = value;
            }
            get
            {
                return body;
            }
        }        /// <summary>
        /// 获取或设置邮件标题
        /// </summary>
        public string Title
        {
            set
            {
                title = value;
            }
            get
            {
                return title;
            }
        }        /// <summary>
        /// 获取或设置电子邮件的优先级
        /// </summary>
        public MailPriority Priority
        {
            get
            {
                return priority;
            }
            set
            {
                priority = value;
            }
        }
    }
}

解决方案 »

  1.   


    using System;
    using System.Web.Mail;namespace LBC.Mail
    {    /// <summary>
        /// 发送邮件的类(System.Web.Mail)
        /// </summary>
        public class WebMail : IMail
        {
            private MaillMessage message;
            private System.Web.Mail.MailMessage webMailMessage;        /// <summary>
            /// 实例化类的实例
            /// </summary>
            public WebMail()
            {
                message = new MaillMessage();
                webMailMessage = new MailMessage();
            }        /// <summary>
            /// 实例化类的实例
            /// </summary>
            /// <param name="message">可以使用SMTP发送的邮件地址</param>
            public WebMail(LBC.Mail.MaillMessage message)
                : this()
            {
                this.message = message;
            }        /// <summary>
            /// 获取或设置可以使用SMTP发送的邮件地址
            /// </summary>
            public MaillMessage Message
            {
                get
                {
                    return message;
                }
                set
                {
                    message = value;
                }
            }        private void Init()
            {
                if (!string.IsNullOrEmpty(message.From))
                {
                    int index = message.From.IndexOf('@');
                    if (index < 0)
                    {
                        throw new FormatException("邮件格式不正确");
                    }
                    //设置为需要用户验证   
                    webMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                    //设置验证用户名   
                    webMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", message.From.Substring(0, index));
                    //设置验证密码   
                    webMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", message.Password);
                }
                webMailMessage.From = message.From;
                webMailMessage.To = message.To;
                webMailMessage.Subject = message.Title;
                webMailMessage.Body = message.Body;
                webMailMessage.BodyEncoding = message.BodyEncoding;
                webMailMessage.Priority = (System.Web.Mail.MailPriority)message.Priority;
                webMailMessage.BodyFormat = (message.IsHtml ? MailFormat.Html : MailFormat.Text);
                if (message.Attachments != null)
                {
                    System.Web.Mail.MailAttachment attachment = null;
                    for (int i = 0; i < message.Attachments.Count; i++)
                    {
                        attachment = new MailAttachment(message.Attachments[i].FileName);
                        webMailMessage.Attachments.Add(attachment);
                    }
                }
            }        /// <summary>
            /// 获取主机地址
            /// </summary>
            private string Host
            {
                get
                {
                    string host = string.Empty; ;
                    if (string.IsNullOrEmpty(message.Host))
                    {
                        int index = message.From.IndexOf('@');
                        if (index < 0)
                        {
                            throw new FormatException("邮件格式不正确");
                        }
                        if (!string.IsNullOrEmpty(message.From))
                        {
                            host = "smtp." + message.From.Substring(index + 1);
                        }
                        else
                        {
                            host = message.Host;
                        }
                    }
                    else
                    {
                        host = message.Host;
                    }
                    return host;
                }
            }        /// <summary>
            /// 发送邮件
            /// </summary>
            public void Send()
            {
                Init();
                SmtpMail.SmtpServer = Host;
                SmtpMail.Send(webMailMessage);
            }
        }
    }
      

  2.   


    using System;
    using System.Net.Mail;namespace LBC.Mail
    {
        /// <summary>
        /// 发送邮件的类(System.Net.Mail)
        /// </summary>
        public class NetMail : IMail, System.IDisposable
        {
            private SmtpClient smtpClient;
            private System.Net.Mail.MailMessage netMailMessage;
            private LBC.Mail.MaillMessage message;        /// <summary>
            /// 实例化类的实例
            /// </summary>
            public NetMail()
            {
                netMailMessage = new MailMessage();
                smtpClient = new SmtpClient();
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = false;
            }        /// <summary>
            /// 实例化类的实例
            /// </summary>
            /// <param name="message">可以使用SMTP发送的邮件地址</param>
            public NetMail(LBC.Mail.MaillMessage message)
                : this()
            {
                this.message = message;
            }        /// <summary>
            /// 获取或设置可以使用SMTP发送的邮件地址
            /// </summary>
            public LBC.Mail.MaillMessage Message
            {
                get
                {
                    return message;
                }
                set
                {
                    message = value;
                }
            }        private void Init()
            {
                netMailMessage.IsBodyHtml = message.IsHtml;
                netMailMessage.Body = message.Body;
                netMailMessage.From = new MailAddress(message.From);
                netMailMessage.To.Add(message.To);
                netMailMessage.Subject = message.Title;
                netMailMessage.BodyEncoding = message.BodyEncoding;
                netMailMessage.Priority = (System.Net.Mail.MailPriority)message.Priority;
                smtpClient.Credentials = new System.Net.NetworkCredential(netMailMessage.From.Address, message.Password);//设置发件人身份的票据
                if (message.Attachments != null)
                {
                    System.Net.Mail.Attachment attachment = null;
                    for (int i = 0; i < message.Attachments.Count; i++)
                    {
                        attachment = new System.Net.Mail.Attachment(message.Attachments[i].FileName);
                        netMailMessage.Attachments.Add(attachment);
                    }
                }
            }        /// <summary>
            /// 获取主机地址
            /// </summary>
            private string Host
            {
                get
                {
                    string host = string.Empty; ;
                    if (string.IsNullOrEmpty(message.Host))
                    {
                        int index = message.From.IndexOf('@');
                        if (index < 0)
                        {
                            throw new System.FormatException("邮件格式不正确");
                        }
                        if (!string.IsNullOrEmpty(message.From))
                        {
                            host = "smtp." + message.From.Substring(index + 1);
                        }
                        else
                        {
                            host = message.Host;
                        }
                    }
                    else
                    {
                        host = message.Host;
                    }
                    return host;
                }
            }        /// <summary>
            /// 发送邮件
            /// </summary>
            public void Send()
            {
                Init();
                smtpClient.Host = Host;
                smtpClient.Send(netMailMessage);
            }        #region IDisposable 成员        /// <summary>
            /// 清理资源
            /// </summary>
            public virtual void Dispose()
            {
                if (netMailMessage != null)
                {
                    netMailMessage.Dispose();
                }
            }        #endregion
        }
    }
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace LBC.Mail
    {
        /// <summary>
        /// 提供发送邮件的方法
        /// </summary>
        public interface IMail
        {
            /// <summary>
            /// 发送邮件
            /// </summary>
            void Send();        /// <summary>
            /// 获取或设置可以使用SMTP发送的邮件地址
            /// </summary>
            LBC.Mail.MaillMessage Message { get; set; }
        }
    }using System;namespace LBC.Mail
    {    /// <summary>
        /// 电子邮件附件
        /// </summary>
        public class Attachment
        {
            private string fileName;        /// <summary>
            /// 用附件的指定文件名初始化Attachment 类的新实例
            /// </summary>
            /// <param name="fileName">附件文件的名称</param>
            public Attachment(string fileName)
            {
                this.fileName = fileName;
            }        /// <summary>
            /// 获取附件文件的名称
            /// </summary>
            public string FileName
            {
                get
                {
                    return fileName;
                }
            }
        }
    }
      

  4.   


    /// <summary>
        /// 电子邮件的优先级
        /// </summary>
        public enum MailPriority
        {
            /// <summary>
            /// 指定电子邮件具有普通优先级
            /// </summary>
            Normal = 0,        /// <summary>
            /// 指定电子邮件具有低优先级
            /// </summary>
            Low = 1,        /// <summary>
            /// 指定电子邮件具有高优先级
            /// </summary>
            High = 2,
        }
      

  5.   

    大家给点意见,这样写好不好啊昨天网站的代码突然发送失败了,就临时写成现在这样的,用web发送的就可以了,微软还说过时
    net里面发送的却不行,不知道代码有没有写错,不过以前是可以用的
      

  6.   

    顶,为啥还分web和net的呢?有啥不一样的用途?
      

  7.   

    .net分2种形式发送邮件啊
    一个是System.Web.Mail一个是System.Net.Mail
      

  8.   

    System.Web.Mai好像是在1.1时候使用
    System.Net.Mail是2.0下的,推荐使用System.Net.Mail
      

  9.   

    System.Net.Mail是2.0下的,推荐使用System.Net.Mail就是用这个错的啊
    我现在改1.0就没错了