码迷,mamicode.com
首页 > Windows程序 > 详细

C#邮件收发

时间:2016-02-21 15:44:20      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

    public class Mail
    {
        private string mailkey;
        private string mailSecret;
        public string mailFrom;

        public Mail()
        {
            this.mailkey = BaseHelper.GetValue("MailKey");
            this.mailSecret = BaseHelper.GetValue("MailSecret");
            this.mailFrom = BaseHelper.GetValue("MailFrom");
        }

        public Mail(string key,string secret,string username)
        {
            this.mailkey = key;
            this.mailSecret = secret;
            this.mailFrom = username;
        }

        /// <summary>
        /// 群发邮件
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="toUsers">接收方</param>
        /// <returns></returns>
        public ResultDTO Send(string title,string content,string[] toUsers)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.Subject = title;
                mail.From = new MailAddress(this.mailkey,this.mailFrom);
                foreach (var user in toUsers)
                {
                    mail.To.Add(new MailAddress(user));
                }
                mail.Body = content;

                mail.BodyEncoding = Encoding.UTF8;
                mail.IsBodyHtml = true;
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.163.com";
                client.Port = 25;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(this.mailkey,this.mailSecret);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(mail);
                return WebApi.Success("邮件发送成功!");
            }
            catch(Exception e)
            {
                return WebApi.Error(e.Message);
            }
        }

        /// <summary>
        /// 单发邮件
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="toUser"></param>
        /// <returns></returns>
        public ResultDTO Send(string title, string content, string toUser)
        {
            return Send(title, content, new[] {toUser});
        }
    }

  

C#邮件收发

标签:

原文地址:http://www.cnblogs.com/xuhang/p/5204957.html

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