邮件类

 public class MailUnit
    {
        public string smtp;
        public string from;
        public string pwd;
        public string to;
        public string title;
        public string body;
        public ArrayList paths;
        /// <summary>
        /// 发送邮件单元类
        /// </summary>
        /// <param name="Psmtp">SMYP服务器地址</param>
        /// <param name="Pfrom">发件人地址</param>
        /// <param name="Ppwd">发件人密码</param>
        /// <param name="Pto">收件人地址</param>
        /// <param name="Ptitle">主题</param>
        /// <param name="Pbody">正文</param>
        /// <param name="Ppaths">附件</param>
        public MailUnit(string Psmtp, string Pfrom, string Ppwd, string Pto, string Ptitle, string Pbody, ArrayList Ppaths)
        {
            smtp = Psmtp; from = Pfrom; pwd = Ppwd; to = Pto; title = Ptitle; body = Pbody; paths = Ppaths;
        }
        /*发邮件*/
        public bool SendMail()
        {
            //创建smtpclient对象
            System.Net.Mail.SmtpClient client = new SmtpClient();
            client.Host = smtp;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(from, pwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //创建mailMessage对象 
            System.Net.Mail.MailMessage message = new MailMessage(from, to);
            message.Subject = title;
            //正文默认格式为html
            message.Body = body;
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            //添加附件
            if (paths!=null && paths.Count != 0)
            {
                foreach (string path in paths)
                {
                    Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
                    message.Attachments.Add(data);
                }
            }
            try { client.Send(message); return true; }//MessageBox.Show("邮件发送成功."); 
            catch { return false; }//MessageBox.Show("邮件发送失败." + ex.ToString());
        }
    }

 

posted @ 2015-12-16 23:17  Leo的日记本  阅读(167)  评论(0编辑  收藏  举报