码迷,mamicode.com
首页 > 其他好文 > 详细

邮件类

时间:2015-12-17 00:23:41      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

 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());
        }
    }

 

邮件类

标签:

原文地址:http://www.cnblogs.com/shanoon/p/5052708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!