邮件发送类

来源:互联网 发布:windows linux mac知乎 编辑:程序博客网 时间:2024/05/20 09:47
using System; using System.Collections.Generic;using System.Text;using System.Collections;using System.Net.Mail;namespace LeafLib{    /// <summary>    /// 邮件发送类    /// </summary>    public class MailUnit    {        public string smtp;        public string from;        public string pwd;        public string to;        public string subject;        public string body;        public ArrayList paths;        /// <summary>        /// 邮件设置        /// </summary>        /// <param name="Psmtp">SMTP地址</param>        /// <param name="Pfrom">发件人邮箱地址</param>        /// <param name="Ppwd">发件人邮箱密码</param>        /// <param name="Pto">收件人邮箱地址</param>        /// <param name="Psubject">邮件标题</param>        /// <param name="Pbody">邮件内容</param>        /// <param name="Ppaths">附件</param>        public MailUnit(string Psmtp, string Pfrom, string Ppwd, string Pto, string Psubject, string Pbody,ArrayList Ppaths)        {            smtp = Psmtp;            from = Pfrom;            pwd = Ppwd;            to = Pto;            subject = Psubject;            body = Pbody;            paths = Ppaths;        }        /*发邮件*/        public bool SendMail()        {            try            {            //创建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 = subject;            //正文默认格式为html            message.Body = body;            message.IsBodyHtml = true;            message.BodyEncoding = System.Text.Encoding.UTF8;            //添加附件            if (paths.Count != 0)            {                foreach (string path in paths)                {                    message.Attachments.Add(new Attachment(path));                }            }                client.Send(message);                return true;            }            catch(Exception)            {                return false;            }        }    }    }}


原创粉丝点击